Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

commit

Vasek Mlejnsky26ccfbd02804
diff --git a/api-service/codegen/agent/__init__.py b/api-service/codegen/agent/__init__.py
index 90abb6733..206d98f36 100644
--- a/api-service/codegen/agent/__init__.py
+++ b/api-service/codegen/agent/__init__.py
@@ -1,3 +1 @@
-from codegen.agent.base import CodegenAgent
-
-__all__ = ["CodegenAgent"]
+from codegen.agent.base import CodegenAgent, CodegenAgentExecutor
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.py
index eecde37d0..e0871bf27 100644
--- a/api-service/codegen/agent/base.py
+++ b/api-service/codegen/agent/base.py
@@ -1,12 +1,21 @@
-from typing import Optional, Tuple, List
+from typing import Optional, Tuple, List, Dict, Union
import json
+import xml.etree.ElementTree as ET
+from langchain.agents.tools import InvalidTool
+from langchain.agents import AgentExecutor
from langchain.agents.chat.base import ChatAgent
-from langchain.schema import AgentAction
+from langchain.schema import AgentAction, AgentFinish
+from langchain.tools.base import BaseTool
FINAL_ANSWER_ACTION = "Final Answer:"
+# class ActionRouter(BaseTool):
+# name = "action_router"
+# description = "Use this tool when you want to use multiple actions at once"
+
+
class CodegenAgent(ChatAgent):
def _construct_scratchpad(
self, intermediate_steps: List[Tuple[AgentAction, str]]
@@ -43,8 +52,15 @@ class CodegenAgent(ChatAgent):
# TODO: Here we can change the JSON formated `action` + `action_input` to something
# more suited to our use-case so the model doesn't need to escape the generated code.
_, action, _ = text.split("```")
- response = json.loads(action.strip())
- return response["action"], response["action_input"]
+ root = ET.fromstring(f"<root>{action.strip()}</root>")
+ actions = root.findall("action")
+ if len(actions) > 1:
+ return "action_router", action.strip()
+ else:
+ return actions[0].attrib["tool"], actions[0].text
+
+ # response = json.loads(action.strip())
+ # return response["action"], response["action_input"]
except Exception as e:
# TODO: I think this is buggy. I haven't really had a chance to properly test it and debug the model's behavior.
print(f"====== Got exception '{str(e)}'\n text:\n{text}")
@@ -55,3 +71,98 @@ class CodegenAgent(ChatAgent):
# f"You are not following the response format as I told you. I just ran your response via json.loads and received this error\n{str(e)}\nPlease try again and change your response.",
)
# raise ValueError(f"Could not parse LLM output: {text}")
+
+
+class CodegenAgentExecutor(AgentExecutor):
+ def _run_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 = tool.run(
+ tool_input,
+ verbose=self.verbose,
+ color=color,
+ llm_prefix=llm_prefix,
+ observation_prefix=self.agent.observation_prefix,
+ )
+ else:
+ observation = InvalidTool().run(
+ tool_name,
+ verbose=self.verbose,
+ color=None,
+ llm_prefix="",
+ observation_prefix=self.agent.observation_prefix,
+ )
+ return observation
+
+ def _take_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 = self.agent.plan(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.
+ 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"
+ )
+
+ #
+ if output.tool == "action_router":
+ observation = ""
+ # The `output.tool_input` is a XML tree with multiple actions
+ # Go through each action and run it.
+ # Collect outputs from each action.
+ root = ET.fromstring(f"<root>{output.tool_input.strip()}</root>")
+ actions = root.findall("action")
+ for action in actions:
+ tool_name = action.attrib["tool"]
+ tool_input = action.text
+ observation = self._run_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 = self._run_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
diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.py
index a0fe3f4d3..aa15bc53e 100644
--- a/api-service/codegen/codegen.py
+++ b/api-service/codegen/codegen.py
@@ -25,7 +25,7 @@ from langchain.tools import BaseTool
# from codegen.tools.playground import create_playground_tools
from codegen.env import EnvVar
-from codegen.agent import CodegenAgent
+from codegen.agent import CodegenAgent, CodegenAgentExecutor
from codegen.prompt import (
PREFIX,
SUFFIX,
@@ -35,17 +35,6 @@ from codegen.prompt import (
)
-class InvalidTool(BaseTool):
- name = "InvalidTool"
- description = "Do not use this tool! It is for human system admin only!"
-
- def _run(self, err: str) -> str:
- return err
-
- async def _arun(self, err: str) -> str:
- return err
-
-
class OutputFinalCode(BaseTool):
name = "OutputFinalCode"
description = "This is the last tool you would use. You use it when you know the final server code and you want to output it. The input should be the final server code that does what the user required."
@@ -119,7 +108,7 @@ class DeployCode(BaseTool):
# """
class Codegen(BaseModel):
agent: Optional[CodegenAgent]
- agent_executor: Optional[AgentExecutor]
+ agent_executor: Optional[CodegenAgentExecutor]
llm = ChatOpenAI(
streaming=True,
temperature=0,
@@ -151,7 +140,7 @@ class Codegen(BaseModel):
format_instructions=FORMAT_INSTRUCTIONS,
input_variables=c.input_variables,
)
- c.agent_executor = AgentExecutor.from_agent_and_tools(
+ c.agent_executor = CodegenAgentExecutor.from_agent_and_tools(
agent=c.agent,
tools=[
*playground_tools,
diff --git a/api-service/codegen/prompt.py b/api-service/codegen/prompt.py
index 1c79beceb..4aea32834 100644
--- a/api-service/codegen/prompt.py
+++ b/api-service/codegen/prompt.py
@@ -12,36 +12,33 @@ PREFIX = """You are an AI JavaScript/Nodejs assistant.
# "action_input": $INPUT
# }}}}
FORMAT_INSTRUCTIONS = """"The way you use the tools is by specifying a XML snippet.
-Specifically, this XML snippet should have a `<Action>$TOOL_NAME</Action>` element (with the name of the tool to use inside) and a `<ActionInput>$INPUT</ActionInput>` element (with the input to the tool going here).
-The XML code snippet should only contain a SINGLE action, do NOT use multiple actions at once. Here is an example of a valid XML code snippet:
+Specifically, this XML snippet MUST have a `<action tool="$TOOL_NAME">$INPUT</action>` element (with the name of the tool in the `tool` attribute and input for the tool inside the XML tag).
+The XML snippect CANNOT HAVE MORE THAN ONE ACTION. Do NOT use multiple actions at once. Here is an example of a valid XML code snippet:
```
-<Action>
-$TOOL_NAME
-</Action>
-<ActionInput>
+<action tool="$TOOL_NAME">
$INPUT
-</ActionInput>
+</action>
```
-ALWAYS use the following format:
+ALWAYS use the following format and NEVER specify more than ONE ACTION AT THE TIME:
Instructions: the input instructions you must implement
Thought: you should always think about what to do
Action:
```
-<Action>
-$TOOL_NAME
-</Action>
-<ActionInput>
+<action tool="$TOOL_NAME">
$INPUT
-</ActionInput>
+</action>
```
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: I now know the final server code and can show it.
Final Answer: the final server code"""
-SUFFIX = """Begin! Reminder to NEVER use tools you don't have access to and ALWAYS use the exact the action `OutputFinalCode` when you know the final server code."""
+SUFFIX = """Begin! Reminder to NEVER use tools you don't have access to and ALWAYS use the exact the action `Final Answer` when you know the final server code.
+Thought: Here is the plan of what I will do based on the instructions I got:
+1.
+"""
HUMAN_INSTRUCTIONS_PREFIX = [
{
@@ -72,6 +69,7 @@ app.listen(port, () => {{
]
HUMAN_INSTRUCTIONS_SUFFIX = [
+ "Always use run the code before you write it to a file or deploy it",
"Generate the full required server code and make sure it starts without any errors.",
"Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server.",
"Once all works without any bugs and errors, write the code to the file.",
diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.py
index 8e805e1ec..eec3c43d7 100644
--- a/api-service/codegen/tools/playground/tools/code.py
+++ b/api-service/codegen/tools/playground/tools/code.py
@@ -25,7 +25,7 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):
yield install_dependencies
- last_javascript_code: str | None
+ last_javascript_code: str | None = None
# This tool is just for executing JavaScript without doing the request to server
@tool("RunJavaScriptCode")
@@ -55,8 +55,9 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):
Make a request to check if the previously run JavaScript code is a server that can handle the needed request.
Use this tool before returning the final answer.
"""
+ nonlocal last_javascript_code
if last_javascript_code is None:
- return "No JavaScript code was previously run"
+ return "Cannot curl, you need to run code first"
mock_request_cmd = mock.terminal_command()
diff --git a/api-service/codegen/tools/playground/tools/process.py b/api-service/codegen/tools/playground/tools/process.py
index b31c2491c..ce1e28a02 100644
--- a/api-service/codegen/tools/playground/tools/process.py
+++ b/api-service/codegen/tools/playground/tools/process.py
@@ -7,7 +7,9 @@ from playground_client.models.process_response import ProcessResponse
def encode_command_output(response: ProcessResponse, only_errors: bool = False) -> str:
result = ""
- err_lines = [err.line for err in response.stderr]
+ err_lines = [err.line for err in response.stderr if "Error:" in err.line]
+ # err_lines = [err.line for err in response.stderr]
+
errors = "\n".join(err_lines)
if len(errors) > 0: