arcade-mcp/examples/fastapi/arcade_example_fastapi/main.py
Nate Barbettini c1a66a6170
SDK: Fully qualified tool names (#47)
In this PR:
- Handle and require fully-qualified tool names `Toolkit.ToolName` in
the actor

Also, unrelated changes/fixes:
- Cleaned up the logic around actor secrets and `$ARCADE_ACTOR_SECRET`
- Removes experimental Flask actor for now

Note: Must be merged along with
https://github.com/ArcadeAI/Engine/pull/87
2024-09-23 15:47:36 -07:00

52 lines
1.6 KiB
Python

import os
from arcade.core.toolkit import Toolkit
import arcade_math
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from arcade.actor.fastapi.actor import FastAPIActor
from arcade.client import AsyncArcade
from arcade.core.config import config
if not config.api or not config.api.key:
raise ValueError("Arcade API key not set. Please run `arcade login`.")
client = AsyncArcade(api_key=config.api.key)
app = FastAPI()
actor_secret = os.environ.get("ARCADE_ACTOR_SECRET")
actor = FastAPIActor(app, secret=actor_secret)
actor.register_toolkit(Toolkit.from_module(arcade_math))
class ChatRequest(BaseModel):
message: str
@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=[
# "Google.GetEmails",
# "Google.SearchEmailsByHeader",
# "Google.WriteDraft",
# "GitHub.CountStargazers",
# "GitHub.SetStarred",
# "GitHub.SearchIssues",
# "Slack.SendDmToUser",
# "Slack.SendMessageToChannel",
],
tool_choice=tool_choice,
user=config.user.email if config.user else None,
)
return raw_response.choices
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))