1

Install Required Packages

First, install all necessary packages:

pip install composio_core composio_openai
pip install fastapi uvicorn pydantic
2

Set Up Environment Variables

Configure your API keys:

export COMPOSIO_API_KEY=<your-composio-api-key>
export OPENAI_API_KEY=<your-openai-api-key>

Replace the API keys with your actual Composio and OpenAI API keys.

3

Authenticate GitHub Account

composio add github # Launches GitHub login

If you haven’t authenticated your GitHub account, complete the authentication process.

4

Set up FastAPI Imports

Create a new file main.py and add the necessary imports:

from fastapi import FastAPI
from pydantic import BaseModel
from composio_openai import ComposioToolSet, App
from openai import OpenAI

# Initialize FastAPI app
app = FastAPI()
5

Create Request Model

Define the Pydantic model for request validation:

class TaskRequest(BaseModel):
    task: str  # This will contain the natural language task description

Pydantic ensures that incoming requests contain a valid task field.

6

Initialize API Clients

Set up the OpenAI and Composio clients:

# Initialize clients
openai_client = OpenAI()
composio_toolset = ComposioToolSet()
tools = composio_toolset.get_tools(apps=[App.GITHUB])

This step prepares the tools needed for GitHub interactions.

7

Create API Endpoint

Add the endpoint that will process tasks:

@app.post("/execute_task")
async def execute_task(request: TaskRequest):
    response = openai_client.chat.completions.create(
        model="gpt-4o-mini",
        tools=tools,
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": request.task},
        ],
    )
    
    result = composio_toolset.handle_tool_calls(response)
    return {"result": result}

This endpoint combines OpenAI’s language understanding with Composio’s GitHub tools to process natural language requests.

8

Run the Server

Start your FastAPI server:

uvicorn main:app --reload

The --reload flag enables auto-reload during development. Remove it in production.

9

Test the Endpoint

Test your endpoint using curl:

curl -X POST \
  http://localhost:8000/execute_task \
  -H "Content-Type: application/json" \
  -d '{"task": "Star the repo composiohq/composio on GitHub"}'
10

Use specific actions (Optional)

You can use specific actions by passing the action IDs while fetching tools. It’s recommended to limit the number of actions to 20 or fewer for optimal performance and clearer AI responses:

tools = composio_toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER])