Skip to content

Serving an Agent as an Endpoint¤

This guide shows you how to publish an agent as an HTTP endpoint using Crewmaster's http_driver.

1. Install the HTTP Driver¤

First, ensure you have the necessary dependencies by installing Crewmaster.

pip install 'crewmaster'

2. Create the Agent and Crew¤

We'll use a simple agent from a previous tutorial. The http_driver serves a Crew instance, so we need to wrap our agent within a Team and a Crew.

# api_app.py

from crewmaster.agent import AgentBase
from crewmaster.crew import Crew
from crewmaster.team import Team
from crewmaster.collaborator import CollaboratorBase
from langchain_openai import ChatOpenAI

# 1. Define the Agent
class GreetingAgent(AgentBase):
    job_description = "You are a friendly assistant that greets users."

    def __init__(self, llm_service: ChatOpenAI):
        super().__init__(llm_service=llm_service)

# 2. Instantiate the Agent
llm_service = ChatOpenAI(model="gpt-3.5-turbo")
greeting_agent = GreetingAgent(llm_service=llm_service)

# 3. Create a Team and add the agent
greeting_team = Team(name="Greeting Team", members=[greeting_agent])

# 4. Create the Crew and add the team
my_crew = Crew(name="Greeting Crew", teams=[greeting_team])

3. Create the Endpoint¤

Now, we use the http_driver to create a FastAPI application that serves our crew.

# api_app.py

# ... (previous code) ...

from crewmaster.http_driver import create_app

# 5. Create the FastAPI app from the crew
app = create_app(my_crew)

4. Run the Server¤

To start the server, use uvicorn. The api_app:app syntax tells uvicorn to run the app object from the api_app.py file.

uvicorn api_app:app

The server will start, typically on http://127.0.0.1:8000. You can now access the automatically generated API documentation at /docs.

5. Test the Endpoint¤

You can test the endpoint using curl or any API client. Send a POST request to the /api/invoke endpoint with a JSON body containing your input.

curl -X POST "[http://127.0.0.1:8000/api/invoke](http://127.0.0.1:8000/api/invoke)" \
-H "Content-Type: application/json" \
-d '{
  "query": "Hello, can you greet me?"
}'

The response will be a JSON object containing the agent's output. The http_driver handles the complex async invocation and response formatting for you.