Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API via e2b SDK #123

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 179 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ python = "^3.11"
openai = "^0.27.8"
openai-function-call = "^0.0.5"
tenacity = "^8.2.2"
agent-protocol = "^0.1.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
src = "src.__main__:main"
api = "smol_dev.api:main"

[project.urls]
"Homepage" = "https://github.com/smol-ai/developer"
Expand Down
56 changes: 56 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,63 @@ for file_path in file_paths:
# there is also an async `generate_code()` version of this
```

### In API mode (via [e2b](https://www.e2b.dev/))
To start the server run:
```bash
poetry run api
```
or
```bash
python smol_dev/api.py
```

and then you can call the API using either the following commands:

To **create a task** run:
```bash
curl --request POST \
--url http://localhost:8000/agent/tasks \
--header 'Content-Type: application/json' \
--data '{
"input": "Write simple script in Python. It should write '\''Hello world!'\'' to hi.txt"
}'
```

You will get a response like this:
```json
{"input":"Write simple script in Python. It should write 'Hello world!' to hi.txt","task_id":"d2c4e543-ae08-4a97-9ac5-5f9a4459cb19","artifacts":[]}
```

Then to **execute one step of the task** copy the `task_id` you got from the previous request and run:

```bash
curl --request POST \
--url http://localhost:8000/agent/tasks/<task-id>/steps
```

or you can use [Python client library](https://github.com/e2b-dev/agent-protocol/tree/main/agent_client/python):

```python
from agent_protocol_client import AgentApi, ApiClient, TaskRequestBody

...

prompt = "Write simple script in Python. It should write 'Hello world!' to hi.txt"

async with ApiClient() as api_client:
# Create an instance of the API class
api_instance = AgentApi(api_client)
task_request_body = TaskRequestBody(input=prompt)

task = await api_instance.create_agent_task(
task_request_body=task_request_body
)
task_id = task.task_id
response = await api_instance.execute_agent_task_step(task_id=task_id)

...

```

## examples/prompt gallery

Expand Down
37 changes: 37 additions & 0 deletions smol_dev/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from smol_dev.prompts import plan, specify_file_paths, generate_code

from agent_protocol import (
Agent,
StepResult,
StepHandler,
)


async def smol_developer(prompt: str):
shared_deps = plan(prompt)
yield shared_deps

file_paths = specify_file_paths(prompt, shared_deps)
yield file_paths

for file_path in file_paths:
code = await generate_code(prompt, shared_deps, file_path)
yield code


async def task_handler(task_input) -> StepHandler:
if not task_input:
raise Exception("No task prompt")

smol_developer_loop = smol_developer(prompt=task_input)

async def step_handler(step_input):
result = await anext(smol_developer_loop, None)
if result is None:
return StepResult(is_last=True)
return StepResult(output=result)

return step_handler


Agent.handle_task(task_handler).start()