Home
Serve tools
Build a worker with FastAPI

Build an Arcade AI worker with FastAPI

In this guide, we'll walk through the process of building a worker with FastAPI that can serve tools from a pre-built toolkit.

Prerequisites

  • Set up Arcade AI

  • Install the Arcade AI SDK and any necessary dependencies:

    pip install arcade-ai fastapi pydantic

Install a Pre-Built Toolkit

Arcade AI offers a variety of pre-built toolkits that you can install and use immediately. For this guide, we'll use the arcade-math toolkit as an example.

  • Install the arcade-math toolkit:

    pip install arcade-math

Create a FastAPI Application to Serve the Toolkit

We'll set up a FastAPI application that acts as a Worker to serve the tools from the installed toolkit.

  1. Create a new Python file (e.g., main.py) and import the necessary modules:

    import os
     
    from fastapi import FastAPI, HTTPException
    from pydantic import BaseModel
     
    from arcade.worker.fastapi.worker import FastAPIWorker
    from arcade.sdk import Toolkit
    from arcadepy import AsyncArcade
    import arcade_math
  2. Ensure Arcade API Key is Set

    The Arcade API key is automatically loaded from the ARCADE_API_KEY environment variable. If you haven't set this yet, you can do so by running:

    export ARCADE_API_KEY="your_api_key"   # On Windows use `set`
  3. Initialize the Arcade Client

    client = AsyncArcade()
  4. Initialize the FastAPI Application and Worker

    app = FastAPI()
     
    worker_secret = os.environ.get("ARCADE_WORKER_SECRET")
    worker = FastAPIWorker(app, secret=worker_secret)
  5. Register the Toolkit with the Worker

    worker.register_toolkit(Toolkit.from_module(arcade_math))
  6. Create a Pydantic Model for Chat Requests

    class ChatRequest(BaseModel):
        message: str
        user_id: str
  7. Define the Chat Endpoint

    @app.post("/chat")
    async def postChat(request: ChatRequest, tool_choice: str = "execute"):
        try:
            raw_response = await client.chat.completions.create(
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": request.message},
                ],
                model="gpt-4o-mini",
                max_tokens=500,
                tools=[],  # List your tools here if needed
                tool_choice=tool_choice,
                user=request.user_id,
            )
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))
        else:
            return raw_response.choices

Run Your Application

Ensure you have the ARCADE_WORKER_SECRET environment variable set for authentication:

export ARCADE_WORKER_SECRET="your_worker_secret_key"  # On Windows use `set`

Start your FastAPI application using Uvicorn:

uvicorn main:app --host 127.0.0.1 --port 8000

Test the Setup

You can now send a POST request to the /chat endpoint with a message, and the assistant will respond using the tools from the arcade-math toolkit.

Example request using curl:

curl -X POST "http://127.0.0.1:8000/chat" \
     -H "Content-Type: application/json" \
     -d '{"message": "What is the square root of 16?", "user_id": "[email protected]"}'

Customize the Tools (Optional)

If you wish to specify which tools to make available in the assistant's responses, modify the tools parameter in the postChat function:

tools = ["Math.Sqrt"]  # Replace with the actual tool names

How It Works

  • Arcade Worker: The FastAPIWorker serves your tools over HTTP, allowing them to be called by the Arcade Engine.
  • Toolkit Registration: By registering the toolkit with the Worker, you make all tools within arcade_math available to your application.
  • Arcade Client: The AsyncArcade client is used to interact with the Arcade AI services, sending chat completions and making use of the tools.
  • Chat Endpoint: The /chat endpoint accepts user messages and processes them using the AI assistant, leveraging the tools provided.

Next Steps

  • Explore Other Toolkits: Install other pre-built toolkits like arcade-search or arcade-slack to add more functionality.
  • Build Custom Tools: When you're ready, learn how to create your own custom toolkits to extend your application's capabilities.
  • Enhance the Assistant: Customize the assistant's behavior by modifying the system prompt or adjusting parameters like model and max_tokens.