Skip to content

armgit / E2B

Public Imported from GitHub
0

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

push logs to db

Vasek Mlejnsky719a5ad17d41
diff --git a/api-service/app.py b/api-service/app.py
index 9d116bf8d..f9bdfd6c8 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -58,6 +58,8 @@ def generate():
route = body["route"]
envs: List[EnvVar] = body["envs"]
+ db.create_deployment(run_id=run_id, project_id=project_id, route_id=route_id)
+
request_body_blocks = [
block for block in blocks if block.get("type") == "RequestBody"
]
@@ -65,6 +67,7 @@ def generate():
request_body_blocks[0]["prompt"] if len(request_body_blocks) > 0 else None
)
+ # Create playground for the LLM
playground_tools, playground = create_playground_tools(
envs=envs,
route=route,
@@ -72,14 +75,23 @@ def generate():
request_body_template=request_body_template,
)
- cg = Codegen.from_playground_tools(playground_tools)
+ # Create a new instance of code generator
+ cg = Codegen.from_playground_and_database(
+ playground_tools=playground_tools,
+ database=db,
+ )
+
+ # Generate the code
cg.generate(
+ run_id=run_id,
route=route,
envs=envs,
method=method,
blocks=blocks,
)
+ db.update_state(run_id=run_id, state=DeploymentState.Finished)
+
playground.close()
return {
diff --git a/api-service/codegen/callback_handler.py b/api-service/codegen/callback_handler.py
index 0e2a3a7db..6320a8d1d 100644
--- a/api-service/codegen/callback_handler.py
+++ b/api-service/codegen/callback_handler.py
@@ -1,23 +1,42 @@
-from typing import Dict, Any, List, Union
-import sys
+from typing import Dict, Any, List, Union, Optional
+# import sys
+import uuid
+from pprint import pprint
+
+from pydantic import PrivateAttr
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import AgentAction, AgentFinish, LLMResult
+from database import Database
+
-class bcolors:
- HEADER = "\033[95m"
- OKBLUE = "\033[94m"
- OKCYAN = "\033[96m"
- OKGREEN = "\033[92m"
- WARNING = "\033[93m"
- FAIL = "\033[91m"
- ENDC = "\033[0m"
- BOLD = "\033[1m"
- UNDERLINE = "\033[4m"
+# class bcolors:
+# HEADER = "\033[95m"
+# OKBLUE = "\033[94m"
+# OKCYAN = "\033[96m"
+# OKGREEN = "\033[92m"
+# WARNING = "\033[93m"
+# FAIL = "\033[91m"
+# ENDC = "\033[0m"
+# BOLD = "\033[1m"
+# UNDERLINE = "\033[4m"
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()
+
+ def __init__(self, database: Database, run_id: str, **kwargs: Any):
+ super().__init__(**kwargs)
+ self._database = database
+ self._run_id = run_id
+
def on_llm_start(
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
) -> None:
@@ -46,7 +65,8 @@ class CustomCallbackHandler(BaseCallbackHandler):
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
"""Run when chain ends running."""
- pass
+ self._file.close()
+ # pass
def on_chain_error(
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
@@ -58,39 +78,65 @@ class CustomCallbackHandler(BaseCallbackHandler):
self, serialized: Dict[str, Any], input_str: str, **kwargs: Any
) -> None:
"""Run when tool starts running."""
- print(
- f"{bcolors.BOLD}{bcolors.OKBLUE}[+CustomCallbackHandler] TOOL START{bcolors.ENDC}{bcolors.OKBLUE}",
- flush=True,
+ self._file.write(
+ "\n[+CustomCallbackHandler] TOOL START\n",
)
- print(serialized)
- print(input_str)
- print("[-CustomCallbackHandler]")
- sys.stdout.flush()
+ 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()
def on_tool_end(self, output: str, **kwargs: Any) -> None:
"""Run when tool ends running."""
- print("[+CustomCallbackHandler] TOOL END", flush=True)
- print(output)
- print("[-CustomCallbackHandler]")
- sys.stdout.flush()
+
+ # 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["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."""
- print("[+CustomCallbackHandler] TOOL ERROR")
- print(error)
- print("[-CustomCallbackHandler]")
- sys.stdout.flush()
+ 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."""
- print("[+CustomCallbackHandler] AGENT ACTION", flush=True)
- print(action.tool)
- print(action.log)
- print(action.tool_input)
- print("[-CustomCallbackHandler]")
- sys.stdout.flush()
+ self._current_action_id = str(uuid.uuid4())
+ self._logs.append(
+ {
+ "id": self._current_action_id,
+ "tool": action.tool,
+ "input": action.tool_input,
+ "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."""
diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.py
index 3ff51eacb..7da126290 100644
--- a/api-service/codegen/codegen.py
+++ b/api-service/codegen/codegen.py
@@ -5,11 +5,12 @@ from typing import (
Any,
Dict,
Optional,
+ ClassVar,
)
import subprocess
from time import sleep
-from pydantic import BaseModel
+from pydantic import BaseModel, PrivateAttr
from langchain.chat_models import ChatOpenAI
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
@@ -23,6 +24,7 @@ from langchain.tools import BaseTool
# from database import Database
# from codegen.tools.playground import create_playground_tools
+from database import Database
from codegen.env import EnvVar
from codegen.agent import CodegenAgent, CodegenAgentExecutor
from codegen.prompt import (
@@ -69,7 +71,6 @@ class WriteCodeToFile(BaseTool):
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:
@@ -90,7 +91,6 @@ class DeployCode(BaseTool):
description = """Deploys the code."""
def _run(self, empty: str) -> str:
- print("Deploying...")
p = subprocess.Popen(
["git", "add", "."],
cwd="/Users/vasekmlejnsky/Developer/nodejs-express-server",
@@ -124,65 +124,107 @@ class DeployCode(BaseTool):
# 6. Once all works without any bugs and errors, write the code to the file.
# 7. Deploy the code.
# """
-cm = CallbackManager([StreamingStdOutCallbackHandler(), CustomCallbackHandler()])
class Codegen(BaseModel):
- agent: Optional[CodegenAgent]
- agent_executor: Optional[CodegenAgentExecutor]
- llm = ChatOpenAI(
- streaming=True,
- temperature=0,
- max_tokens=2056,
- verbose=True,
- callback_manager=cm,
- # callback_manager=CallbackManager([CustomCallbackHandler()]),
- )
- input_variables = ["input", "agent_scratchpad", "method"]
- tools = [
- # InvalidTool(),
- # OutputFinalCode(),
- AskHuman(),
- WriteCodeToFile(),
- DeployCode(),
- ]
+ input_variables: ClassVar[List[str]] = ["input", "agent_scratchpad", "method"]
+ _agent: CodegenAgent = PrivateAttr()
+ _agent_executor: CodegenAgentExecutor = PrivateAttr()
+ _tools: List[BaseTool] = PrivateAttr()
+ _llm: ChatOpenAI = PrivateAttr()
+ _database: Database = PrivateAttr()
+ _callback_manager: CallbackManager = PrivateAttr()
+
+ def __init__(
+ self,
+ database: Database,
+ callback_manager: CallbackManager,
+ tools: List[BaseTool],
+ llm: ChatOpenAI,
+ agent: CodegenAgent,
+ **kwargs: Any,
+ ) -> None:
+ super().__init__(**kwargs)
+ self._database = database
+ self._callback_manager = callback_manager
+ self._tools = tools
+ self._llm = llm
+ self._agent = agent
+
+ self._agent_executor = CodegenAgentExecutor.from_agent_and_tools(
+ agent=self._agent,
+ tools=self._tools,
+ verbose=True,
+ callback_manager=self._callback_manager,
+ )
@classmethod
- def from_playground_tools(cls, playground_tools: Tuple[List[Any]]):
- c = cls()
-
- # Create CodegenAgent and its executor
- c.agent = CodegenAgent.from_llm_and_tools(
- llm=c.llm,
- tools=[
- *playground_tools,
- *c.tools,
- ],
+ def from_playground_and_database(
+ cls,
+ playground_tools: List[BaseTool],
+ database: Database,
+ ):
+ callback_manager = CallbackManager(
+ [
+ # StreamingStdOutCallbackHandler(),
+ # CustomCallbackHandler(database=database),
+ ]
+ )
+
+ # Assign custom callback manager to all playground tools
+ for tool in playground_tools:
+ tool.callback_manager = callback_manager
+
+ # Prepare tools for Codegen
+ tools = [
+ # InvalidTool(),
+ # OutputFinalCode(),
+ *playground_tools,
+ AskHuman(callback_manager=callback_manager),
+ WriteCodeToFile(callback_manager=callback_manager),
+ DeployCode(callback_manager=callback_manager),
+ ]
+
+ # Create the LLM
+ llm = ChatOpenAI(
+ streaming=True,
+ temperature=0,
+ max_tokens=2056,
+ verbose=True,
+ callback_manager=callback_manager,
+ )
+
+ # Create CodegenAgent
+ agent = CodegenAgent.from_llm_and_tools(
+ llm=llm,
+ tools=tools,
prefix=PREFIX,
suffix=SUFFIX,
format_instructions=FORMAT_INSTRUCTIONS,
- input_variables=c.input_variables,
- callback_manager=cm,
- )
- c.agent_executor = CodegenAgentExecutor.from_agent_and_tools(
- agent=c.agent,
- tools=[
- *playground_tools,
- *c.tools,
- ],
- verbose=True,
- callback_manager=cm,
+ input_variables=Codegen.input_variables,
+ callback_manager=callback_manager,
)
- return c
+ return cls(
+ database=database,
+ callback_manager=callback_manager,
+ tools=tools,
+ llm=llm,
+ agent=agent,
+ )
def generate(
self,
envs: List[EnvVar],
+ run_id: str,
route: str,
method: str,
blocks: List[Dict],
):
+ self._callback_manager.add_handler(
+ CustomCallbackHandler(database=self._database, run_id=run_id)
+ )
+
input_vars = {
"route": route,
"method": method,
@@ -214,7 +256,7 @@ class Codegen(BaseModel):
inst_idx += 1
instructions = instructions + "\n" + f"{inst_idx}. {inst}"
- self.agent_executor.run(
+ self._agent_executor.run(
agent_scratchpad="",
# input=testing_instructions
input=instructions,
diff --git a/api-service/codegen/out.txt b/api-service/codegen/out.txt
new file mode 100644
index 000000000..b84804a1d
--- /dev/null
+++ b/api-service/codegen/out.txt
@@ -0,0 +1,238 @@
+
+[+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/tools/playground/main.py b/api-service/codegen/tools/playground/main.py
index f339cb84e..ae8479465 100644
--- a/api-service/codegen/tools/playground/main.py
+++ b/api-service/codegen/tools/playground/main.py
@@ -1,5 +1,7 @@
from typing import List, Any, Tuple
+from langchain.tools import BaseTool
+
from codegen.env import EnvVar
from codegen.tools.playground.mock.request import MockRequestFactory
@@ -14,7 +16,7 @@ def create_playground_tools(
method: str,
route: str,
request_body_template: str | None,
-) -> Tuple[List[Any], NodeJSPlayground]:
+) -> Tuple[List[BaseTool], NodeJSPlayground]:
playground = NodeJSPlayground(envs)
mock = MockRequestFactory(
method=method,
diff --git a/api-service/codegen/tools/playground/playground.py b/api-service/codegen/tools/playground/playground.py
index afb0646e9..be8c3a5c0 100644
--- a/api-service/codegen/tools/playground/playground.py
+++ b/api-service/codegen/tools/playground/playground.py
@@ -193,8 +193,7 @@ 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...")
+ # 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}",
@@ -202,13 +201,13 @@ class NodeJSPlayground(Playground):
env_vars=self.env_vars,
timeout=self.run_code_timeout,
)
- pprint.pprint(f"Result: {result}")
+ # pprint.pprint(f"Result: {result}")
return result
def install_dependencies(self, dependencies: str):
- print(f"Installing dependencies: {dependencies}")
+ # print(f"Installing dependencies: {dependencies}")
result = self.run_command(f"npm install {dependencies}", rootdir=self.rootdir)
- pprint.pprint(f"Result: {result}")
+ # pprint.pprint(f"Result: {result}")
return result
def run_javascript_server_code_with_request(
@@ -217,7 +216,7 @@ class NodeJSPlayground(Playground):
request_cmd: str,
port: float,
):
- print(f"Running '{request_cmd}' for code:\n{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}",
@@ -226,5 +225,5 @@ class NodeJSPlayground(Playground):
rootdir=self.rootdir,
env_vars=self.env_vars,
)
- pprint.pprint(f"Result: {result}")
+ # pprint.pprint(f"Result: {result}")
return result
diff --git a/api-service/database/database.py b/api-service/database/database.py
index 5f502db6a..805f2d07f 100644
--- a/api-service/database/database.py
+++ b/api-service/database/database.py
@@ -1,4 +1,4 @@
-from typing import List
+from typing import List, Dict
from enum import Enum
from supabase import create_client
@@ -27,7 +27,7 @@ class Database:
},
).execute()
- def push_logs(self, run_id: str, logs: List[str]) -> None:
+ def push_logs(self, run_id: str, logs: List[Dict[str, str]]) -> None:
if len(logs) > 0:
self.client.table(deploymentsTable).update(
{
diff --git a/api-service/test.py b/api-service/test.py
index ed00b4c14..25fe643b8 100644
--- a/api-service/test.py
+++ b/api-service/test.py
@@ -165,15 +165,15 @@ FORMAT_INSTRUCTIONS = """"""
# // 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:
+PREFIX = """You are an AI JavaScript backend developer.
+- 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 are using existing tools to figure out how to do your job better.
+You have access to the following set of primitive tools:"""
-FORMAT_INSTRUCTIONS = """"The way you use the tools is by specifying a json blob.
+FORMAT_INSTRUCTIONS = """"Your goal is to use existing tools to come up with a new set of tools that you, as an AI JavaScript backend developer, would use to do your job most effectively.
+The new set of tools MUST be based on the primitive tools you already have access to. The new tools should make a job of JavaScript backend developer easier by being a higher-level abstraction of the primitive tools you already have access to.
+Use the existing tools to build the new tools. 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}. 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:
@@ -198,9 +198,9 @@ Action:
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 code that satisfies all the input instructions. THE FINAL ANSWER IT MUST BE JUST THE CODE."""
+Final Answer: the final answer."""
-SUFFIX = """Begin! Reminder to NEVER use tools you don't have access to and ALWAYS use the exact characters `Final Answer` when responding."""
+SUFFIX = """Begin! Reminder to always use the exact characters `Final Answer` when responding."""
# Create model
@@ -208,19 +208,19 @@ cm = CallbackManager([StreamingStdOutCallbackHandler()])
chat = ChatOpenAI(
openai_api_key=OPENAI_API_KEY,
streaming=True,
- temperature=0,
+ temperature=0.0,
max_tokens=2056,
verbose=True,
callback_manager=cm,
)
# Initialize playground
-playground_tools, playground = create_playground_tools(
- envs=[],
- route="/",
- method="post",
- request_body_template="email: string",
-)
+# playground_tools, playground = create_playground_tools(
+# envs=[],
+# route="/",
+# method="post",
+# request_body_template="email: string",
+# )
# Create prompt
input_variables = [
@@ -230,50 +230,91 @@ input_variables = [
# "instructions",
]
# prompt = ChatAgent.create_prompt(
-prompt = CustomChatAgent.create_prompt(
- tools=[
- *playground_tools,
- WriteCodeToFile(),
- DeployCode(),
- ],
- prefix=PREFIX,
- suffix=SUFFIX,
- format_instructions=FORMAT_INSTRUCTIONS,
- input_variables=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()
+class WriteToFile(BaseTool):
+ name = "WriteToFile"
+ description = "Write content to file on path. The input is in the format of `$PATH_TO_FILE $CONTENT`"
+
+ def _run(self, query: str) -> str:
+ return "Successfully wrote to file"
+
+ def _arun(self, tool_input: str) -> str:
+ NotImplementedError("Not implemented")
+
+
+class ReadFromFile(BaseTool):
+ name = "ReadFromFile"
+ description = (
+ "Read content from file on path. The input is in the format of `$PATH_TO_FILE`"
+ )
+
+ def _run(self, query: str) -> str:
+ return "Successfully read from file"
+
+ def _arun(self, tool_input: str) -> str:
+ NotImplementedError("Not implemented")
+
+
+class RunShellCommand(BaseTool):
+ name = "RunShellCommand"
+ description = "Run shell command. The input is in the format of `$COMMAND`"
+
+ def _run(self, tool_input: str) -> str:
+ return "Successfully ran shell command"
+
+ def _arun(self, tool_input: str) -> str:
+ NotImplementedError("Not implemented")
+
+
invalid_tool = InvalidTool()
# Create agent and it's executor
# agent = ChatAgent.from_llm_and_tools(
memory = ConversationBufferWindowMemory(k=1, return_messages=True)
-agent = CustomChatAgent.from_llm_and_tools(
+agent = ChatAgent.from_llm_and_tools(
llm=chat,
tools=[
- *playground_tools,
- invalid_tool,
- WriteCodeToFile(),
- DeployCode(),
+ # *playground_tools,
+ # invalid_tool,
+ # WriteCodeToFile(),
+ # DeployCode(),
+ WriteToFile(),
+ ReadFromFile(),
+ RunShellCommand(),
],
prefix=PREFIX,
suffix=SUFFIX,
format_instructions=FORMAT_INSTRUCTIONS,
input_variables=input_variables,
- # verbose=True,
+ verbose=True,
# memory=ConversationBufferWindowMemory(k=2),
- memory=memory,
+ # memory=memory,
)
ae = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=[
- *playground_tools,
- invalid_tool,
- WriteCodeToFile(),
- DeployCode(),
+ # *playground_tools,
+ # invalid_tool,
+ # WriteCodeToFile(),
+ # DeployCode(),
+ WriteToFile(),
+ ReadFromFile(),
+ RunShellCommand(),
],
- memory=memory,
+ # memory=memory,
verbose=True,
)
@@ -303,7 +344,9 @@ try:
ae.run(
# "1. Check if the incoming request is POST request. If not, respond with an adequate error",
agent_scratchpad="",
- input=f"Here are the instructions:\n{user_input}",
+ # input=f"Here are the instructions:\n{user_input}",
+ # input="Given the tools you already have access to, what would be the ideal tools you need to make your job as a JavaScript backend developer most effectively?",
+ input="What new tools do you need?",
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.
@@ -391,8 +434,8 @@ except ValueError as e:
# # chat([*chat_messages, instruct1_prompt])
-print("=========== MEMORY")
-for message in memory.buffer:
- print(message.content)
+# print("=========== MEMORY")
+# for message in memory.buffer:
+# print(message.content)
-playground.close()
+# playground.close()