arcade-mcp/examples/example_nate/arcade_example_nate/main.py
Sam Partee 8964111023
Refactor into library approach (#7)
This PR makes a few sweeping changes to the actor, cli, and overall
structure of the project.

- CLI commands skeleton 
- ``arcade run``, ``arcade show``, and ``arcade new``
- Working package mangement solution (``arcade_`` packages)
- Actor approach for using frameworks other than FastAPI
- Client for calling Engine within ``arcade/core``
- beginning of the config interface.

---------

Co-authored-by: Nate Barbettini <nate@arcade-ai.com>
2024-07-23 16:26:54 -07:00

43 lines
1.3 KiB
Python

from arcade.actor.fastapi.actor import FastAPIActor
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import AsyncOpenAI
from arcade_example_nate.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,
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))