@@ -48,6 +48,29 @@ def _fix_text(self, text: str) -> str:
48
48
def _stop (self ) -> List [str ]:
49
49
return [f"\n { self .observation_prefix } " ]
50
50
51
+ def _construct_scratchpad (
52
+ self , intermediate_steps : List [Tuple [AgentAction , str ]]
53
+ ) -> str :
54
+ """Construct the scratchpad that lets the agent continue its thought process."""
55
+ thoughts = ""
56
+ for action , observation in intermediate_steps :
57
+ thoughts += action .log
58
+ thoughts += f"\n { self .observation_prefix } { observation } \n { self .llm_prefix } "
59
+ return thoughts
60
+
61
+ def _get_next_action (self , full_inputs : Dict [str , str ]) -> AgentAction :
62
+ full_output = self .llm_chain .predict (** full_inputs )
63
+ parsed_output = self ._extract_tool_and_input (full_output )
64
+ while parsed_output is None :
65
+ full_output = self ._fix_text (full_output )
66
+ full_inputs ["agent_scratchpad" ] += full_output
67
+ output = self .llm_chain .predict (** full_inputs )
68
+ full_output += output
69
+ parsed_output = self ._extract_tool_and_input (full_output )
70
+ return AgentAction (
71
+ tool = parsed_output [0 ], tool_input = parsed_output [1 ], log = full_output
72
+ )
73
+
51
74
def plan (
52
75
self , intermediate_steps : List [Tuple [AgentAction , str ]], ** kwargs : Any
53
76
) -> Union [AgentAction , AgentFinish ]:
@@ -61,24 +84,14 @@ def plan(
61
84
Returns:
62
85
Action specifying what tool to use.
63
86
"""
64
- thoughts = ""
65
- for action , observation in intermediate_steps :
66
- thoughts += action .log
67
- thoughts += f"\n { self .observation_prefix } { observation } \n { self .llm_prefix } "
87
+ thoughts = self ._construct_scratchpad (intermediate_steps )
68
88
new_inputs = {"agent_scratchpad" : thoughts , "stop" : self ._stop }
69
89
full_inputs = {** kwargs , ** new_inputs }
70
- full_output = self .llm_chain .predict (** full_inputs )
71
- parsed_output = self ._extract_tool_and_input (full_output )
72
- while parsed_output is None :
73
- full_output = self ._fix_text (full_output )
74
- full_inputs ["agent_scratchpad" ] += full_output
75
- output = self .llm_chain .predict (** full_inputs )
76
- full_output += output
77
- parsed_output = self ._extract_tool_and_input (full_output )
78
- tool , tool_input = parsed_output
79
- if tool == self .finish_tool_name :
80
- return AgentFinish ({"output" : tool_input }, full_output )
81
- return AgentAction (tool , tool_input , full_output )
90
+
91
+ action = self ._get_next_action (full_inputs )
92
+ if action .tool == self .finish_tool_name :
93
+ return AgentFinish ({"output" : action .tool_input }, action .log )
94
+ return action
82
95
83
96
def prepare_for_new_call (self ) -> None :
84
97
"""Prepare the agent for new call, if needed."""
0 commit comments