Skip to content

Commit

Permalink
Agent Skeleton (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftyos committed Aug 18, 2023
1 parent 8b44ecb commit 75b80ff
Show file tree
Hide file tree
Showing 22 changed files with 1,512 additions and 177 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,5 @@ CURRENT_BULLETIN.md

# agbenchmark

agbenchmark/reports
agbenchmark/workspace
agbenchmark
agent.db
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: update-protocol

update-protocol:
@if [ -d "../agent-protocol/sdk/python/agent_protocol" ]; then \
cp -r ../agent-protocol/sdk/python/agent_protocol autogpt; \
rm -Rf autogpt/agent_protocol/utils; \
rm -Rf autogpt/agent_protocol/cli.py; \
echo "Protocol updated successfully!"; \
else \
echo "Error: Source directory ../agent-protocol/sdk/python/agent_protocol does not exist."; \
exit 1; \
fi

change-protocol:
@if [ -d "autogpt/agent_protocol" ]; then \
cp -r autogpt/agent_protocol ../agent-protocol/sdk/python; \
rm ../agent-protocol/sdk/python/agent_protocol/README.md; \
echo "Protocol reversed successfully!"; \
else \
echo "Error: Target directory autogpt/agent_protocol does not exist."; \
exit 1; \
fi
10 changes: 4 additions & 6 deletions autogpt/__main__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import os

from agent_protocol import Agent
from dotenv import load_dotenv

import autogpt.agent
import autogpt.db
from autogpt.benchmark_integration import add_benchmark_routes
from autogpt.workspace import LocalWorkspace

if __name__ == "__main__":
"""Runs the agent server"""
load_dotenv()
router = add_benchmark_routes()

database_name = os.getenv("DATABASE_STRING")
workspace = LocalWorkspace(os.getenv("AGENT_WORKSPACE"))
print(database_name)
port = os.getenv("PORT")
workspace = os.getenv("AGENT_WORKSPACE")
auto_gpt = autogpt.agent.AutoGPT()

database = autogpt.db.AgentDB(database_name)
agent = Agent.setup_agent(auto_gpt.task_handler, auto_gpt.step_handler)
agent.db = database
agent.workspace = workspace
agent = autogpt.agent.AutoGPT(db=database, workspace=workspace)

agent.start(port=port, router=router)
78 changes: 55 additions & 23 deletions autogpt/agent.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,64 @@
import time

from agent_protocol import Agent, Step, Task
import os

import autogpt.utils
from autogpt.agent_protocol import Agent, Artifact, Step, Task, TaskDB

from .workspace import Workspace

class AutoGPT:
def __init__(self) -> None:
pass

async def task_handler(self, task: Task) -> None:
print(f"task: {task.input}")
await Agent.db.create_step(task.task_id, task.input, is_last=True)
time.sleep(2)
class AutoGPT(Agent):
def __init__(self, db: TaskDB, workspace: Workspace) -> None:
super().__init__(db)
self.workspace = workspace

# autogpt.utils.run(task.input) the task_handler only creates the task, it doesn't execute it
# print(f"Created Task id: {task.task_id}")
async def create_task(self, task: Task) -> None:
print(f"task: {task.input}")
return task

async def step_handler(self, step: Step) -> Step:
# print(f"step: {step}")
agent_step = await Agent.db.get_step(step.task_id, step.step_id)
updated_step: Step = await Agent.db.update_step(
agent_step.task_id, agent_step.step_id, status="completed"
)
updated_step.output = agent_step.input
if step.is_last:
print(f"Task completed: {updated_step.task_id}")
async def run_step(self, step: Step) -> Step:
artifacts = autogpt.utils.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 isntance of Artifact {type(art)}"
step.artifacts.append(art)
step.status = "completed"
return step

async def retrieve_artifact(self, task_id: str, artifact: Artifact) -> bytes:
"""
Retrieve the artifact data from wherever it is stored and return it as bytes.
"""
if not artifact.uri.startswith("file://"):
raise NotImplementedError("Loading from uri not implemented")
file_path = artifact.uri.split("file://")[1]
if not self.workspace.exists(file_path):
raise FileNotFoundError(f"File {file_path} not found in workspace")
return self.workspace.read(file_path)

async def save_artifact(
self, task_id: str, artifact: Artifact, data: bytes
) -> Artifact:
"""
Save the artifact data to the agent's workspace, loading from uri if bytes are not available.
"""
assert (
data is not None and artifact.uri is not None
), "Data or Artifact uri must be set"

if data is not None:
file_path = os.path.join(task_id / artifact.file_name)
self.write(file_path, data)
artifact.uri = f"file://{file_path}"
self.db.save_artifact(task_id, artifact)
else:
print(f"Step completed: {updated_step}")
return updated_step
raise NotImplementedError("Loading from uri not implemented")

return artifact
21 changes: 21 additions & 0 deletions autogpt/agent_protocol/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Autogpt Protocol Directory

# DO NOT MODIFY ANY FILES IN THIS DIRECTORY

This directory contains protocol definitions crucial for our project. The current setup is a temporary measure to allow for speedy updating of the protocol.

## Background

In an ideal scenario, we'd directly use a submodule pointing to the original repository. However, given our specific needs and to expedite our development process, we've chosen a slightly different approach.

## Process

1. **Fork and Clone**: We started by forking the original repository `e2b-dev/agent-protocol` (not `Swiftyos/agent-protocol` as previously mentioned) to have our own version. This allows us to have more control over updates and possibly any specific changes that our project might need in the future.

2. **Manual Content Integration**: Instead of adding the entire forked repository as a submodule, we've manually copied over the contents of `sdk/python/agent_protocol` into this directory. This ensures we only have the parts we need, without any additional overhead.

3. **Updates**: Any necessary updates to the protocol can be made directly in our fork, and subsequently, the required changes can be reflected in this directory.

## Credits

All credit for the original protocol definitions goes to [e2b-dev/agent-protocol](https://github.com/e2b-dev/agent-protocol). We deeply appreciate their efforts in building the protocol, and this temporary measure is in no way intended to diminish the significance of their work. It's purely a practical approach for our specific requirements at this point in our development phase.
16 changes: 16 additions & 0 deletions autogpt/agent_protocol/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .agent import Agent
from .agent import base_router as router
from .db import Step, Task, TaskDB
from .models import Artifact, Status, StepRequestBody, TaskRequestBody

__all__ = [
"Agent",
"Artifact",
"Status",
"Step",
"StepRequestBody",
"Task",
"TaskDB",
"TaskRequestBody",
"router",
]
Loading

0 comments on commit 75b80ff

Please sign in to comment.