Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Cleanup
Tomas Valenta1257d1c36f5d
diff --git a/api-service/agent/smol_agent.py b/api-service/agent/smol_agent.pyindex 6311ff693..3e18b3225 100644--- a/api-service/agent/smol_agent.py+++ b/api-service/agent/smol_agent.py@@ -3,10 +3,17 @@ import uuidimport osimport ast+from langchain.callbacks import get_openai_callbackfrom typing import Any, Listfrom langchain.callbacks.base import AsyncCallbackManagerfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler-from langchain.schema import AIMessage, BaseMessage, SystemMessage, HumanMessage+from langchain.schema import (+ AIMessage,+ BaseLanguageModel,+ BaseMessage,+ SystemMessage,+ HumanMessage,+)from langchain.prompts.chat import (ChatPromptValue,)@@ -33,6 +40,7 @@ class SmolAgent(AgentBase):get_envs: GetEnvs,on_logs: OnLogs,on_interaction_request: OnInteractionRequest,+ model: BaseLanguageModel,):super().__init__()self._dev_loop: asyncio.Task | None = None@@ -40,6 +48,7 @@ class SmolAgent(AgentBase):self.config = ModelConfig(**config)self.on_interaction_request = on_interaction_requestself.on_logs = on_logs+ self.model = model@classmethodasync def create(@@ -49,13 +58,113 @@ class SmolAgent(AgentBase):on_logs: OnLogs,on_interaction_request: OnInteractionRequest,):+ callback_manager = AsyncCallbackManager([StreamingStdOutCallbackHandler()])++ # Use default openai api key if not provided+ config.args["openai_api_key"] = config.args.get(+ "openai_api_key", default_openai_api_key+ )++ model = get_model(config, callback_manager)+return cls(config,get_envs,on_logs,on_interaction_request,+ model,+ )++ async def generate_file(+ self,+ filename: str,+ filepaths_string=None,+ shared_dependencies=None,+ prompt=None,+ ):+ # call openai api with this prompt+ filecode, metadata = await self.generate_response(+ f"""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.++the app is: {prompt}++the files we have decided to generate are: {filepaths_string}++the shared dependencies (like filenames and variable names) we have decided on are: {shared_dependencies}++only write valid code for the given filepath and file type, and return only the code.+do not add any other explanation, only return valid code for that file type.+""",+ f"""+We have broken up the program into per-file generation.+Now your job is to generate only the code for the file {filename}.+Make sure to have consistent filenames if you reference other files we are also generating.++Remember that you must obey 3 things:+- you are generating code for the file {filename}+- do not stray from the names of the files and the shared dependencies we have decided on+- MOST IMPORTANT OF ALL - the purpose of our app is {prompt} - every line of code you generate must be valid code. Do not include code fences in your response, for example++Bad response:+```javascript+console.log("hello world")+```++Good response:+console.log("hello world")++Begin generating the code now.++""",+ )++ await self.on_logs(+ {+ "message": f"Generated file",+ "properties": {+ **metadata,+ "result": filecode,+ "model": "gpt-4",+ },+ "type": "model",+ })+ return filename, filecode, metadata++ async def generate_response(+ self,+ system_prompt: str,+ user_prompt: str,+ *args: Any,+ ):+ messages: List[BaseMessage] = [+ SystemMessage(content=system_prompt),+ HumanMessage(content=user_prompt),+ ]++ # loop thru each arg and add it to messages alternating role between "assistant" and "user"+ role = "assistant"+ for value in args:+ if role == "assistant":+ messages.append(AIMessage(content=value))+ else:+ messages.append(HumanMessage(content=value))+ role = "user" if role == "assistant" else "assistant"++ model_prompt = ChatPromptValue(messages=messages)++ with get_openai_callback() as cb:+ response = await self.model.agenerate_prompt([model_prompt])++ return response.generations[0][0].text, {+ "prompt": model_prompt.to_string(),+ "total_tokens": cb.total_tokens,+ "prompt_tokens": cb.prompt_tokens,+ "response_tokens": cb.completion_tokens,+ "cost": cb.total_cost,+ }+async def _dev(self, instructions: Any):user_prompt: str = instructions["Prompt"]access_token: str = instructions["AccessToken"]@@ -70,26 +179,14 @@ class SmolAgent(AgentBase):f"https://{git_app_name}:{access_token}@github.com/{owner}/{repo}.git")- # TODO: Apply the file rewrite when the agent was invoked via PR code comment- file: str | None = instructions.get("File", None)-await self.on_logs({- "message": f"hi its me, 🐣the smol developer🐣! you said you wanted:\n{user_prompt}",+ "message": f"hi its me, 🐣the smol developer🐣!","type": "info",})playground = Nonetry:- callback_manager = AsyncCallbackManager([StreamingStdOutCallbackHandler()])-- # Use default openai api key if not provided- self.config.args["openai_api_key"] = self.config.args.get(- "openai_api_key", default_openai_api_key- )-- model = get_model(self.config, callback_manager)-playground = Playground(env_id="PPSrlH5TIvFx", get_envs=self.get_envs)await playground.open()@@ -139,94 +236,7 @@ class SmolAgent(AgentBase):".tiff",]- async def clean_dir():- files = await playground.get_filenames(rootdir, [".git"])- for file in files:- _, extension = os.path.splitext(file.name)- if extension not in extensions_to_skip:- await playground.delete_file(file.name)-- async def generate_response(- system_prompt: str,- user_prompt: str,- *args: Any,- ):- messages: List[BaseMessage] = [- SystemMessage(content=system_prompt),- HumanMessage(content=user_prompt),- ]-- # loop thru each arg and add it to messages alternating role between "assistant" and "user"- role = "assistant"- for value in args:- if role == "assistant":- messages.append(AIMessage(content=value))- else:- messages.append(HumanMessage(content=value))- role = "user" if role == "assistant" else "assistant"-- model_prompt = ChatPromptValue(messages=messages)-- response = await model.agenerate_prompt([model_prompt])- return response.generations[0][0].text, model_prompt.to_string()-- async def generate_file(- filename: str,- filepaths_string=None,- shared_dependencies=None,- prompt=None,- ):- # call openai api with this prompt- filecode, file_prompt = await generate_response(- f"""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.--the app is: {prompt}--the files we have decided to generate are: {filepaths_string}--the shared dependencies (like filenames and variable names) we have decided on are: {shared_dependencies}--only write valid code for the given filepath and file type, and return only the code.-do not add any other explanation, only return valid code for that file type.-""",- f"""-We have broken up the program into per-file generation.-Now your job is to generate only the code for the file {filename}.-Make sure to have consistent filenames if you reference other files we are also generating.--Remember that you must obey 3 things:-- you are generating code for the file {filename}-- do not stray from the names of the files and the shared dependencies we have decided on-- MOST IMPORTANT OF ALL - the purpose of our app is {prompt} - every line of code you generate must be valid code. Do not include code fences in your response, for example--Bad response:-```javascript-console.log("hello world")-```--Good response:-console.log("hello world")--Begin generating the code now.--""",- )-- await self.on_logs(- {- "message": f"Generated file",- "properties": {- "prompt": file_prompt,- "result": filecode,- "model": "gpt-4",- },- "type": "model",- }- )-- return filename, filecode, file_prompt-- filepaths_string, filepaths_prompt = await generate_response(+ filepaths_string, metadata = await self.generate_response("""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.When given their intent, create a complete, exhaustive list of filepaths that the user would write to make the program.@@ -241,7 +251,7 @@ do not add any other explanation, only return a python list of strings.{"message": f"Generating filepaths","properties": {- "prompt": filepaths_prompt,+ **metadata,"result": filepaths_string,"model": "gpt-4",},@@ -261,59 +271,27 @@ do not add any other explanation, only return a python list of strings.except NotFoundException:pass- if file is not None:- filename, filecode, file_prompt = await generate_file(- file,- filepaths_string=filepaths_string,- shared_dependencies=shared_dependencies,- prompt=user_prompt,- )+ files = await playground.get_filenames(rootdir, [".git"])+ for file in files:+ _, extension = os.path.splitext(file.name)+ if extension not in extensions_to_skip:+ await playground.delete_file(file.name)- await self.on_logs(- {- "message": f"Generated file",- "properties": {- "prompt": file_prompt,- "result": filecode,- "model": "gpt-4",- },- "type": "model",- }- )-- await playground.write_file(os.path.join(rootdir, filename), filecode)-- await self.on_logs(- {- "message": f"Saved file",- "properties": {- "filename": filename,- "content": filecode,- "tool": "filesystem",- },- "type": "tool",- }- )-- else:- await clean_dir()-- await self.on_logs(- {- "message": f"Cleaned root directory",- "properties": {- "tool": "filesystem",- },- "type": "tool",- }- )+ await self.on_logs(+ {+ "message": f"Cleaned root directory",+ "properties": {+ "tool": "filesystem",+ },+ "type": "tool",+ }+ )- # understand shared dependencies- (- shared_dependencies,- shared_dependencies_prompt,- ) = await generate_response(- """You are an AI developer who is trying to write a program that will generate code for the user based on their intent.+ (+ shared_dependencies,+ metadata,+ ) = await self.generate_response(+ """You are an AI developer who is trying to write a program that will generate code for the user based on their intent.In response to the user's prompt:@@ -327,78 +305,76 @@ Now that we have a list of files, we need to understand what dependencies they sPlease name and briefly describe what is shared between the files we are generating, including exported variables, data schemas, id names of every DOM elements that javascript functions will use, message names, and function names.Exclusively focus on the names of the shared dependencies, and do not add any other explanation.""",- user_prompt,- )+ user_prompt,+ )- await self.on_logs(- {- "message": f"Generated shared dependencies",- "properties": {- "prompt": shared_dependencies_prompt,- "result": shared_dependencies,- "model": "gpt-4",- },- "type": "model",- }- )+ await self.on_logs(+ {+ "message": f"Generated shared dependencies",+ "properties": {+ **metadata,+ "result": shared_dependencies,+ "model": "gpt-4",+ },+ "type": "model",+ }+ )++ # write shared dependencies as a md file inside the generated directory+ await playground.write_file(+ os.path.join(rootdir, "shared_dependencies.md"),+ shared_dependencies,+ )- # write shared dependencies as a md file inside the generated directory- await playground.write_file(- os.path.join(rootdir, "shared_dependencies.md"),- shared_dependencies,+ await self.on_logs(+ {+ "message": f"Saved shared dependencies",+ "properties": {+ "filename": "shared_dependencies.md",+ "content": shared_dependencies,+ "tool": "filesystem",+ },+ "type": "tool",+ }+ )+ # Maximum number of allowed concurrent calls+ semaphore = asyncio.Semaphore(3)++ async def with_semaphore(coro):+ async with semaphore:+ return await coro++ # execute the file generation in paralell and wait for all of them to finish. Use list comprehension to generate the tasks+ coros = [+ with_semaphore(+ self.generate_file(+ name,+ filepaths_string=filepaths_string,+ shared_dependencies=shared_dependencies,+ prompt=user_prompt,+ ))+ for name in list_actual+ # Filter out files that end with esxtensions we don't want to generate+ if not any(name.endswith(extension) for extension in extensions_to_skip)+ ]++ generated_files = await asyncio.gather(*coros)+ for name, content, file_prompt in generated_files:+ filepath = os.path.join(rootdir, name)+ await playground.write_file(filepath, content)await self.on_logs({- "message": f"Saved shared dependencies",+ "message": f"Saved file","properties": {- "filename": "shared_dependencies.md",- "content": shared_dependencies,+ "filename": filepath,+ "content": content,"tool": "filesystem",},"type": "tool",})- # Maximum number of allowed concurrent calls- semaphore = asyncio.Semaphore(3)-- async def with_semaphore(coro):- async with semaphore:- return await coro-- # execute the file generation in paralell and wait for all of them to finish. Use list comprehension to generate the tasks- coros = [- with_semaphore(- generate_file(- name,- filepaths_string=filepaths_string,- shared_dependencies=shared_dependencies,- prompt=user_prompt,- )- )- for name in list_actual- # Filter out files that end with esxtensions we don't want to generate- if not any(- name.endswith(extension) for extension in extensions_to_skip- )- ]-- generated_files = await asyncio.gather(*coros)-- for name, content, file_prompt in generated_files:- filepath = os.path.join(rootdir, name)- await playground.write_file(filepath, content)- await self.on_logs(- {- "message": f"Saved file",- "properties": {- "filename": filepath,- "content": content,- "tool": "filesystem",- },- "type": "tool",- }- )await fixClockDriftawait playground.push_repo(@@ -456,9 +432,7 @@ Exclusively focus on the names of the shared dependencies, and do not add any otAgentInteractionRequest(interaction_id=str(uuid.uuid4()),type="cancelled",- data={- # "associated_comment_id": self._dev_comment_id,- },+ data={},))@@ -467,7 +441,6 @@ Exclusively focus on the names of the shared dependencies, and do not add any otself._dev_loop = asyncio.create_task(self._dev(instructions=instructions),)-await asyncio.wait_for(self._dev_loop,timeout=self.max_run_time,