arcade-mcp/examples/fastapi/arcade_example_fastapi/main.py
Sam Partee 35baaf0dc8
Pass ToolContext and CLI cleanup (#13)
Added
-  `arcade dev` - serves a simple fastapi actor
- `arcade config` - show/edit/change config in `~/.arcade`
- `arcade chat` - chat with LLM without toolcalls

Changed:
- `arcade show`, `arcade run` - can now use all installed toolkits

---------

Co-authored-by: Nate Barbettini <nate@arcade-ai.com>
2024-08-13 15:40:08 -07:00

43 lines
1.3 KiB
Python

from fastapi import FastAPI, HTTPException
from openai import AsyncOpenAI
from pydantic import BaseModel
from arcade_arithmetic.tools import arithmetic
from arcade_gmail.tools import gmail
from arcade.actor.fastapi.actor import FastAPIActor
client = AsyncOpenAI(base_url="http://localhost:9099/v1")
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)
actor.register_tool(gmail.get_emails)
class ChatRequest(BaseModel):
message: str
@app.post("/chat")
async def chat(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=150,
# TODO tests for tool choice
tools=["Add", "Multiply", "Divide", "Sqrt", "GetEmails"],
tool_choice=tool_choice,
user="sam",
)
return raw_response.choices
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))