arcade-mcp/examples/fastapi/arcade_example_fastapi/main.py
Nate Barbettini 3699b16b2d
fix: circular import in arcade.core (#137)
Fixes a circular import issue where `arcade.sdk -> arcade.core` but also
`arcade.core -> arcade.sdk`. My mistake!

Moved some of the shared classes down into `core`, and re-exported them
to `sdk` to keep the expected interface for devs.
2024-11-01 12:38:44 -07:00

51 lines
1.4 KiB
Python

import os
import arcade_math
from fastapi import FastAPI, HTTPException
from openai import AsyncOpenAI
from pydantic import BaseModel
from arcade.actor.fastapi.actor import FastAPIActor
from arcade.sdk import Toolkit
client = AsyncOpenAI(api_key=os.environ["ARCADE_API_KEY"], base_url="http://localhost:9099/v1")
app = FastAPI()
actor_secret = os.environ["ARCADE_ACTOR_SECRET"]
actor = FastAPIActor(app, secret=actor_secret)
actor.register_toolkit(Toolkit.from_module(arcade_math))
class ChatRequest(BaseModel):
message: str
user_id: str | None = None
@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=[
"Math.Add",
"Math.Subtract",
"Math.Multiply",
"Math.Divide",
"Math.Sqrt",
# Other tools can be added as needed:
# "Math.SumList"
],
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