arcade-mcp/examples/math/arcade_arithmetic/main.py
Nate Barbettini 1b67cee667
JWT auth for Engine->Actor communication (#11)
Implements:
https://app.clickup.com/9014390315/v/dc/8cmtbhb-2714/8cmtbhb-5974

Todo:
- [x] Initial demo
- [x] Get API key config from `arcade.config`
- [x] Get engine URL from config
- [x] Final cleanup
- [ ] Enforce auth for all requests (waiting for engine)
2024-08-01 09:14:37 -07:00

45 lines
1.4 KiB
Python

from arcade.actor.fastapi.actor import FastAPIActor
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import AsyncOpenAI
from tools import arithmetic
client = AsyncOpenAI(base_url="http://localhost:6901")
app = FastAPI()
actor = FastAPIActor(app)
actor.register_tool(arithmetic.add)
actor.register_tool(arithmetic.multiply)
actor.register_tool(arithmetic.divide)
actor.register_tool(arithmetic.sqrt)
class ChatRequest(BaseModel):
message: str
@app.post("/chat")
async def chat(request: ChatRequest):
try:
raw_response = await client.chat.completions.with_raw_response.create(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": request.message},
],
model="gpt-4o-mini",
max_tokens=150,
tools=["add", "subtract", "multiply", "divide", "sqrt"],
tool_choice="execute",
)
chat_completion = raw_response.parse()
return {
"response": chat_completion.choices[0].message.content.strip(),
"tool_call_count": raw_response.headers["arcade-tool-calls"],
"tool_call_duration_ms": raw_response.headers["arcade-total-tool-duration"],
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))