@ -1179,37 +1179,48 @@ 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 :
if run_manager :
yield self . _perform_agent_action (
run_manager . on_agent_action ( agent_action , color = " green " )
name_to_tool_map , color_mapping , agent_action , run_manager
# Otherwise we lookup the tool
)
if agent_action . tool in name_to_tool_map :
tool = name_to_tool_map [ agent_action . tool ]
def _perform_agent_action (
return_direct = tool . return_direct
self ,
color = color_mapping [ agent_action . tool ]
name_to_tool_map : Dict [ str , BaseTool ] ,
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
color_mapping : Dict [ str , str ] ,
if return_direct :
agent_action : AgentAction ,
tool_run_kwargs [ " llm_prefix " ] = " "
run_manager : Optional [ CallbackManagerForChainRun ] = None ,
# We then call the tool on the tool input to get an observation
) - > AgentStep :
observation = tool . run (
if run_manager :
agent_action . tool_input ,
run_manager . on_agent_action ( agent_action , color = " green " )
verbose = self . verbose ,
# Otherwise we lookup the tool
color = color ,
if agent_action . tool in name_to_tool_map :
callbacks = run_manager . get_child ( ) if run_manager else None ,
tool = name_to_tool_map [ agent_action . tool ]
* * tool_run_kwargs ,
return_direct = tool . return_direct
)
color = color_mapping [ agent_action . tool ]
else :
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
if return_direct :
observation = InvalidTool ( ) . run (
tool_run_kwargs [ " llm_prefix " ] = " "
{
# We then call the tool on the tool input to get an observation
" requested_tool_name " : agent_action . tool ,
observation = tool . run (
" available_tool_names " : list ( name_to_tool_map . keys ( ) ) ,
agent_action . tool_input ,
} ,
verbose = self . verbose ,
verbose = self . verbose ,
color = color ,
color = None ,
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 ,
)
)
else :
yield AgentStep ( action = agent_action , observation = observation )
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
observation = InvalidTool ( ) . run (
{
" requested_tool_name " : agent_action . tool ,
" available_tool_names " : list ( name_to_tool_map . keys ( ) ) ,
} ,
verbose = self . verbose ,
color = None ,
callbacks = run_manager . get_child ( ) if run_manager else None ,
* * tool_run_kwargs ,
)
return AgentStep ( action = agent_action , observation = observation )
async def _atake_next_step (
async def _atake_next_step (
self ,
self ,
@ -1303,52 +1314,61 @@ class AgentExecutor(Chain):
for agent_action in actions :
for agent_action in actions :
yield agent_action
yield agent_action
async def _aperform_agent_action (
agent_action : AgentAction ,
) - > AgentStep :
if run_manager :
await run_manager . on_agent_action (
agent_action , verbose = self . verbose , color = " green "
)
# Otherwise we lookup the tool
if agent_action . tool in name_to_tool_map :
tool = name_to_tool_map [ agent_action . tool ]
return_direct = tool . return_direct
color = color_mapping [ agent_action . tool ]
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
if return_direct :
tool_run_kwargs [ " llm_prefix " ] = " "
# We then call the tool on the tool input to get an observation
observation = await tool . arun (
agent_action . tool_input ,
verbose = self . verbose ,
color = color ,
callbacks = run_manager . get_child ( ) if run_manager else None ,
* * tool_run_kwargs ,
)
else :
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
observation = await InvalidTool ( ) . arun (
{
" requested_tool_name " : agent_action . tool ,
" available_tool_names " : list ( name_to_tool_map . keys ( ) ) ,
} ,
verbose = self . verbose ,
color = None ,
callbacks = run_manager . get_child ( ) if run_manager else None ,
* * tool_run_kwargs ,
)
return AgentStep ( action = agent_action , observation = observation )
# Use asyncio.gather to run multiple tool.arun() calls concurrently
# Use asyncio.gather to run multiple tool.arun() calls concurrently
result = await asyncio . gather (
result = await asyncio . gather (
* [ _aperform_agent_action ( agent_action ) for agent_action in actions ]
* [
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
# TODO This could yield each result as it becomes available
for chunk in result :
for chunk in result :
yield chunk
yield chunk
async def _aperform_agent_action (
self ,
name_to_tool_map : Dict [ str , BaseTool ] ,
color_mapping : Dict [ str , str ] ,
agent_action : AgentAction ,
run_manager : Optional [ AsyncCallbackManagerForChainRun ] = None ,
) - > AgentStep :
if run_manager :
await run_manager . on_agent_action (
agent_action , verbose = self . verbose , color = " green "
)
# Otherwise we lookup the tool
if agent_action . tool in name_to_tool_map :
tool = name_to_tool_map [ agent_action . tool ]
return_direct = tool . return_direct
color = color_mapping [ agent_action . tool ]
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
if return_direct :
tool_run_kwargs [ " llm_prefix " ] = " "
# We then call the tool on the tool input to get an observation
observation = await tool . arun (
agent_action . tool_input ,
verbose = self . verbose ,
color = color ,
callbacks = run_manager . get_child ( ) if run_manager else None ,
* * tool_run_kwargs ,
)
else :
tool_run_kwargs = self . agent . tool_run_logging_kwargs ( )
observation = await InvalidTool ( ) . arun (
{
" requested_tool_name " : agent_action . tool ,
" available_tool_names " : list ( name_to_tool_map . keys ( ) ) ,
} ,
verbose = self . verbose ,
color = None ,
callbacks = run_manager . get_child ( ) if run_manager else None ,
* * tool_run_kwargs ,
)
return AgentStep ( action = agent_action , observation = observation )
def _call (
def _call (
self ,
self ,
inputs : Dict [ str , str ] ,
inputs : Dict [ str , str ] ,