arcade-mcp/examples/flask/arcade_example_flask/app.py
Eric Gustin 53fa083efd
Add initial X toolkit, remove Github toolkit, rename math toolkit (#52)
* Renamed `arcade_arithmetic` to `arcade_math`
* Deleted `arcade_github` toolkit for the next release. This will be
reintroduced later.
* Added 5 tools to `arcade_x` toolkit
- post_tweet
- delete_tweet_by_id
- search_recent_tweets_by_username
- search_recent_tweets_by_keywords
- lookup_single_user_by_username
2024-09-23 13:42:22 -07:00

65 lines
1.8 KiB
Python

from flask import Flask, jsonify, request
from openai import OpenAI
from pydantic import BaseModel, ValidationError
from arcade_math.tools import arithmetic
from arcade.actor.flask.actor import FlaskActor
client = OpenAI(base_url="http://localhost:9099")
app = Flask(__name__)
actor = FlaskActor(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.route("/")
def hello_world():
return "Hello, World!"
@app.route("/chat", methods=["POST"])
def chat():
try:
# Parse JSON request body
req_data = request.get_json()
request_obj = ChatRequest(**req_data)
raw_response = client.chat.completions.with_raw_response.create(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": request_obj.message},
],
model="gpt-4o-mini",
max_tokens=150,
tools=["add", "subtract", "multiply", "divide", "sqrt"],
tool_choice="execute",
)
chat_completion = raw_response.parse()
return jsonify(
{
"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 ValidationError as e:
return jsonify({"error": e.errors()}), 422
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)