mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 07:35:18 +00:00
agent refactors (#997)
This commit is contained in:
parent
7fb33fca47
commit
0f0e69adce
@ -375,35 +375,22 @@ class AgentExecutor(Chain, BaseModel):
|
|||||||
final_output["intermediate_steps"] = intermediate_steps
|
final_output["intermediate_steps"] = intermediate_steps
|
||||||
return final_output
|
return final_output
|
||||||
|
|
||||||
def _call(self, inputs: Dict[str, str]) -> Dict[str, Any]:
|
def _take_next_step(
|
||||||
"""Run text through and get agent response."""
|
self,
|
||||||
# Make sure that every tool is synchronous (not a coroutine)
|
name_to_tool_map: Dict[str, Tool],
|
||||||
for tool in self.tools:
|
color_mapping: Dict[str, str],
|
||||||
if asyncio.iscoroutinefunction(tool.func):
|
inputs: Dict[str, str],
|
||||||
raise ValueError(
|
intermediate_steps: List[Tuple[AgentAction, str]],
|
||||||
"Tools cannot be asynchronous for `run` method. "
|
) -> Union[AgentFinish, Tuple[AgentAction, str]]:
|
||||||
"Please use `arun` instead."
|
"""Take a single step in the thought-action-observation loop.
|
||||||
)
|
|
||||||
|
|
||||||
# Do any preparation necessary when receiving a new input.
|
Override this to take control of how the agent makes and acts on choices.
|
||||||
self.agent.prepare_for_new_call()
|
"""
|
||||||
# Construct a mapping of tool name to tool for easy lookup
|
|
||||||
name_to_tool_map = {tool.name: tool for tool in self.tools}
|
|
||||||
# We construct a mapping from each tool to a color, used for logging.
|
|
||||||
color_mapping = get_color_mapping(
|
|
||||||
[tool.name for tool in self.tools], excluded_colors=["green"]
|
|
||||||
)
|
|
||||||
intermediate_steps: List[Tuple[AgentAction, str]] = []
|
|
||||||
# Let's start tracking the iterations the agent has gone through
|
|
||||||
iterations = 0
|
|
||||||
# We now enter the agent loop (until it returns something).
|
|
||||||
while self._should_continue(iterations):
|
|
||||||
# Call the LLM to see what to do.
|
# Call the LLM to see what to do.
|
||||||
output = self.agent.plan(intermediate_steps, **inputs)
|
output = self.agent.plan(intermediate_steps, **inputs)
|
||||||
# If the tool chosen is the finishing tool, then we end and return.
|
# If the tool chosen is the finishing tool, then we end and return.
|
||||||
if isinstance(output, AgentFinish):
|
if isinstance(output, AgentFinish):
|
||||||
return self._return(output, intermediate_steps)
|
return output
|
||||||
|
|
||||||
# Otherwise we lookup the tool
|
# Otherwise we lookup the tool
|
||||||
if output.tool in name_to_tool_map:
|
if output.tool in name_to_tool_map:
|
||||||
tool = name_to_tool_map[output.tool]
|
tool = name_to_tool_map[output.tool]
|
||||||
@ -436,11 +423,41 @@ class AgentExecutor(Chain, BaseModel):
|
|||||||
llm_prefix=llm_prefix,
|
llm_prefix=llm_prefix,
|
||||||
verbose=self.verbose,
|
verbose=self.verbose,
|
||||||
)
|
)
|
||||||
intermediate_steps.append((output, observation))
|
|
||||||
if return_direct:
|
if return_direct:
|
||||||
# Set the log to "" because we do not want to log it.
|
# Set the log to "" because we do not want to log it.
|
||||||
output = AgentFinish({self.agent.return_values[0]: observation}, "")
|
return AgentFinish({self.agent.return_values[0]: observation}, "")
|
||||||
return self._return(output, intermediate_steps)
|
return output, observation
|
||||||
|
|
||||||
|
def _call(self, inputs: Dict[str, str]) -> Dict[str, Any]:
|
||||||
|
"""Run text through and get agent response."""
|
||||||
|
# Make sure that every tool is synchronous (not a coroutine)
|
||||||
|
for tool in self.tools:
|
||||||
|
if asyncio.iscoroutinefunction(tool.func):
|
||||||
|
raise ValueError(
|
||||||
|
"Tools cannot be asynchronous for `run` method. "
|
||||||
|
"Please use `arun` instead."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Do any preparation necessary when receiving a new input.
|
||||||
|
self.agent.prepare_for_new_call()
|
||||||
|
# Construct a mapping of tool name to tool for easy lookup
|
||||||
|
name_to_tool_map = {tool.name: tool for tool in self.tools}
|
||||||
|
# We construct a mapping from each tool to a color, used for logging.
|
||||||
|
color_mapping = get_color_mapping(
|
||||||
|
[tool.name for tool in self.tools], excluded_colors=["green"]
|
||||||
|
)
|
||||||
|
intermediate_steps: List[Tuple[AgentAction, str]] = []
|
||||||
|
# Let's start tracking the iterations the agent has gone through
|
||||||
|
iterations = 0
|
||||||
|
# We now enter the agent loop (until it returns something).
|
||||||
|
while self._should_continue(iterations):
|
||||||
|
next_step_output = self._take_next_step(
|
||||||
|
name_to_tool_map, color_mapping, inputs, intermediate_steps
|
||||||
|
)
|
||||||
|
if isinstance(next_step_output, AgentFinish):
|
||||||
|
return self._return(next_step_output, intermediate_steps)
|
||||||
|
|
||||||
|
intermediate_steps.append(next_step_output)
|
||||||
iterations += 1
|
iterations += 1
|
||||||
output = self.agent.return_stopped_response(
|
output = self.agent.return_stopped_response(
|
||||||
self.early_stopping_method, intermediate_steps, **inputs
|
self.early_stopping_method, intermediate_steps, **inputs
|
||||||
|
Loading…
Reference in New Issue
Block a user