Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Override correct AgentExecutor method for async runs
Vasek Mlejnsky3dfc3470c734
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.pyindex 42d5fafbf..3c805ac86 100644--- a/api-service/codegen/agent/base.py+++ b/api-service/codegen/agent/base.py@@ -220,6 +220,115 @@ class CodegenAgentExecutor(AgentExecutor):)return observation+ async def _arun_tool(+ self,+ tool_name: str,+ tool_input: str,+ color_mapping: Dict[str, str],+ name_to_tool_map: Dict[str, BaseTool],+ ) -> str:+ # Otherwise we lookup the tool+ if tool_name in name_to_tool_map:+ tool = name_to_tool_map[tool_name]+ return_direct = tool.return_direct+ color = color_mapping[tool_name]+ llm_prefix = "" if return_direct else self.agent.llm_prefix+ # We then call the tool on the tool input to get an observation+ observation = await tool.arun(+ tool_input,+ verbose=self.verbose,+ color=color,+ llm_prefix=llm_prefix,+ observation_prefix=self.agent.observation_prefix,+ )+ else:+ observation = await InvalidTool().arun(+ tool_name,+ verbose=self.verbose,+ color=None,+ llm_prefix="",+ observation_prefix=self.agent.observation_prefix,+ )+ return observation++ async def _atake_next_step(+ self,+ name_to_tool_map: Dict[str, BaseTool],+ color_mapping: Dict[str, str],+ inputs: Dict[str, str],+ intermediate_steps: List[Tuple[AgentAction, str]],+ ) -> Union[AgentFinish, Tuple[AgentAction, str]]:+ """Take a single step in the thought-action-observation loop.++ Override this to take control of how the agent makes and acts on choices.+ """+ # Call the LLM to see what to do.+ output = await self.agent.aplan(intermediate_steps, **inputs)+ # If the tool chosen is the finishing tool, then we end and return.+ if isinstance(output, AgentFinish):+ return output+ # Sometimes the LLM decides to pass the value of `FINAL_ANSWER_ACTION` as a tool name.+ # Instead of trying to "force" the LLM to don't do that, we can just write a bit of+ # code and handle this case.+ elif output.tool == FINAL_ANSWER_ACTION:+ return AgentFinish({"output": output.tool_input}, output.log)++ # Sometimes the agent just completely messed up the output format.+ # We want to remind it that the last answer was wrong and it should+ # follow the format.+ if output.tool == MALFORMED_ANSWER:+ return output, output.tool_input++ if self.callback_manager.is_async:+ await self.callback_manager.on_agent_action(+ output, verbose=self.verbose, color="green"+ )+ else:+ self.callback_manager.on_agent_action(+ output, verbose=self.verbose, color="green"+ )++ # The `ACTIONS_QUEUE` isn't really a name of a tool.+ # It's a way for us to specify that the LLM passed multiple actions in a single response.+ # We handle this case by running each action one by one.+ if output.tool == ACTIONS_QUEUE:+ observation = ""++ # The `output.tool_input` is a raw action string+ actions = parse_action_string(output.tool_input)++ # Go through each action and run it.+ # Collect outputs from each action.+ for action in actions:+ tool_name = action.attrib["tool"]+ tool_input = action.text+ observation = await self._arun_tool(+ tool_name=tool_name,+ tool_input=tool_input,+ name_to_tool_map=name_to_tool_map,+ color_mapping=color_mapping,+ )+ # observation = (+ # observation+ # + f"Output of action '{tool_name}':\n"+ # + self._run_tool(+ # tool_name=tool_name,+ # tool_input=tool_input,+ # name_to_tool_map=name_to_tool_map,+ # color_mapping=color_mapping,+ # )+ # + "==="+ # )++ else:+ observation = await self._arun_tool(+ tool_name=output.tool,+ tool_input=output.tool_input,+ name_to_tool_map=name_to_tool_map,+ color_mapping=color_mapping,+ )+ return output, observation+def _take_next_step(self,name_to_tool_map: Dict[str, BaseTool],@@ -241,11 +350,7 @@ class CodegenAgentExecutor(AgentExecutor):# Instead of trying to "force" the LLM to don't do that, we can just write a bit of# code and handle this case.elif output.tool == FINAL_ANSWER_ACTION:- return AgentFinish({"output": action.tool_input}, action.log)-- self.callback_manager.on_agent_action(- output, verbose=self.verbose, color="green"- )+ return AgentFinish({"output": output.tool_input}, output.log)# Sometimes the agent just completely messed up the output format.# We want to remind it that the last answer was wrong and it should@@ -253,6 +358,10 @@ class CodegenAgentExecutor(AgentExecutor):if output.tool == MALFORMED_ANSWER:return output, output.tool_input+ self.callback_manager.on_agent_action(+ output, verbose=self.verbose, color="green"+ )+# The `ACTIONS_QUEUE` isn't really a name of a tool.# It's a way for us to specify that the LLM passed multiple actions in a single response.# We handle this case by running each action one by one.diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.pyindex a2134ff9e..09f289b17 100644--- a/api-service/codegen/tools/playground/tools/code.py+++ b/api-service/codegen/tools/playground/tools/code.py@@ -52,7 +52,7 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory, **@async_tool("CurlJavaScriptServer")async def curl_javascript_server(empty: str) -> str:"""- Make a request to check if the previously run JavaScript code is a server that can handle the needed request. Example usage:+ Make a request to check if the previously run JavaScript code is a server that can handle the needed request. This tool has no input. Example usage:```<action tool="CurlJavaScriptServer"></action>