Skip to content

armgit / E2B

Public Imported from GitHub
0

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

commit

Vasek Mlejnsky871e47980f49
diff --git a/api-service/codegen/__init__.py b/api-service/codegen/__init__.py
index 388180603..e69de29bb 100644
--- a/api-service/codegen/__init__.py
+++ b/api-service/codegen/__init__.py
@@ -1 +0,0 @@
-from .codegen import generate_req_handler
diff --git a/api-service/codegen/agent/__init__.py b/api-service/codegen/agent/__init__.py
new file mode 100644
index 000000000..90abb6733
--- /dev/null
+++ b/api-service/codegen/agent/__init__.py
@@ -0,0 +1,3 @@
+from codegen.agent.base import CodegenAgent
+
+__all__ = ["CodegenAgent"]
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.py
new file mode 100644
index 000000000..f4919b157
--- /dev/null
+++ b/api-service/codegen/agent/base.py
@@ -0,0 +1,58 @@
+from typing import Optional, Tuple, List
+import json
+
+from langchain.agents.chat.base import ChatAgent
+from langchain.schema import AgentAction
+
+FINAL_ANSWER_ACTION = "Final Answer:"
+
+
+class CodegenAgent(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()
+ _, action, _ = text.split("```")
+ try:
+ # 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.
+ 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)}' when parsing json:\n{action.strip()}"
+ )
+ # input = response["action_input"]
+ return (
+ "InvalidTool",
+ f"I just ran your response via json.loads and received this error\n{str(e)}\nPlease try again",
+ )
+ # raise ValueError(f"Could not parse LLM output: {text}")
diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.py
index 26ac72db4..40986285d 100644
--- a/api-service/codegen/codegen.py
+++ b/api-service/codegen/codegen.py
@@ -1,5 +1,11 @@
-from typing import List, Dict
+from typing import List, Tuple, List, Any, Dict
+
+from pydantic import BaseModel
from langchain.llms.openai import OpenAIChat, OpenAI
+from langchain.chat_models import ChatOpenAI
+from langchain.callbacks.base import CallbackManager
+from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
+from langchain.agents import AgentExecutor
# from codegen.tools.documentation import ReadDocumentation
from codegen.env import EnvVar
@@ -8,6 +14,72 @@ from codegen.prompt import PREFIX, SUFFIX, FORMAT_INSTRUCTIONS
from codegen.tools.playground import create_playground_tools
from database import Database
+from codegen.tools.playground import create_playground_tools
+from codegen.agent import CodegenAgent
+from codegen.prompt import PREFIX, SUFFIX, FORMAT_INSTRUCTIONS
+
+
+class Codegen(BaseModel):
+ agent: CodegenAgent
+ agent_executor: AgentExecutor
+ llm = ChatOpenAI(
+ streaming=True,
+ temperature=0,
+ max_tokens=2056,
+ verbose=True,
+ callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
+ )
+ input_variables = ["input", "agent_scratchpad", "method"]
+ tools = []
+
+ @classmethod
+ def from_playground_tools(cls, playground_tools: Tuple[List[Any]]):
+ c = cls()
+ print(c.input_variables)
+ print(c.tools)
+
+ # Create prompt
+ prompt = CodegenAgent.create_prompt(
+ tools=[
+ *playground_tools,
+ *c.tools,
+ ],
+ prefix=PREFIX,
+ suffix=SUFFIX,
+ format_instructions=FORMAT_INSTRUCTIONS,
+ input_variables=c.input_variables,
+ )
+
+ # Create CodegenAgent and its executor
+ c.agent = CodegenAgent.from_llm_and_tools(
+ llm=c.llm,
+ tools=[
+ *playground_tools,
+ *c.tools,
+ ],
+ prefix=PREFIX,
+ suffix=SUFFIX,
+ format_instructions=FORMAT_INSTRUCTIONS,
+ input_variables=c.input_variables,
+ )
+ c.agent_executor = AgentExecutor.from_agent_and_tools(
+ agent=c.agent,
+ tools=[
+ *playground_tools,
+ *c.tools,
+ ],
+ verbose=True,
+ )
+
+ return c
+
+ def generate():
+ # TODO
+ pass
+
+
+cg = Codegen()
+
def generate_req_handler(
db: Database,
diff --git a/api-service/codegen/prompt.py b/api-service/codegen/prompt.py
index aa1fcc31d..ed09fdcb3 100644
--- a/api-service/codegen/prompt.py
+++ b/api-service/codegen/prompt.py
@@ -1,110 +1,14 @@
-# PREFIX = """Complete a body of nodejs function that handles incoming {method} requests inside an Express server.
-
-# You must follow these rules:
-# - Don't write anything else but the body of function. Once you have the body function written, you should finish and output the code you have so far.
-# - You have access to the following environment variables and you can use them if needed: SUPABASE_URL, SUPABASE_KEY.
-
-# If you cannot follow any of the rules above, finish with the last code you wrote. Don't try in any way to go around the rules or come up with other solutions.
-
-
-# The code must perform following logic:
-# """
-
-PREFIX = """You are a skilled programmer that knows nodejs and javascript.
-USE ONLY TOOLS THAT YOU HAVE ACCESS TO!
-You are building a serverless API with Cloudflare Workers using the `itty-router` package. The javascript code is in the form of ES modules.
-You need to complete the `handle{method}Request` function that handles the incoming {method} requests and return the whole completed code.
-
-You can install any package that you might need.
-
-The incoming request payload looks like this:
-```
-{request_body}
-```
-
-Here is the starting code that handles the initialization. The function that you need to complete is marked with a TODO comment:
-```
-import {{ Router }} from 'itty-router'
-
-const router = Router()
-
-async function handle{method}Request(req, env) {{
- // DO NOT USE `process.env` TO ACCES ENVIRONMENT VARIABLES. Instead, you access environment variables through the `env` parameter like this: `const myEnv = env.MY_ENV`.
- // You have access to the following environment variables:
- {envs}
-
- // The incoming request content-type is application/json. Make sure to correctly retrieve it.
- const requestBody = await req.json()
- // TODO
-}}
-
-router.{method}('/', handle{method}Request)
-router.get('*', () => new Response('Not found', {{ status: 404 }}))
-
-export default {{
- fetch: router.handle
-}}
-```
-
-The code must perform the following logic:
-"""
-
-# SUFFIX = """
-# Here's the code that must be completed:
-# ```
-# const express = require('express');
-# const app = express();
-
-# app.use(express.json());
-
-# app.{method}(/, function(req, res) {{
-# // TODO: Implement body of this function based on the required logic
-# }})
-
-# app.listen(8080, () => console.log('Listening on port 8080'));
-# ```
-# """
-
-# SUFFIX = """Here is already written code that handles the initialization. You don't need to rewrite that.
-# The part that you need to complete is marked with a TODO comment:
-# ```
-# import {{ Router }} from 'itty-router'
-
-# const router = Router()
-
-# function handleRequest(request) {{
-# router
-# .get('/', async function(req) {{
-# // TODO
-# })
-# .get('*', () => new Response("Not found", {{ status: 404 }}))
-# router.handle(request)
-# }
-
-# addEventListener('fetch', (event) => {{
-# event.respondWith(handleRequest(event.request))
-# }})
-# ```
-# """
-
-SUFFIX = ""
-
-################## NEW FOR CHAT GPT
-
-PREFIX = """You are a senior JavaScript developer and you have access to the following tools: {tool_names}.
-You've been tasked to build the `handle{method}Request` function that handles incoming {method} requests for your REST API.
-The incoming {method} request payload ALWAYS looks like this:
-```
-{{{{
- {request_body}
-}}}}
-```
-
-The function `handle{method}Request` MUST do the following logic: {steps}"""
-
-FORMAT_INSTRUCTIONS = """The way you use the tools is by specifying a json blob.
+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:"""
+
+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).
-The only values that should be in the "action" field are: {tool_names}
+THE ONLY VALUES THAT SHOULD BE IN THE "action" FIELD ARE: {tool_names}. NO OTHER VALUES ARE ALLOWED.
The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:
```
{{{{
@@ -113,87 +17,20 @@ The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of mult
}}}}
```
ALWAYS use the following format:
-Requirement: the required logic from the code
-Thought: you should always think about what to do
-Action:
-```
-$JSON_BLOB
-```
-Observation: the result of the action
-... (this Thought/Action/Observation can repeat N times)
-Thought: I now know the final answer
-Final Answer: the final answer to the original input question"""
-
-##########################
-
-PREFIX = """You are a senior JavaScript developer and you have access to the following tools: {tool_names}.
-You're buildiing a serverless API with Cloudflare Workers using the `itty-router` package. You've been tasked to build the `handlepostRequest` function that handles incoming POST requests for your REST API.
-The incoming POST request payload ALWAYS looks like this:
-```
-{{
- email: string,
- userID: string,
- pricingType: string,
- pricingCost: number
-}}
-```
-
-The code MUST do the following logic:
-1. Use the following starting template. The part that must be completed is marked with the TODO comment
-```
-import {{ Router }} from 'itty-router'
-
-const router = Router()
-async function handlepostRequest(req, env) {{
- // DO NOT USE `process.env` TO ACCES ENVIRONMENT VARIABLES. Instead, you access environment variables through the `env` parameter like this: `const myEnv = env.MY_ENV`.
- // You have access to the following environment variables:
- // - env.SLACK_BOT_ACCESS_TOKEN
-
- // The incoming request content-type is application/json. Make sure to correctly retrieve it.
- const requestBody = await req.json()
- // TODO
-}}
-
-router.post('/', handlepostRequest)
-router.get('*', () => new Response('Not found', {{ status: 404 }}))
-
-export default {{
- fetch: router.handle
-}}
-```
-2. Use the `@sagi.io/workers-slack` package to send a message to channel 'general' in the format: `User '<email>' selected pricing '<type>' for $<cost>''` based on the incoming request data. This is an example of how to send a Slack message using the `@sagi.io/workers-slack` message to channel 'general':
-```
-const botAccessToken = env.SLACK_BOT_ACCESS_TOKEN;
-const SlackREST = require('@sagi.io/workers-slack')
-const SlackAPI = new SlackREST({{ botAccessToken }})
-
-const data = {{ channel: 'general', text: 'hello world' }}
-const result = await SlackAPI.chat.postMessage(data)
-```
-
-3. Respond back with the message "Ok" if there wasn't any error
-"""
-
-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).
-The only values that should be in the "action" field are: {tool_names}
-The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:
-```
-{{
- "action": $TOOL_NAME,
- "action_input": $INPUT
-}}
-```
-ALWAYS use the following format:
-Requirement: the required logic from the code
+Instructions: the input instructions you must implement
Thought: you should always think about what to do
Action:
```
-$JSON_BLOB
+{{{{
+ "action": $TOOL_NAME,
+ "action_input": $INPUT
+}}}}
```
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: I now know the final answer
-Final Answer: the final answer to the original input question"""
+Final Answer: the final code that satisfies all the input instructions. THE FINAL ANSWER IT MUST BE JUST THE CODE."""
+
+SUFFIX = """Begin! Reminder to NEVER use tools you don't have access to and ALWAYS use the exact characters `Final Answer` when responding."""
diff --git a/api-service/codegen/prompt_old.py b/api-service/codegen/prompt_old.py
new file mode 100644
index 000000000..aa1fcc31d
--- /dev/null
+++ b/api-service/codegen/prompt_old.py
@@ -0,0 +1,199 @@
+# PREFIX = """Complete a body of nodejs function that handles incoming {method} requests inside an Express server.
+
+# You must follow these rules:
+# - Don't write anything else but the body of function. Once you have the body function written, you should finish and output the code you have so far.
+# - You have access to the following environment variables and you can use them if needed: SUPABASE_URL, SUPABASE_KEY.
+
+# If you cannot follow any of the rules above, finish with the last code you wrote. Don't try in any way to go around the rules or come up with other solutions.
+
+
+# The code must perform following logic:
+# """
+
+PREFIX = """You are a skilled programmer that knows nodejs and javascript.
+USE ONLY TOOLS THAT YOU HAVE ACCESS TO!
+You are building a serverless API with Cloudflare Workers using the `itty-router` package. The javascript code is in the form of ES modules.
+You need to complete the `handle{method}Request` function that handles the incoming {method} requests and return the whole completed code.
+
+You can install any package that you might need.
+
+The incoming request payload looks like this:
+```
+{request_body}
+```
+
+Here is the starting code that handles the initialization. The function that you need to complete is marked with a TODO comment:
+```
+import {{ Router }} from 'itty-router'
+
+const router = Router()
+
+async function handle{method}Request(req, env) {{
+ // DO NOT USE `process.env` TO ACCES ENVIRONMENT VARIABLES. Instead, you access environment variables through the `env` parameter like this: `const myEnv = env.MY_ENV`.
+ // You have access to the following environment variables:
+ {envs}
+
+ // The incoming request content-type is application/json. Make sure to correctly retrieve it.
+ const requestBody = await req.json()
+ // TODO
+}}
+
+router.{method}('/', handle{method}Request)
+router.get('*', () => new Response('Not found', {{ status: 404 }}))
+
+export default {{
+ fetch: router.handle
+}}
+```
+
+The code must perform the following logic:
+"""
+
+# SUFFIX = """
+# Here's the code that must be completed:
+# ```
+# const express = require('express');
+# const app = express();
+
+# app.use(express.json());
+
+# app.{method}(/, function(req, res) {{
+# // TODO: Implement body of this function based on the required logic
+# }})
+
+# app.listen(8080, () => console.log('Listening on port 8080'));
+# ```
+# """
+
+# SUFFIX = """Here is already written code that handles the initialization. You don't need to rewrite that.
+# The part that you need to complete is marked with a TODO comment:
+# ```
+# import {{ Router }} from 'itty-router'
+
+# const router = Router()
+
+# function handleRequest(request) {{
+# router
+# .get('/', async function(req) {{
+# // TODO
+# })
+# .get('*', () => new Response("Not found", {{ status: 404 }}))
+# router.handle(request)
+# }
+
+# addEventListener('fetch', (event) => {{
+# event.respondWith(handleRequest(event.request))
+# }})
+# ```
+# """
+
+SUFFIX = ""
+
+################## NEW FOR CHAT GPT
+
+PREFIX = """You are a senior JavaScript developer and you have access to the following tools: {tool_names}.
+You've been tasked to build the `handle{method}Request` function that handles incoming {method} requests for your REST API.
+The incoming {method} request payload ALWAYS looks like this:
+```
+{{{{
+ {request_body}
+}}}}
+```
+
+The function `handle{method}Request` MUST do the following logic: {steps}"""
+
+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).
+The only values that should be in the "action" field are: {tool_names}
+The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:
+```
+{{{{
+ "action": $TOOL_NAME,
+ "action_input": $INPUT
+}}}}
+```
+ALWAYS use the following format:
+Requirement: the required logic from the code
+Thought: you should always think about what to do
+Action:
+```
+$JSON_BLOB
+```
+Observation: the result of the action
+... (this Thought/Action/Observation can repeat N times)
+Thought: I now know the final answer
+Final Answer: the final answer to the original input question"""
+
+##########################
+
+PREFIX = """You are a senior JavaScript developer and you have access to the following tools: {tool_names}.
+You're buildiing a serverless API with Cloudflare Workers using the `itty-router` package. You've been tasked to build the `handlepostRequest` function that handles incoming POST requests for your REST API.
+The incoming POST request payload ALWAYS looks like this:
+```
+{{
+ email: string,
+ userID: string,
+ pricingType: string,
+ pricingCost: number
+}}
+```
+
+The code MUST do the following logic:
+1. Use the following starting template. The part that must be completed is marked with the TODO comment
+```
+import {{ Router }} from 'itty-router'
+
+const router = Router()
+
+async function handlepostRequest(req, env) {{
+ // DO NOT USE `process.env` TO ACCES ENVIRONMENT VARIABLES. Instead, you access environment variables through the `env` parameter like this: `const myEnv = env.MY_ENV`.
+ // You have access to the following environment variables:
+ // - env.SLACK_BOT_ACCESS_TOKEN
+
+ // The incoming request content-type is application/json. Make sure to correctly retrieve it.
+ const requestBody = await req.json()
+ // TODO
+}}
+
+router.post('/', handlepostRequest)
+router.get('*', () => new Response('Not found', {{ status: 404 }}))
+
+export default {{
+ fetch: router.handle
+}}
+```
+
+2. Use the `@sagi.io/workers-slack` package to send a message to channel 'general' in the format: `User '<email>' selected pricing '<type>' for $<cost>''` based on the incoming request data. This is an example of how to send a Slack message using the `@sagi.io/workers-slack` message to channel 'general':
+```
+const botAccessToken = env.SLACK_BOT_ACCESS_TOKEN;
+const SlackREST = require('@sagi.io/workers-slack')
+const SlackAPI = new SlackREST({{ botAccessToken }})
+
+const data = {{ channel: 'general', text: 'hello world' }}
+const result = await SlackAPI.chat.postMessage(data)
+```
+
+3. Respond back with the message "Ok" if there wasn't any error
+"""
+
+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).
+The only values that should be in the "action" field are: {tool_names}
+The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:
+```
+{{
+ "action": $TOOL_NAME,
+ "action_input": $INPUT
+}}
+```
+ALWAYS use the following format:
+Requirement: the required logic from the code
+Thought: you should always think about what to do
+Action:
+```
+$JSON_BLOB
+```
+Observation: the result of the action
+... (this Thought/Action/Observation can repeat N times)
+Thought: I now know the final answer
+Final Answer: the final answer to the original input question"""
diff --git a/api-service/codegen/tools/playground/main.py b/api-service/codegen/tools/playground/main.py
index fb72e0030..f339cb84e 100644
--- a/api-service/codegen/tools/playground/main.py
+++ b/api-service/codegen/tools/playground/main.py
@@ -1,4 +1,4 @@
-from typing import List, Any
+from typing import List, Any, Tuple
from codegen.env import EnvVar
from codegen.tools.playground.mock.request import MockRequestFactory
@@ -14,7 +14,7 @@ def create_playground_tools(
method: str,
route: str,
request_body_template: str | None,
-) -> tuple[List[Any], NodeJSPlayground]:
+) -> Tuple[List[Any], NodeJSPlayground]:
playground = NodeJSPlayground(envs)
mock = MockRequestFactory(
method=method,