yoheinakajima / babyagi

MIT License
19.64k stars 2.57k forks source link

Add API mode #356

Open jakubno opened 11 months ago

jakubno commented 11 months ago

API mode

This PR adds an API to the babyagi by using the e2b agent protocol SDK that implements the Agent Communication Protocol.

Here is a short preview how to use the API that I also added to the README

To start the server run (from classic directory):

python api.py

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

To create a task run:

curl --request POST \
  --url http://localhost:8000/agent/tasks \
  --header 'Content-Type: application/json' \
  --data '{
    "input": "Find the Answer to the Ultimate Question of Life, the Universe, and Everything."
}'

You will get a response like this:

{"input":"Find the Answer to the Ultimate Question of Life, the Universe, and Everything.","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:

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

or you can use Python client library:

from agent_protocol_client import AgentApi, ApiClient, TaskRequestBody

...

prompt = "Find the Answer to the Ultimate Question of Life, the Universe, and Everything."

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)

...