-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi_app.py
More file actions
82 lines (62 loc) · 2.18 KB
/
Copy pathfastapi_app.py
File metadata and controls
82 lines (62 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
from fastapi import FastAPI, HTTPException
from openai import OpenAI
from dotenv import load_dotenv
from ai_token_monitor import TokenMonitor, ChatManager
from ai_token_monitor.utils import normalize_response, extract_reply
load_dotenv() # Reads .env file
app = FastAPI(title="AI Token Monitor Demo")
monitor = TokenMonitor()
chat_manager = ChatManager()
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"], # Set in .env, never hardcode
)
MODEL = "openai/gpt-4o-mini"
@app.post("/chat/start")
def start_chat(user_id: str):
"""Create a new chat session for a user."""
chat_id = chat_manager.create_chat(user_id)
return {"chat_id": chat_id}
@app.post("/chat/message")
def chat(chat_id: str, message: str):
"""Send a message and get a tracked response."""
try:
chat_manager.add_message(chat_id, "user", message)
except KeyError:
raise HTTPException(status_code=404, detail=f"Chat ID '{chat_id}' not found.")
# Call the LLM with full message history
raw_response = client.chat.completions.create(
model=MODEL,
messages=chat_manager.get_messages(chat_id),
)
# Normalize Pydantic → dict (fixes the original bug)
data = normalize_response(raw_response)
# Safely extract the reply text
answer = extract_reply(data)
# Store assistant reply
chat_manager.add_message(chat_id, "assistant", answer)
# Track token usage and cost
usage_log = monitor.track(
data,
model=MODEL,
chat_manager=chat_manager,
chat_id=chat_id,
)
return {
"answer": answer,
"usage": usage_log,
"chat_summary": chat_manager.summary(chat_id),
"global_summary": monitor.summary(),
}
@app.get("/monitor/summary")
def global_summary():
"""Get total tokens and cost across all requests."""
return monitor.summary()
@app.get("/chat/{chat_id}/summary")
def chat_summary(chat_id: str):
"""Get usage summary for a specific chat session."""
try:
return chat_manager.summary(chat_id)
except KeyError:
raise HTTPException(status_code=404, detail=f"Chat ID '{chat_id}' not found.")