Return response when agent has been stopped due to max iterations.
(
self,
early_stopping_method: str,
intermediate_steps: List[Tuple[AgentAction, str]],
**kwargs: Any,
)
| 240 | ) |
| 241 | |
| 242 | def return_stopped_response( |
| 243 | self, |
| 244 | early_stopping_method: str, |
| 245 | intermediate_steps: List[Tuple[AgentAction, str]], |
| 246 | **kwargs: Any, |
| 247 | ) -> AgentFinish: |
| 248 | """Return response when agent has been stopped due to max iterations.""" |
| 249 | if early_stopping_method == "force": |
| 250 | # `force` just returns a constant string |
| 251 | return AgentFinish( |
| 252 | {"output": "Agent stopped due to iteration limit or time limit."}, "" |
| 253 | ) |
| 254 | elif early_stopping_method == "generate": |
| 255 | # Generate does one final forward pass |
| 256 | thoughts = "" |
| 257 | for action, observation in intermediate_steps: |
| 258 | thoughts += action.log |
| 259 | thoughts += ( |
| 260 | f"\n{self.observation_prefix}{observation}\n{self.llm_prefix}" |
| 261 | ) |
| 262 | # Adding to the previous steps, we now tell the LLM to make a final pred |
| 263 | thoughts += ( |
| 264 | "\n\nI now need to return a final answer based on the previous steps:" |
| 265 | ) |
| 266 | new_inputs = {"agent_scratchpad": thoughts, "stop": self._stop} |
| 267 | full_inputs = {**kwargs, **new_inputs} |
| 268 | full_output = self.llm_chain.predict(**full_inputs) |
| 269 | # We try to extract a final answer |
| 270 | parsed_output = self.output_parser.parse(full_output) |
| 271 | if isinstance(parsed_output, AgentFinish): |
| 272 | # If we can extract, we send the correct stuff |
| 273 | return parsed_output |
| 274 | else: |
| 275 | # If we can extract, but the tool is not the final tool, |
| 276 | # we just return the full output |
| 277 | return AgentFinish({"output": full_output}, full_output) |
| 278 | else: |
| 279 | raise ValueError( |
| 280 | "early_stopping_method should be one of `force` or `generate`, " |
| 281 | f"got {early_stopping_method}" |
| 282 | ) |
| 283 | |
| 284 | def tool_run_logging_kwargs(self) -> Dict: |
| 285 | return { |