Skip to content

armgit / E2B

Public Imported from GitHub
0

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

commit

Vasek Mlejnskyfa04ceb89b34
diff --git a/api-service/codegen/callback_handler.py b/api-service/codegen/callback_handler.py
index 6320a8d1d..66f447fce 100644
--- a/api-service/codegen/callback_handler.py
+++ b/api-service/codegen/callback_handler.py
@@ -1,8 +1,6 @@
from typing import Dict, Any, List, Union, Optional
-
-# import sys
+import datetime
import uuid
-from pprint import pprint
from pydantic import PrivateAttr
from langchain.callbacks.base import BaseCallbackHandler
@@ -23,19 +21,54 @@ from database import Database
# UNDERLINE = "\033[4m"
+from threading import Timer
+
+
+class RepeatedTimer(object):
+ def __init__(self, interval, function, *args, **kwargs):
+ self._timer = None
+ self.interval = interval
+ self.function = function
+ self.args = args
+ self.kwargs = kwargs
+ self.is_running = False
+ self.start()
+
+ def _run(self):
+ self.is_running = False
+ self.start()
+ self.function(*self.args, **self.kwargs)
+
+ def start(self):
+ if not self.is_running:
+ self._timer = Timer(self.interval, self._run)
+ self._timer.start()
+ self.is_running = True
+
+ def stop(self):
+ self._timer.cancel()
+ self.is_running = False
+
+
class CustomCallbackHandler(BaseCallbackHandler):
- _file = open(
- "/Users/vasekmlejnsky/Developer/ai-api/api-service/codegen/out.txt", "a"
- )
_logs: List[Dict[str, str]] = []
_current_action_id: Optional[str] = None
_database: Database = PrivateAttr()
_run_id: str = PrivateAttr()
+ _raw_logs: str = ""
+
+ _rt: RepeatedTimer = PrivateAttr()
+
def __init__(self, database: Database, run_id: str, **kwargs: Any):
super().__init__(**kwargs)
self._database = database
self._run_id = run_id
+ self._rt = RepeatedTimer(1, self._push_raw_logs)
+
+ def _push_raw_logs(self) -> None:
+ if self._raw_logs:
+ self._database.push_raw_logs(self._run_id, self._raw_logs)
def on_llm_start(
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
@@ -45,7 +78,7 @@ class CustomCallbackHandler(BaseCallbackHandler):
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
"""Run on new LLM token. Only available when streaming is enabled."""
- pass
+ self._raw_logs += token
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
"""Run when LLM ends running."""
@@ -65,8 +98,7 @@ class CustomCallbackHandler(BaseCallbackHandler):
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
"""Run when chain ends running."""
- self._file.close()
- # pass
+ pass
def on_chain_error(
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
@@ -78,41 +110,24 @@ class CustomCallbackHandler(BaseCallbackHandler):
self, serialized: Dict[str, Any], input_str: str, **kwargs: Any
) -> None:
"""Run when tool starts running."""
- self._file.write(
- "\n[+CustomCallbackHandler] TOOL START\n",
- )
- self._file.write("serialized:")
- for k, v in serialized.items():
- self._file.write(f"\t{k}: {str(v)}")
- self._file.write(f"\n{input_str}\n")
- self._file.write("[-CustomCallbackHandler]\n")
- self._file.flush()
+ pass
def on_tool_end(self, output: str, **kwargs: Any) -> None:
"""Run when tool ends running."""
# Update the correct action in the list with the new output
action = next(a for a in self._logs if a["id"] == self._current_action_id)
+ action["finish_at"] = str(datetime.datetime.now())
action["output"] = output
- # TODO: Push logs to the database
self._database.push_logs(
run_id=self._run_id,
logs=self._logs,
)
- self._file.write("\n[+CustomCallbackHandler] TOOL END\n")
- self._file.write(f"\n{output}\n")
- self._file.write("[-CustomCallbackHandler]\n")
- self._file.flush()
-
def on_tool_error(
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
) -> None:
"""Run when tool errors."""
- self._file.write("\n[+CustomCallbackHandler] TOOL ERROR\n")
- self._file.write(f"\n{str(error)}\n")
- self._file.write("[-CustomCallbackHandler]\n")
- self._file.flush()
def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
"""Run on agent action."""
@@ -120,24 +135,19 @@ class CustomCallbackHandler(BaseCallbackHandler):
self._logs.append(
{
"id": self._current_action_id,
- "tool": action.tool,
+ "type": "tool",
+ "name": action.tool,
"input": action.tool_input,
+ "start_at": str(datetime.datetime.now()),
+ "finish_at": "",
"output": "",
}
)
- # TODO: Push logs to the database
self._database.push_logs(
run_id=self._run_id,
logs=self._logs,
)
- self._file.write("\n[+CustomCallbackHandler] AGENT ACTION\n")
- self._file.write(f"{action.tool}\n")
- self._file.write(f"{action.log}\n")
- self._file.write(f"{action.tool_input}\n")
- self._file.write("[-CustomCallbackHandler]\n")
- self._file.flush()
-
def on_text(self, text: str, **kwargs: Any) -> None:
"""Run on arbitrary text."""
pass
diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.py
index 7da126290..c12dc5e0b 100644
--- a/api-service/codegen/codegen.py
+++ b/api-service/codegen/codegen.py
@@ -166,7 +166,7 @@ class Codegen(BaseModel):
):
callback_manager = CallbackManager(
[
- # StreamingStdOutCallbackHandler(),
+ StreamingStdOutCallbackHandler(),
# CustomCallbackHandler(database=database),
]
)
diff --git a/api-service/codegen/human_input.txt b/api-service/codegen/human_input.txt
deleted file mode 100644
index 21cb523e3..000000000
--- a/api-service/codegen/human_input.txt
+++ /dev/null
@@ -1 +0,0 @@
-I just set it, you are good to go
\ No newline at end of file
diff --git a/api-service/codegen/out.txt b/api-service/codegen/out.txt
deleted file mode 100644
index b84804a1d..000000000
--- a/api-service/codegen/out.txt
+++ /dev/null
@@ -1,238 +0,0 @@
-
-[+CustomCallbackHandler] AGENT ACTION
-InstallNPMDependencies
-Thought: Ok, here is what I will do:
-1. Install the required dependencies.
-2. Implement the post handler with the required functionality.
-3. Test the server with a mock `curl` request.
-4. Write the code to the file.
-5. Deploy the code.
-
-Action:
-```
-<action tool="InstallNPMDependencies">
-express
-</action>
-```
-
-
-
-express
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL START
-serialized: name: InstallNPMDependencies description: InstallNPMDependencies(dependencies: str) -> str - Install dependecies with NPM.
- The input should be a list of Node.js dependencies separated by spaces.
-
-express
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL END
-
-All dependencies installed
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] AGENT ACTION
-RunJavaScriptCode
-Now I will implement the post handler with the required functionality.
-Action:
-```
-<action tool="RunJavaScriptCode">
-import express from 'express';
-const app = express();
-const port = 3000;
-
-app.use(express.json())
-
-app.post('/', (req, res) => {
- if (req.body.email) {
- res.send('Ok');
- } else {
- res.status(400).send('Error: email is missing');
- }
-});
-
-app.listen(port, () => {
- console.log(`Server listening on port ${port}`)
-})
-</action>
-```
-
-
-import express from 'express';
-const app = express();
-const port = 3000;
-
-app.use(express.json())
-
-app.post('/', (req, res) => {
- if (req.body.email) {
- res.send('Ok');
- } else {
- res.status(400).send('Error: email is missing');
- }
-});
-
-app.listen(port, () => {
- console.log(`Server listening on port ${port}`)
-})
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL START
-serialized: name: RunJavaScriptCode description: RunJavaScriptCode(code: str) -> str - Run JavaScript code and return errors and output.
- Input should be a valid JavaScript code.
-
-import express from 'express';
-const app = express();
-const port = 3000;
-
-app.use(express.json())
-
-app.post('/', (req, res) => {
- if (req.body.email) {
- res.send('Ok');
- } else {
- res.status(400).send('Error: email is missing');
- }
-});
-
-app.listen(port, () => {
- console.log(`Server listening on port ${port}`)
-})
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL END
-
-Output:
-Server listening on port 3000
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] AGENT ACTION
-CurlJavaScriptServer
-Now I will test the server with a mock `curl` request.
-Action:
-```
-<action tool="CurlJavaScriptServer">
-</action>
-```
-
-
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL START
-serialized: name: CurlJavaScriptServer description: CurlJavaScriptServer(empty: str) -> str - 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.
-
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL END
-
-Curl result:
-Output:
-Ok
-Code execution result:
-Output:
-Server listening on port 3000
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] AGENT ACTION
-WriteCodeToFile
-The server seems to be working as expected. Now I will write the code to the file.
-Action:
-```
-<action tool="WriteCodeToFile">
-import express from 'express';
-const app = express();
-const port = 3000;
-
-app.use(express.json())
-
-app.post('/', (req, res) => {
- if (req.body.email) {
- res.send('Ok');
- } else {
- res.status(400).send('Error: email is missing');
- }
-});
-
-app.listen(port, () => {
- console.log(`Server listening on port ${port}`)
-})
-</action>
-```
-
-
-
-import express from 'express';
-const app = express();
-const port = 3000;
-
-app.use(express.json())
-
-app.post('/', (req, res) => {
- if (req.body.email) {
- res.send('Ok');
- } else {
- res.status(400).send('Error: email is missing');
- }
-});
-
-app.listen(port, () => {
- console.log(`Server listening on port ${port}`)
-})
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL START
-serialized: name: WriteCodeToFile description: Writes code to the index.js file. The input should be the code to be written.
-
-import express from 'express';
-const app = express();
-const port = 3000;
-
-app.use(express.json())
-
-app.post('/', (req, res) => {
- if (req.body.email) {
- res.send('Ok');
- } else {
- res.status(400).send('Error: email is missing');
- }
-});
-
-app.listen(port, () => {
- console.log(`Server listening on port ${port}`)
-})
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL END
-
-wrote code to index.js
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] AGENT ACTION
-DeployCode
-Now I will deploy the code.
-Action:
-```
-<action tool="DeployCode">
-</action>
-```
-
-
-
-
-[-CustomCallbackHandler]
-
-[+CustomCallbackHandler] TOOL START
-serialized: name: DeployCode description: Deploys the code.
-
-
-[-CustomCallbackHandler]
diff --git a/api-service/codegen/prompt.py b/api-service/codegen/prompt.py
index ce31ec9c6..d60222f12 100644
--- a/api-service/codegen/prompt.py
+++ b/api-service/codegen/prompt.py
@@ -37,12 +37,13 @@ Observation: the result of the action
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 `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.
-"""
+SUFFIX = """Begin! Reminder to NEVER use tools you don't have access. Reminder to ALWAYS use the exact the action `Final Answer` when you know the final server code."""
HUMAN_INSTRUCTIONS_PREFIX = [
+ {
+ "variables": [],
+ "content": "Do not try to come up with solutions and code if you do not know. Instead, ask the human for help.",
+ },
{
"variables": ["method"],
"content": """Use this starting template:
@@ -76,4 +77,6 @@ HUMAN_INSTRUCTIONS_SUFFIX = [
"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.",
"Deploy the code.",
+ """Thought: Here is the plan of what I will do based on the instructions I got:
+1.""",
]
diff --git a/api-service/database/database.py b/api-service/database/database.py
index 805f2d07f..e5520a351 100644
--- a/api-service/database/database.py
+++ b/api-service/database/database.py
@@ -35,6 +35,14 @@ class Database:
}
).eq("id", run_id).execute()
+ def push_raw_logs(self, run_id: str, logs_raw: str) -> None:
+ if logs_raw:
+ self.client.table(deploymentsTable).update(
+ {
+ "logs_raw": logs_raw,
+ }
+ ).eq("id", run_id).execute()
+
def update_state(self, run_id: str, state: DeploymentState) -> None:
self.client.table(deploymentsTable).update(
{
@@ -43,7 +51,7 @@ class Database:
).eq("id", run_id).execute()
def update_url(self, run_id: str, url: str) -> None:
- self.client.table("deployments").update(
+ self.client.table(deploymentsTable).update(
{
"url": url,
}