mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-21 14:18:52 +00:00
langchain[patch]: Extract _aperform_agent_action from _aiter_next_step from AgentExecutor (#15707)
- **Description:** extreact the _aperform_agent_action in the AgentExecutor class to allow for easier overriding. Extracted logic from _iter_next_step into a new method _perform_agent_action for consistency and easier overriding. - **Issue:** #15706 Closes #15706
This commit is contained in:
parent
95ee69a301
commit
c69f599594
@ -1179,6 +1179,17 @@ class AgentExecutor(Chain):
|
|||||||
for agent_action in actions:
|
for agent_action in actions:
|
||||||
yield agent_action
|
yield agent_action
|
||||||
for agent_action in actions:
|
for agent_action in actions:
|
||||||
|
yield self._perform_agent_action(
|
||||||
|
name_to_tool_map, color_mapping, agent_action, run_manager
|
||||||
|
)
|
||||||
|
|
||||||
|
def _perform_agent_action(
|
||||||
|
self,
|
||||||
|
name_to_tool_map: Dict[str, BaseTool],
|
||||||
|
color_mapping: Dict[str, str],
|
||||||
|
agent_action: AgentAction,
|
||||||
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
||||||
|
) -> AgentStep:
|
||||||
if run_manager:
|
if run_manager:
|
||||||
run_manager.on_agent_action(agent_action, color="green")
|
run_manager.on_agent_action(agent_action, color="green")
|
||||||
# Otherwise we lookup the tool
|
# Otherwise we lookup the tool
|
||||||
@ -1209,7 +1220,7 @@ class AgentExecutor(Chain):
|
|||||||
callbacks=run_manager.get_child() if run_manager else None,
|
callbacks=run_manager.get_child() if run_manager else None,
|
||||||
**tool_run_kwargs,
|
**tool_run_kwargs,
|
||||||
)
|
)
|
||||||
yield AgentStep(action=agent_action, observation=observation)
|
return AgentStep(action=agent_action, observation=observation)
|
||||||
|
|
||||||
async def _atake_next_step(
|
async def _atake_next_step(
|
||||||
self,
|
self,
|
||||||
@ -1303,8 +1314,26 @@ class AgentExecutor(Chain):
|
|||||||
for agent_action in actions:
|
for agent_action in actions:
|
||||||
yield agent_action
|
yield agent_action
|
||||||
|
|
||||||
|
# Use asyncio.gather to run multiple tool.arun() calls concurrently
|
||||||
|
result = await asyncio.gather(
|
||||||
|
*[
|
||||||
|
self._aperform_agent_action(
|
||||||
|
name_to_tool_map, color_mapping, agent_action, run_manager
|
||||||
|
)
|
||||||
|
for agent_action in actions
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO This could yield each result as it becomes available
|
||||||
|
for chunk in result:
|
||||||
|
yield chunk
|
||||||
|
|
||||||
async def _aperform_agent_action(
|
async def _aperform_agent_action(
|
||||||
|
self,
|
||||||
|
name_to_tool_map: Dict[str, BaseTool],
|
||||||
|
color_mapping: Dict[str, str],
|
||||||
agent_action: AgentAction,
|
agent_action: AgentAction,
|
||||||
|
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
|
||||||
) -> AgentStep:
|
) -> AgentStep:
|
||||||
if run_manager:
|
if run_manager:
|
||||||
await run_manager.on_agent_action(
|
await run_manager.on_agent_action(
|
||||||
@ -1340,15 +1369,6 @@ class AgentExecutor(Chain):
|
|||||||
)
|
)
|
||||||
return AgentStep(action=agent_action, observation=observation)
|
return AgentStep(action=agent_action, observation=observation)
|
||||||
|
|
||||||
# Use asyncio.gather to run multiple tool.arun() calls concurrently
|
|
||||||
result = await asyncio.gather(
|
|
||||||
*[_aperform_agent_action(agent_action) for agent_action in actions]
|
|
||||||
)
|
|
||||||
|
|
||||||
# TODO This could yield each result as it becomes available
|
|
||||||
for chunk in result:
|
|
||||||
yield chunk
|
|
||||||
|
|
||||||
def _call(
|
def _call(
|
||||||
self,
|
self,
|
||||||
inputs: Dict[str, str],
|
inputs: Dict[str, str],
|
||||||
|
Loading…
Reference in New Issue
Block a user