AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Refactored forge to a cleaner layout
SwiftyOS13c53b650d35
diff --git a/agent.db b/agent.dbdeleted file mode 100644index ff241f140f..0000000000Binary files a/agent.db and /dev/null differdiff --git a/autogpt/__main__.py b/autogpt/__main__.pyindex 7deefcf6e3..16ef14b079 100644--- a/autogpt/__main__.py+++ b/autogpt/__main__.py@@ -3,23 +3,23 @@ import osfrom dotenv import load_dotenvload_dotenv()-import autogpt.forge_log+import autogpt.sdk.forge_logENABLE_TRACING = os.environ.get("ENABLE_TRACING", "false").lower() == "true"-autogpt.forge_log.setup_logger()+autogpt.sdk.forge_log.setup_logger()-LOG = autogpt.forge_log.CustomLogger(__name__)+LOG = autogpt.sdk.forge_log.CustomLogger(__name__)if __name__ == "__main__":"""Runs the agent server"""# modules are imported here so that logging is setup firstimport autogpt.agent- import autogpt.db+ import autogpt.sdk.dbfrom autogpt.benchmark_integration import add_benchmark_routes- from autogpt.workspace import LocalWorkspace+ from autogpt.sdk.workspace import LocalWorkspacerouter = add_benchmark_routes()@@ -27,7 +27,7 @@ if __name__ == "__main__":workspace = LocalWorkspace(os.getenv("AGENT_WORKSPACE"))port = os.getenv("PORT")- database = autogpt.db.AgentDB(database_name, debug_enabled=False)- agent = autogpt.agent.Agent(database=database, workspace=workspace)+ database = autogpt.sdk.db.AgentDB(database_name, debug_enabled=False)+ agent = autogpt.agent.AutoGPTAgent(database=database, workspace=workspace)agent.start(port=port, router=router)diff --git a/autogpt/agent.py b/autogpt/agent.pyindex 6204f7483d..3563e86ea3 100644--- a/autogpt/agent.py+++ b/autogpt/agent.py@@ -1,238 +1,12 @@-import asyncio-import os+import autogpt.sdk.agent+from autogpt.sdk.schema import Step, StepRequestBody-from fastapi import APIRouter, FastAPI, Response, UploadFile-from fastapi.responses import FileResponse-from hypercorn.asyncio import serve-from hypercorn.config import Config-from prometheus_fastapi_instrumentator import Instrumentator--from .db import AgentDB-from .errors import NotFoundError-from .forge_log import CustomLogger-from .middlewares import AgentMiddleware-from .routes.agent_protocol import base_router-from .schema import *-from .tracing import setup_tracing-from .utils import run-from .workspace import Workspace, load_from_uri--LOG = CustomLogger(__name__)---class Agent:- def __init__(self, database: AgentDB, workspace: Workspace):- self.db = database- self.workspace = workspace-- def start(self, port: int = 8000, router: APIRouter = base_router):- """- Start the agent server.- """- config = Config()- config.bind = [f"localhost:{port}"]- app = FastAPI(- title="Auto-GPT Forge",- description="Modified version of The Agent Protocol.",- version="v0.4",- )-- # Add Prometheus metrics to the agent- # https://github.com/trallnag/prometheus-fastapi-instrumentator- instrumentator = Instrumentator().instrument(app)-- @app.on_event("startup")- async def _startup():- instrumentator.expose(app)-- app.include_router(router)- app.add_middleware(AgentMiddleware, agent=self)- setup_tracing(app)- config.loglevel = "ERROR"- config.bind = [f"0.0.0.0:{port}"]-- LOG.info(f"Agent server starting on http://{config.bind[0]}")- asyncio.run(serve(app, config))-- async def create_task(self, task_request: TaskRequestBody) -> Task:- """- Create a task for the agent.- """- try:- task = await self.db.create_task(- input=task_request.input,- additional_input=task_request.additional_input,- )- return task- except Exception as e:- raise-- async def list_tasks(self, page: int = 1, pageSize: int = 10) -> TaskListResponse:- """- List all tasks that the agent has created.- """- try:- tasks, pagination = await self.db.list_tasks(page, pageSize)- response = TaskListResponse(tasks=tasks, pagination=pagination)- return response- except Exception as e:- raise-- async def get_task(self, task_id: str) -> Task:- """- Get a task by ID.- """- try:- task = await self.db.get_task(task_id)- except Exception as e:- raise- return task-- async def list_steps(- self, task_id: str, page: int = 1, pageSize: int = 10- ) -> TaskStepsListResponse:- """- List the IDs of all steps that the task has created.- """- try:- steps, pagination = await self.db.list_steps(task_id, page, pageSize)- response = TaskStepsListResponse(steps=steps, pagination=pagination)- return response- except Exception as e:- raise+class AutoGPTAgent(autogpt.sdk.agent.Agent):async def create_and_execute_step(self, task_id: str, step_request: StepRequestBody) -> Step:"""- Create a step for the task.- """- if step_request.input != "y":- step = await self.db.create_step(- task_id=task_id,- input=step_request,- additional_input=step_request.additional_input,- )- # utils.run- artifacts = run(step.input)- for artifact in artifacts:- art = await self.db.create_artifact(- task_id=step.task_id,- file_name=artifact["file_name"],- uri=artifact["uri"],- agent_created=True,- step_id=step.step_id,- )- assert isinstance(- art, Artifact- ), f"Artifact not instance of Artifact {type(art)}"- step.artifacts.append(art)- step.status = "completed"- else:- steps, steps_pagination = await self.db.list_steps(- task_id, page=1, per_page=100- )- # Find the latest step that has not been completed- step = next((s for s in reversed(steps) if s.status != "completed"), None)- if step is None:- # If all steps have been completed, create a new placeholder step- step = await self.db.create_step(- task_id=task_id,- input="y",- additional_input={},- )- step.status = "completed"- step.is_last = True- step.output = "No more steps to run."- step = await self.db.update_step(step)- if isinstance(step.status, Status):- step.status = step.status.value- step.output = "Done some work"- return step-- async def get_step(self, task_id: str, step_id: str) -> Step:- """- Get a step by ID.- """- try:- step = await self.db.get_step(task_id, step_id)- return step- except Exception as e:- raise-- async def list_artifacts(- self, task_id: str, page: int = 1, pageSize: int = 10- ) -> TaskArtifactsListResponse:- """- List the artifacts that the task has created.- """- try:- artifacts, pagination = await self.db.list_artifacts(- task_id, page, pageSize- )- response = TaskArtifactsListResponse(- artifacts=artifacts, pagination=pagination- )- return Response(content=response.json(), media_type="application/json")- except Exception as e:- raise-- async def create_artifact(- self,- task_id: str,- file: UploadFile | None = None,- uri: str | None = None,- ) -> Artifact:- """- Create an artifact for the task.- """- data = None- if not uri:- file_name = file.filename or str(uuid4())- try:- data = b""- while contents := file.file.read(1024 * 1024):- data += contents- except Exception as e:- raise- else:- try:- data = await load_from_uri(uri, task_id)- file_name = uri.split("/")[-1]- except Exception as e:- raise-- file_path = os.path.join(task_id / file_name)- self.write(file_path, data)- self.db.save_artifact(task_id, artifact)-- artifact = await self.create_artifact(- task_id=task_id,- file_name=file_name,- uri=f"file://{file_path}",- agent_created=False,- )-- return artifact-- async def get_artifact(self, task_id: str, artifact_id: str) -> Artifact:- """- Get an artifact by ID.+ Create a step for the task and execute it."""- try:- artifact = await self.db.get_artifact(artifact_id)- retrieved_artifact = await self.load_from_uri(artifact.uri, artifact_id)- path = artifact.file_name- with open(path, "wb") as f:- f.write(retrieved_artifact)- except NotFoundError as e:- raise- except FileNotFoundError as e:- raise- except Exception as e:- raise- return FileResponse(- # Note: mimetype is guessed in the FileResponse constructor- path=path,- filename=artifact.file_name,- )+ return await super().create_and_execute_step(task_id, step_request)diff --git a/autogpt/benchmark_integration.py b/autogpt/benchmark_integration.pyindex 9d5d40673c..d59bacdf4d 100644--- a/autogpt/benchmark_integration.py+++ b/autogpt/benchmark_integration.py@@ -5,7 +5,7 @@ from fastapi import ()from fastapi.responses import FileResponse-from autogpt.routes.agent_protocol import base_router+from autogpt.sdk.routes.agent_protocol import base_routerdef add_benchmark_routes():diff --git a/autogpt/routes/__init__.py b/autogpt/sdk/__init__.pysimilarity index 100%rename from autogpt/routes/__init__.pyrename to autogpt/sdk/__init__.pydiff --git a/autogpt/sdk/agent.py b/autogpt/sdk/agent.pynew file mode 100644index 0000000000..6204f7483d--- /dev/null+++ b/autogpt/sdk/agent.py@@ -0,0 +1,238 @@+import asyncio+import os++from fastapi import APIRouter, FastAPI, Response, UploadFile+from fastapi.responses import FileResponse+from hypercorn.asyncio import serve+from hypercorn.config import Config+from prometheus_fastapi_instrumentator import Instrumentator++from .db import AgentDB+from .errors import NotFoundError+from .forge_log import CustomLogger+from .middlewares import AgentMiddleware+from .routes.agent_protocol import base_router+from .schema import *+from .tracing import setup_tracing+from .utils import run+from .workspace import Workspace, load_from_uri++LOG = CustomLogger(__name__)+++class Agent:+ def __init__(self, database: AgentDB, workspace: Workspace):+ self.db = database+ self.workspace = workspace++ def start(self, port: int = 8000, router: APIRouter = base_router):+ """+ Start the agent server.+ """+ config = Config()+ config.bind = [f"localhost:{port}"]+ app = FastAPI(+ title="Auto-GPT Forge",+ description="Modified version of The Agent Protocol.",+ version="v0.4",+ )++ # Add Prometheus metrics to the agent+ # https://github.com/trallnag/prometheus-fastapi-instrumentator+ instrumentator = Instrumentator().instrument(app)++ @app.on_event("startup")+ async def _startup():+ instrumentator.expose(app)++ app.include_router(router)+ app.add_middleware(AgentMiddleware, agent=self)+ setup_tracing(app)+ config.loglevel = "ERROR"+ config.bind = [f"0.0.0.0:{port}"]++ LOG.info(f"Agent server starting on http://{config.bind[0]}")+ asyncio.run(serve(app, config))++ async def create_task(self, task_request: TaskRequestBody) -> Task:+ """+ Create a task for the agent.+ """+ try:+ task = await self.db.create_task(+ input=task_request.input,+ additional_input=task_request.additional_input,+ )+ return task+ except Exception as e:+ raise++ async def list_tasks(self, page: int = 1, pageSize: int = 10) -> TaskListResponse:+ """+ List all tasks that the agent has created.+ """+ try:+ tasks, pagination = await self.db.list_tasks(page, pageSize)+ response = TaskListResponse(tasks=tasks, pagination=pagination)+ return response+ except Exception as e:+ raise++ async def get_task(self, task_id: str) -> Task:+ """+ Get a task by ID.+ """+ try:+ task = await self.db.get_task(task_id)+ except Exception as e:+ raise+ return task++ async def list_steps(+ self, task_id: str, page: int = 1, pageSize: int = 10+ ) -> TaskStepsListResponse:+ """+ List the IDs of all steps that the task has created.+ """+ try:+ steps, pagination = await self.db.list_steps(task_id, page, pageSize)+ response = TaskStepsListResponse(steps=steps, pagination=pagination)+ return response+ except Exception as e:+ raise++ async def create_and_execute_step(+ self, task_id: str, step_request: StepRequestBody+ ) -> Step:+ """+ Create a step for the task.+ """+ if step_request.input != "y":+ step = await self.db.create_step(+ task_id=task_id,+ input=step_request,+ additional_input=step_request.additional_input,+ )+ # utils.run+ artifacts = run(step.input)+ for artifact in artifacts:+ art = await self.db.create_artifact(+ task_id=step.task_id,+ file_name=artifact["file_name"],+ uri=artifact["uri"],+ agent_created=True,+ step_id=step.step_id,+ )+ assert isinstance(+ art, Artifact+ ), f"Artifact not instance of Artifact {type(art)}"+ step.artifacts.append(art)+ step.status = "completed"+ else:+ steps, steps_pagination = await self.db.list_steps(+ task_id, page=1, per_page=100+ )+ # Find the latest step that has not been completed+ step = next((s for s in reversed(steps) if s.status != "completed"), None)+ if step is None:+ # If all steps have been completed, create a new placeholder step+ step = await self.db.create_step(+ task_id=task_id,+ input="y",+ additional_input={},+ )+ step.status = "completed"+ step.is_last = True+ step.output = "No more steps to run."+ step = await self.db.update_step(step)+ if isinstance(step.status, Status):+ step.status = step.status.value+ step.output = "Done some work"+ return step++ async def get_step(self, task_id: str, step_id: str) -> Step:+ """+ Get a step by ID.+ """+ try:+ step = await self.db.get_step(task_id, step_id)+ return step+ except Exception as e:+ raise++ async def list_artifacts(+ self, task_id: str, page: int = 1, pageSize: int = 10+ ) -> TaskArtifactsListResponse:+ """+ List the artifacts that the task has created.+ """+ try:+ artifacts, pagination = await self.db.list_artifacts(+ task_id, page, pageSize+ )+ response = TaskArtifactsListResponse(+ artifacts=artifacts, pagination=pagination+ )+ return Response(content=response.json(), media_type="application/json")+ except Exception as e:+ raise++ async def create_artifact(+ self,+ task_id: str,+ file: UploadFile | None = None,+ uri: str | None = None,+ ) -> Artifact:+ """+ Create an artifact for the task.+ """+ data = None+ if not uri:+ file_name = file.filename or str(uuid4())+ try:+ data = b""+ while contents := file.file.read(1024 * 1024):+ data += contents+ except Exception as e:+ raise+ else:+ try:+ data = await load_from_uri(uri, task_id)+ file_name = uri.split("/")[-1]+ except Exception as e:+ raise++ file_path = os.path.join(task_id / file_name)+ self.write(file_path, data)+ self.db.save_artifact(task_id, artifact)++ artifact = await self.create_artifact(+ task_id=task_id,+ file_name=file_name,+ uri=f"file://{file_path}",+ agent_created=False,+ )++ return artifact++ async def get_artifact(self, task_id: str, artifact_id: str) -> Artifact:+ """+ Get an artifact by ID.+ """+ try:+ artifact = await self.db.get_artifact(artifact_id)+ retrieved_artifact = await self.load_from_uri(artifact.uri, artifact_id)+ path = artifact.file_name+ with open(path, "wb") as f:+ f.write(retrieved_artifact)+ except NotFoundError as e:+ raise+ except FileNotFoundError as e:+ raise+ except Exception as e:+ raise+ return FileResponse(+ # Note: mimetype is guessed in the FileResponse constructor+ path=path,+ filename=artifact.file_name,+ )diff --git a/autogpt/agent_test.py b/autogpt/sdk/agent_test.pysimilarity index 98%rename from autogpt/agent_test.pyrename to autogpt/sdk/agent_test.pyindex b46fce48a3..85d2d66ee3 100644--- a/autogpt/agent_test.py+++ b/autogpt/sdk/agent_test.py@@ -13,6 +13,7 @@ def agent():return Agent(db, workspace)@pytest.mark.asyncioasync def test_create_task(agent):task_request = TaskRequestBody(@@ -22,6 +23,7 @@ async def test_create_task(agent):assert task.input == "test_input"@pytest.mark.asyncioasync def test_list_tasks(agent):task_request = TaskRequestBody(@@ -32,6 +34,7 @@ async def test_list_tasks(agent):assert isinstance(tasks, TaskListResponse)@pytest.mark.asyncioasync def test_get_task(agent):task_request = TaskRequestBody(diff --git a/autogpt/db.py b/autogpt/sdk/db.pysimilarity index 100%rename from autogpt/db.pyrename to autogpt/sdk/db.pydiff --git a/autogpt/db_test.py b/autogpt/sdk/db_test.pysimilarity index 98%rename from autogpt/db_test.pyrename to autogpt/sdk/db_test.pyindex 6c59c2a056..bcca3dd5c2 100644--- a/autogpt/db_test.py+++ b/autogpt/sdk/db_test.py@@ -4,7 +4,7 @@ from datetime import datetimeimport pytest-from autogpt.db import (+from autogpt.sdk.db import (AgentDB,ArtifactModel,StepModel,@@ -13,8 +13,8 @@ from autogpt.db import (convert_to_step,convert_to_task,)-from autogpt.errors import NotFoundError as DataNotFoundError-from autogpt.schema import *+from autogpt.sdk.errors import NotFoundError as DataNotFoundError+from autogpt.sdk.schema import *@pytest.mark.asyncio@@ -210,6 +210,7 @@ async def test_get_task_not_found():os.remove(db_name.split("///")[1])@pytest.mark.asyncioasync def test_create_and_get_step():db_name = "sqlite:///test_db.sqlite3"@@ -221,6 +222,7 @@ async def test_create_and_get_step():os.remove(db_name.split("///")[1])@pytest.mark.asyncioasync def test_updating_step():db_name = "sqlite:///test_db.sqlite3"@@ -243,6 +245,7 @@ async def test_get_step_not_found():os.remove(db_name.split("///")[1])@pytest.mark.asyncioasync def test_get_artifact():db_name = "sqlite:///test_db.sqlite3"@@ -290,6 +293,7 @@ async def test_list_tasks():os.remove(db_name.split("///")[1])@pytest.mark.asyncioasync def test_list_steps():db_name = "sqlite:///test_db.sqlite3"diff --git a/autogpt/errors.py b/autogpt/sdk/errors.pysimilarity index 100%rename from autogpt/errors.pyrename to autogpt/sdk/errors.pydiff --git a/autogpt/forge_log.py b/autogpt/sdk/forge_log.pysimilarity index 100%rename from autogpt/forge_log.pyrename to autogpt/sdk/forge_log.pydiff --git a/autogpt/llm.py b/autogpt/sdk/llm.pysimilarity index 100%rename from autogpt/llm.pyrename to autogpt/sdk/llm.pydiff --git a/autogpt/middlewares.py b/autogpt/sdk/middlewares.pysimilarity index 100%rename from autogpt/middlewares.pyrename to autogpt/sdk/middlewares.pydiff --git a/autogpt/sdk/routes/__init__.py b/autogpt/sdk/routes/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/autogpt/routes/agent_protocol.py b/autogpt/sdk/routes/agent_protocol.pysimilarity index 99%rename from autogpt/routes/agent_protocol.pyrename to autogpt/sdk/routes/agent_protocol.pyindex 8ee6fa2577..42362dd266 100644--- a/autogpt/routes/agent_protocol.py+++ b/autogpt/sdk/routes/agent_protocol.py@@ -28,10 +28,10 @@ from typing import Optionalfrom fastapi import APIRouter, Query, Request, Response, UploadFilefrom fastapi.responses import FileResponse-from autogpt.errors import *-from autogpt.forge_log import CustomLogger-from autogpt.schema import *-from autogpt.tracing import tracing+from autogpt.sdk.errors import *+from autogpt.sdk.forge_log import CustomLogger+from autogpt.sdk.schema import *+from autogpt.sdk.tracing import tracingbase_router = APIRouter()diff --git a/autogpt/schema.py b/autogpt/sdk/schema.pysimilarity index 100%rename from autogpt/schema.pyrename to autogpt/sdk/schema.pydiff --git a/autogpt/tracing.py b/autogpt/sdk/tracing.pysimilarity index 98%rename from autogpt/tracing.pyrename to autogpt/sdk/tracing.pyindex b1411e53cb..4c335a4633 100644--- a/autogpt/tracing.py+++ b/autogpt/sdk/tracing.py@@ -3,7 +3,7 @@ from functools import wrapsfrom dotenv import load_dotenv-from autogpt.forge_log import CustomLogger+from autogpt.sdk.forge_log import CustomLoggerload_dotenv()@@ -47,7 +47,7 @@ if ENABLE_TRACING:from opentelemetry.trace import NonRecordingSpanfrom pydantic import BaseModel- from autogpt.schema import Task+ from autogpt.sdk.schema import Tasktasks_context_db = {}diff --git a/autogpt/utils.py b/autogpt/sdk/utils.pysimilarity index 100%rename from autogpt/utils.pyrename to autogpt/sdk/utils.pydiff --git a/autogpt/workspace.py b/autogpt/sdk/workspace.pysimilarity index 100%rename from autogpt/workspace.pyrename to autogpt/sdk/workspace.pydiff --git a/autogpt/workspace_test.py b/autogpt/sdk/workspace_test.pysimilarity index 100%rename from autogpt/workspace_test.pyrename to autogpt/sdk/workspace_test.py