Skip to content

armgit / E2B

Public Imported from GitHub
0

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

commit

Vasek Mlejnsky2f0b14dbe49a
diff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.py
index 37b91a55d..afb0646e9 100644
--- a/api-service/codegen/tools/playground/playground.py
+++ b/api-service/codegen/tools/playground/playground.py
@@ -193,7 +193,8 @@ class NodeJSPlayground(Playground):
self.env_vars = self.format_env_vars(envs)
def run_javascript_code(self, code: str):
- print(f"Running javascript code: {code}")
+ # print(f"Running javascript code: {code}")
+ print(f"Running javascript code...")
self.write_file(self.default_javascript_code_file, code)
result = self.run_command(
f"node {self.default_javascript_code_file}",
@@ -216,7 +217,7 @@ class NodeJSPlayground(Playground):
request_cmd: str,
port: float,
):
- print(f"Running curl {request_cmd} for code: {code}")
+ print(f"Running '{request_cmd}' for code:\n{code}")
self.write_file(self.default_javascript_code_file, code)
result = self.run_server_with_request(
server_cmd=f"node {self.default_javascript_code_file}",
diff --git a/api-service/test.py b/api-service/test.py
index 189aa4830..13d1880e2 100644
--- a/api-service/test.py
+++ b/api-service/test.py
@@ -1,5 +1,6 @@
-from typing import Optional, Tuple
+from typing import Optional, Tuple, List
import json
+import subprocess
from langchain import LLMChain
from langchain.tools import BaseTool
@@ -13,14 +14,15 @@ from langchain.prompts.chat import (
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
-from langchain.schema import HumanMessage
+from langchain.schema import AgentAction, HumanMessage
+from langchain.memory import ConversationBufferWindowMemory
from codegen.tools.playground import create_playground_tools
class InvalidTool(BaseTool):
name = "InvalidTool"
- description = "indicates that the last selected tool was an invalid tool"
+ description = "Indicates that the last selected tool was an invalid tool"
def _run(self, err: str) -> str:
return err
@@ -29,10 +31,84 @@ class InvalidTool(BaseTool):
return err
+class WriteCodeToFile(BaseTool):
+ name = "WriteCodeToFile"
+ description = """Writes code to the index.js file. The input should be the code to be written."""
+
+ def _run(self, code: str) -> str:
+ print(f"Writing code to file: \n{code}")
+ with open(
+ "/Users/vasekmlejnsky/Developer/nodejs-express-server/index.js", "w"
+ ) as f:
+ f.write(code)
+ return "wrote code to index.js"
+
+ async def _arun(self, err: str) -> str:
+ return NotImplementedError("WriteCodeToFile does not support async")
+
+
+class DeployCode(BaseTool):
+ name = "DeployCode"
+ description = """Deploys the code."""
+
+ def _run(self, empty: str) -> str:
+ print("Deploying...")
+ p = subprocess.Popen(
+ ["git", "add", "."],
+ cwd="/Users/vasekmlejnsky/Developer/nodejs-express-server",
+ )
+ p.wait()
+ p = subprocess.Popen(
+ ["git", "commit", "-m", "Deploy"],
+ cwd="/Users/vasekmlejnsky/Developer/nodejs-express-server",
+ )
+ p.wait()
+ p = subprocess.Popen(
+ [
+ "git",
+ "push",
+ ],
+ cwd="/Users/vasekmlejnsky/Developer/nodejs-express-server",
+ )
+ p.wait()
+ return f"deployed server"
+
+ async def _arun(self, empty: str) -> str:
+ return NotImplementedError("DeployCode does not support async")
+
+
FINAL_ANSWER_ACTION = "Final Answer:"
class CustomChatAgent(ChatAgent):
+ def _construct_scratchpad(
+ self, intermediate_steps: List[Tuple[AgentAction, str]]
+ ) -> str:
+ # print("CONSTRUCTING SCRATCHPAD before:")
+ # print(len(intermediate_steps))
+
+ # Leave only the latest element in the `intermediate_steps` list
+ # if len(intermediate_steps) > 4:
+ # intermediate_steps = intermediate_steps[len(intermediate_steps) - 2 :]
+ # print(intermediate_steps)
+
+ # print("CONSTRUCTING SCRATCHPAD after:")
+ # print(len(intermediate_steps))
+
+ agent_scratchpad = super()._construct_scratchpad(intermediate_steps)
+ # print("=======================SCRATCHPAD:", agent_scratchpad)
+ # print("=======================")
+ if not isinstance(agent_scratchpad, str):
+ raise ValueError("agent_scratchpad should be of type string.")
+ if agent_scratchpad:
+ return (
+ f"This was your previous work "
+ f"(but I haven't seen any of it! I only see what "
+ f"you return as final answer):\n{agent_scratchpad}"
+ )
+ else:
+ return agent_scratchpad
+
def _extract_tool_and_input(self, text: str) -> Optional[Tuple[str, str]]:
if FINAL_ANSWER_ACTION in text:
return "Final Answer", text.split(FINAL_ANSWER_ACTION)[-1].strip()
@@ -41,6 +117,9 @@ class CustomChatAgent(ChatAgent):
response = json.loads(action.strip())
return response["action"], response["action_input"]
except Exception as e:
+ print(
+ f"====== Got exception '{str(e)}' when parsing json:\n{action.strip()}"
+ )
# input = response["action_input"]
return (
"InvalidTool",
@@ -62,33 +141,32 @@ FORMAT_INSTRUCTIONS = """"""
##########
# - Think step-by-step. Use each step to describe your plan on how you will go about implementing the required instructions.
-PREFIX = """You are an AI JavaScript/Nodejs assistant.
-- Follow the user's instructions carefully & to the letter.
-- Minimize any other prose.
-- You are building an Express server that handles your REST API and you are required to complete the code based on the provided instructions.
-- You are working with an ES module so don't use `require` use `import` instead.
-- Start with the following code snippet that starts the server
-```
-import express from 'express';
-const app = express();
+# - Start with the following code snippet that starts the server
+# ```
+# import express from 'express';
+# const app = express();
-// All incoming requests have payload in the JSON format.
-app.use(express.json());
+# // All incoming requests have payload in the JSON format.
+# app.use(express.json());
-// The function we need to implement that must handle the {method} requests.
-// We can make the function async if needed.
-function handle{method}Request(req, res) {{
- // TODO: Complete the function based on the instructions
-}}
+# // The function we need to implement that must handle the {method} requests.
+# // We can make the function async if needed.
+# function handle{method}Request(req, res) {{
+# // TODO: Complete the function based on the instructions
+# }}
-app.{method}('/', handle{method}Request);
+# app.{method}('/', handle{method}Request);
-// Start the server
-app.listen(3000, () => console.log('Listening on port 3000'));
-```
+# // Start the server
+# app.listen(3000, () => console.log('Listening on port 3000'));
+# ```
+PREFIX = """You are an AI JavaScript/Nodejs assistant.
+- Follow the user's instructions carefully & to the letter:
+- Minimize any other prose.
+- You are building an Express server that handles REST API.
+- ALWAYS use `import` to import packages.
- Make sure any code you generate is JSON escaped
-- You have access to the following tools:
-"""
+- You have access to the following tools:"""
FORMAT_INSTRUCTIONS = """"The way you use the tools is by specifying a json blob.
Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).
@@ -150,12 +228,16 @@ input_variables = [
prompt = CustomChatAgent.create_prompt(
tools=[
*playground_tools,
+ WriteCodeToFile(),
+ DeployCode(),
],
prefix=PREFIX,
suffix=SUFFIX,
format_instructions=FORMAT_INSTRUCTIONS,
input_variables=input_variables,
)
+# print(prompt.messages[0].prompt.template)
+# exit()
invalid_tool = InvalidTool()
@@ -166,28 +248,55 @@ agent = CustomChatAgent.from_llm_and_tools(
tools=[
*playground_tools,
invalid_tool,
+ WriteCodeToFile(),
+ DeployCode(),
],
prefix=PREFIX,
suffix=SUFFIX,
format_instructions=FORMAT_INSTRUCTIONS,
input_variables=input_variables,
- verbose=True,
+ # verbose=True,
+ # memory=ConversationBufferWindowMemory(k=2),
)
ae = AgentExecutor.from_agent_and_tools(
- agent=agent, tools=[*playground_tools, invalid_tool]
+ agent=agent,
+ tools=[
+ *playground_tools,
+ invalid_tool,
+ WriteCodeToFile(),
+ DeployCode(),
+ ],
+ # memory=ConversationBufferWindowMemory(k=2),
+ verbose=True,
)
+
+# 2. Retrieve email from the request payload and check if it's a valid email. Respond with 'Ok' if it is, otherwise respond with the adequate error.
+# 5. Once all works without any bugs and errors, write the code to the index.js file.""",
+# input="""Here are the instructions:
+# 1. Extract `email` from the incoming POST request.
+# 2. If there's no email, respond back with an error.
+# 3. Otherwise, respond back with the part of the email before the '@' sign.
+# 3. Generate the full required server code and make sure it starts without any errors.
+# 4. Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server.
+# 5. Once all works without any bugs and errors, write the code to the file and deploy it.""",
+
+user_input = """1. Extract `email` from the incoming POST request.
+2. If there's no email, respond back with an error.
+3. Otherwise, respond back with the part of the email before the '@' sign.
+4. Generate the full required server code and make sure it starts without any errors.
+5. Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server.
+6. Once all works without any bugs and errors, write the code to the file.
+7. Deploy the code.
+"""
+print(f"Instructions:\n{user_input}")
+
try:
# Run
ae.run(
# "1. Check if the incoming request is POST request. If not, respond with an adequate error",
agent_scratchpad="",
- input="""Here are the instructions:
-1. Check if the incoming request is POST request. If not, respond with an adequate error.
-2. Retrieve email from the request payload and check if it's a valid email. Respond with 'Ok' if it is, otherwise respond with the adequate error.
-3. Generate the full required server code and make sure it starts without any errors.
-4. 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, provide the final answer""",
+ input=f"Here are the instructions:\n{user_input}",
method="post",
# instructions="""// 1. Check if the incoming request is POST request. If not, respond with an adequate error.
# // 2. Retrieve email from the request payload and check if it's a valid email. Respond with 'Ok' if it is, otherwise respond with the adequate error.
@@ -195,14 +304,15 @@ Once all works without any bugs and errors, provide the final answer""",
)
except ValueError as e:
- if "Could not parse LLM output" in str(e):
- # Here we want to remind LLM that it's not properly formatting its output.
- # https://twitter.com/zachtratar/status/1633376345995739136
- # eg:
- # I just ran your response via json.loads and received this error:
- # {error}
- # Please try again
- print("FAILED TO PARSE LLM")
+ raise e
+# if "Could not parse LLM output" in str(e):
+# # Here we want to remind LLM that it's not properly formatting its output.
+# # https://twitter.com/zachtratar/status/1633376345995739136
+# # eg:
+# # I just ran your response via json.loads and received this error:
+# # {error}
+# # Please try again
+# print("FAILED TO PARSE LLM")
# agent_exec = initialize_agent(
# tools=playground_tools,