(self, messages: List[Message], lang: Literal['en', 'zh'] = 'en', **kwargs)
| 110 | raise e # Raise the exception if the last retry fails |
| 111 | |
| 112 | def _run(self, messages: List[Message], lang: Literal['en', 'zh'] = 'en', **kwargs) -> Iterator[List[Message]]: |
| 113 | text_messages = self._prepend_react_prompt(messages, lang=lang) |
| 114 | num_llm_calls_available = MAX_LLM_CALL_PER_RUN |
| 115 | response: str = 'Thought: ' |
| 116 | query = self.llm_cfg["query"] |
| 117 | action_count = self.llm_cfg.get("action_count", MAX_LLM_CALL_PER_RUN) |
| 118 | num_llm_calls_available = action_count |
| 119 | while num_llm_calls_available > 0: |
| 120 | num_llm_calls_available -= 1 |
| 121 | output = [] |
| 122 | for output in self._call_llm(messages=text_messages): |
| 123 | if output: |
| 124 | yield [Message(role=ASSISTANT, content=output[-1].content)] |
| 125 | # Accumulate the current response |
| 126 | if output: |
| 127 | response += output[-1].content |
| 128 | |
| 129 | has_action, action, action_input, thought = self._detect_tool("\n"+output[-1].content) |
| 130 | if not has_action: |
| 131 | if "Final Answer: " in output[-1].content: |
| 132 | break |
| 133 | else: |
| 134 | continue |
| 135 | |
| 136 | # Add the tool result |
| 137 | query = self.llm_cfg["query"] |
| 138 | observation = self._call_tool(action, action_input, messages=messages, **kwargs) |
| 139 | stage1 = self.observation_information_extraction(query, observation) |
| 140 | if stage1: |
| 141 | self.momery.append(stage1+"\n") |
| 142 | if len(self.momery) > 1: |
| 143 | yield [Message(role=ASSISTANT, content= "Memory:\n" + "-".join(self.momery)+"\"}")] |
| 144 | else: |
| 145 | yield [Message(role=ASSISTANT, content= "Memory:\n" + "-" + self.momery[0]+"\"}")] |
| 146 | stage2 = self.critic_information(query, self.momery) |
| 147 | if stage2: |
| 148 | response = f'Final Answer: {stage2}' |
| 149 | yield [Message(role=ASSISTANT, content=response)] |
| 150 | break |
| 151 | |
| 152 | |
| 153 | observation = f'\nObservation: {observation}\nThought: ' |
| 154 | response += observation |
| 155 | # yield [Message(role=ASSISTANT, content=response)] |
| 156 | |
| 157 | if (not text_messages[-1].content.endswith('\nThought: ')) and (not thought.startswith('\n')): |
| 158 | # Add the '\n' between '\nQuestion:' and the first 'Thought:' |
| 159 | text_messages[-1].content += '\n' |
| 160 | if action_input.startswith('```'): |
| 161 | # Add a newline for proper markdown rendering of code |
| 162 | action_input = '\n' + action_input |
| 163 | text_messages[-1].content += thought + f'\nAction: {action}\nAction Input: {action_input}' + observation |
| 164 | # print(text_messages[-1].content) |
| 165 | |
| 166 | def _prepend_react_prompt(self, messages: List[Message], lang: Literal['en', 'zh']) -> List[Message]: |
| 167 | tool_descs = [] |
nothing calls this directly
no test coverage detected