Build an Arcade AI actor with FastAPI
In this guide, we'll walk through the process of building an actor with FastAPI that can serve tools from a pre-built toolkit.
Prerequisites
-
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 an Actor to serve the tools from the installed toolkit.
-
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.actor.fastapi.actor import FastAPIActor from arcade.sdk import Toolkit from arcadepy import AsyncArcade import arcade_math
-
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`
-
Initialize the Arcade Client
client = AsyncArcade()
-
Initialize the FastAPI Application and Actor
app = FastAPI() actor_secret = os.environ.get("ARCADE_ACTOR_SECRET") actor = FastAPIActor(app, secret=actor_secret)
-
Register the Toolkit with the Actor
actor.register_toolkit(Toolkit.from_module(arcade_math))
-
Create a Pydantic Model for Chat Requests
class ChatRequest(BaseModel): message: str user_id: str
-
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_ACTOR_SECRET
environment variable set for authentication:
export ARCADE_ACTOR_SECRET="your_actor_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 Actor: The
FastAPIActor
serves your tools over HTTP, allowing them to be called by the Arcade Engine. - Toolkit Registration: By registering the toolkit with the Actor, 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
orarcade-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
andmax_tokens
.