Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Cleanup; Connect AskHuman to frontend [WIP]
Tomas Valenta412a55788213
diff --git a/api-service/app.py b/api-service/app.pyindex 00f83fa22..e279f435a 100644--- a/api-service/app.py+++ b/api-service/app.py@@ -64,6 +64,7 @@ def generate():cg = Codegen.from_playground_and_database(playground_tools=playground_tools,database=db,+ run_id=run_id,)# Generate the codediff --git a/api-service/codegen/callback_handler.py b/api-service/codegen/callback_handler.pyindex cbbf81e90..8f19de955 100644--- a/api-service/codegen/callback_handler.py+++ b/api-service/codegen/callback_handler.py@@ -47,7 +47,8 @@ class RepeatedTimer(object):self.is_running = Truedef stop(self):- self._timer.cancel()+ if self._timer:+ self._timer.cancel()self.is_running = Falsediff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.pyindex 48664adb4..3c1e9b988 100644--- a/api-service/codegen/codegen.py+++ b/api-service/codegen/codegen.py@@ -1,29 +1,18 @@from typing import (List,- Tuple,List,Any,Dict,- Optional,ClassVar,)-import subprocess-from time import sleep-+from codegen.tools.human.ask import AskHuman+from langchain.agents import AgentExecutorfrom pydantic import BaseModel, PrivateAttrfrom langchain.chat_models import ChatOpenAIfrom langchain.callbacks.base import CallbackManagerfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlerfrom langchain.tools import BaseTool-# from codegen.tools.documentation import ReadDocumentation-# from codegen.env import EnvVar-# from codegen.js_agent import create_js_agent-# 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_toolsfrom session.env import EnvVarfrom database import Databasefrom codegen.agent import CodegenAgent, CodegenAgentExecutor@@ -36,25 +25,6 @@ from codegen.prompt import ()from .callback_handler import CustomCallbackHandler--class AskHuman(BaseTool):- name = "AskHuman"- description = "You can ask a human for guidance when you think you got stuck or you are not sure what to do next. The input should be a question for the human."-- def _run(self, question: str) -> str:- print(question)- sleep(60)- with open(- "/Users/vasekmlejnsky/Developer/ai-api/api-service/codegen/human_input.txt"- ) as f:- human_input = f.read()- print("human_input:", human_input)- return human_input-- async def _arun(self, question: str) -> str:- return NotImplementedError("AskHuman does not support async")--# 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."@@ -63,56 +33,7 @@ class AskHuman(BaseTool):# return final_code# async def _arun(self, final_code: str) -> str:-# return NotImplementedError("OutputFinalCode does not support async")---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:- with open(- "/Users/vasekmlejnsky/Developer/nodejs-express-server/index.js", "w"- ) as f:- f.write(- f"""- import {{ createRequire }} from "module";- const require = createRequire(import.meta.url);- {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:- 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")+# raise NotImplementedError("OutputFinalCode does not support async")# testing_instructions = """Here are your instructions:@@ -129,7 +50,7 @@ class DeployCode(BaseTool):class Codegen(BaseModel):input_variables: ClassVar[List[str]] = ["input", "agent_scratchpad", "method"]_agent: CodegenAgent = PrivateAttr()- _agent_executor: CodegenAgentExecutor = PrivateAttr()+ _agent_executor: AgentExecutor = PrivateAttr()_tools: List[BaseTool] = PrivateAttr()_llm: ChatOpenAI = PrivateAttr()_database: Database = PrivateAttr()@@ -163,6 +84,7 @@ class Codegen(BaseModel):cls,playground_tools: List[BaseTool],database: Database,+ run_id: str):callback_manager = CallbackManager([@@ -180,8 +102,8 @@ class Codegen(BaseModel):# InvalidTool(),# OutputFinalCode(),*playground_tools,- AskHuman(callback_manager=callback_manager),- WriteCodeToFile(callback_manager=callback_manager),+ AskHuman(callback_manager=callback_manager, run_id=run_id),+ # WriteCodeToFile(callback_manager=callback_manager),# DeployCode(callback_manager=callback_manager),]@@ -262,78 +184,3 @@ class Codegen(BaseModel):input=instructions,method=method,)---# def generate_req_handler(-# db: Database,-# run_id: str,-# blocks: List[Dict],-# method: str,-# route: str,-# envs: List[EnvVar],-# ):-# request_body_blocks = [-# block for block in blocks if block.get("type") == "RequestBody"-# ]-# request_body_template = (-# request_body_blocks[0]["prompt"] if len(request_body_blocks) > 0 else None-# )-# playground_tools, playground = create_playground_tools(-# envs=envs,-# route=route,-# method=method,-# request_body_template=request_body_template,-# )--# steps = ""-# for idx, block in enumerate(blocks):-# if block.get("type") == "Basic":-# steps = steps + "\n" + "{}. ".format(idx + 1) + block["prompt"] + "\n"-# tool_names = ["InstallNPMDependencies", "RunJavaScriptCode"]-# prefix = PREFIX.format(-# method=method,-# tool_names=tool_names,-# steps=steps,-# request_body=request_body_template,-# )-# format_instructions = FORMAT_INSTRUCTIONS.format(-# tool_names=tool_names,-# )-# executor = create_js_agent(-# db=db,-# run_id=run_id,-# llm=OpenAI(temperature=0, max_tokens=1000),-# # llm=OpenAI(temperature=0, model_name='code-davinci-002', max_tokens=1000),-# # llm=OpenAIChat(temperature=0, max_tokens=1000),-# tools=[-# # ReadDocumentation()-# *playground_tools,-# ],-# verbose=True,-# prefix=prefix,-# format_instructions=format_instructions,-# )--# # Convert env vars to Javascript comments, each on its on line for each env var.-# # envs_str = ""-# # for env in envs:-# # envs_str += f'// const {env["key"]} = `env.{env["key"]}`\n'--# # prompt = PREFIX.format(-# # method=method, envs=envs_str, request_body=request_body_template-# # )--# # for idx, block in enumerate(blocks):-# # if block.get("type") == "Basic":-# # prompt = prompt + "\n" + "{}. ".format(idx + 1) + block["prompt"] + "\n"--# # prompt = prompt + "\n" + SUFFIX.format(method=method)--# # handler_code = executor.run(prompt).strip("`").strip()-# handler_code = executor.run(f"Requirement:").strip("`").strip()-# print("CODE")-# print(handler_code)--# playground.close()--# return "", handler_codediff --git a/api-service/codegen/tools/documentation/tool.py b/api-service/codegen/tools/documentation/tool.pyindex 40e9a05c5..ff73f5933 100644--- a/api-service/codegen/tools/documentation/tool.py+++ b/api-service/codegen/tools/documentation/tool.py@@ -17,4 +17,4 @@ class ReadDocumentation(BaseTool):return f.read()async def _arun(self, query: str) -> str:- return NotImplementedError("ReadDocumentation does not support async")+ raise NotImplementedError("ReadDocumentation does not support async")diff --git a/api-service/codegen/tools/human/__init__.py b/api-service/codegen/tools/human/__init__.pynew file mode 100644index 000000000..ce22369ea--- /dev/null+++ b/api-service/codegen/tools/human/__init__.py@@ -0,0 +1 @@+from .ask import AskHumandiff --git a/api-service/codegen/tools/human/ask.py b/api-service/codegen/tools/human/ask.pynew file mode 100644index 000000000..a2442ca43--- /dev/null+++ b/api-service/codegen/tools/human/ask.py@@ -0,0 +1,46 @@+from time import sleep+from langchain.callbacks.base import CallbackManager++from langchain.tools import BaseTool++# {+# "id": "52b2b157-132f-47c6-be1e-3c203034fdfb",+# "name": "InstallNPMDependencies",+# "type": "tool",+# "input": "\nexpress\n",+# "output": "All dependencies installed",+# "start_at": "2023-03-19 23:49:23.079327",+# "finish_at": "2023-03-19 23:49:26.002323"+# },+++class AskHuman(BaseTool):+ name = "AskHuman"+ description = "You can ask a human for guidance when you think you got stuck or you are not sure what to do next. The input should be a question for the human."++ def __init__(self, callback_manager: CallbackManager, run_id: str):+ super().__init__(name=self.name, description=self.description, callback_manager=callback_manager)+ self.run_id = run_id++ def _run(self, question: str) -> str:+ # TODO: Save the question to DB (in logs? - should we still use callback manager)+++++ # TODO: Wait until the question is answered -> timeout?+ # TODO: Parse and return the correct output from the logs++++ # print(question)+ # sleep(60)+ # with open(+ # "/Users/vasekmlejnsky/Developer/ai-api/api-service/codegen/human_input.txt"+ # ) as f:+ # human_input = f.read()+ # print("human_input:", human_input)+ # return human_input++ async def _arun(self, question: str) -> str:+ raise NotImplementedError("AskHuman does not support async")diff --git a/api-service/database/realtime.py b/api-service/database/realtime.pyindex 2563033de..66c0666e8 100644--- a/api-service/database/realtime.py+++ b/api-service/database/realtime.py@@ -1,35 +1,31 @@+from time import sleepimport os-import threading-import asynciofrom realtime.connection import Socketsupabase_key = os.environ.get("SUPABASE_SECRET_KEY")-def get_out():- loop = asyncio.new_event_loop()- event = threading.Event()-+async def get_out():result = None--URL = f"wss://ntjfcwpzsxugrykskdgi.supabase.co/realtime/v1/websocket?apikey={supabase_key}&vsn=1.0.0"s = Socket(URL)- s.connect()+ await s._connect()async def callback(payload):nonlocal resultresult = payloadprint(payload)- event.set()- channel = s.set_channel(s, "realtime:*")- channel.join().on("UPDATE", callback)- s.listen(s)+ channel = s.set_channel("realtime:*") # type: ignore+ channel.on("UPDATE", callback)+ await s._listen() # type: ignore- event.wait()- channel.off("UPDATE")+ for i in range(300):+ print("check")+ if result:+ return result+ sleep(1)return resultdiff --git a/api-service/test.py b/api-service/test.pyindex 25fe643b8..5498025bc 100644--- a/api-service/test.py+++ b/api-service/test.py@@ -44,7 +44,7 @@ class WriteCodeToFile(BaseTool):return "wrote code to index.js"async def _arun(self, err: str) -> str:- return NotImplementedError("WriteCodeToFile does not support async")+ raise NotImplementedError("WriteCodeToFile does not support async")class DeployCode(BaseTool):@@ -74,7 +74,7 @@ class DeployCode(BaseTool):return f"deployed server"async def _arun(self, empty: str) -> str:- return NotImplementedError("DeployCode does not support async")+ raise NotImplementedError("DeployCode does not support async")FINAL_ANSWER_ACTION = "Final Answer:"