Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
104 lines (76 loc) · 2.93 KB
/
Copy pathserver.py
File metadata and controls
104 lines (76 loc) · 2.93 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
server.py — Phase 2 backend service for the CURT inventory assistant.
FastAPI server exposing:
POST /chat → {session_id, message} → {session_id, reply}
GET /inventory → current inventory as JSON
GET /health → liveness check
The LLM (Groq gpt-oss-20b) decides which tools to call; tools are executed
against the real SQLite database. Conversation memory is kept per session.
Run with:
.venv/Scripts/python.exe -m uvicorn server:app --reload --port 8000
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import database as db
import llm_assistant
@asynccontextmanager
async def lifespan(app: FastAPI):
# Make sure the database exists and is seeded before serving requests
db.init_db()
db.seed_db()
yield
app = FastAPI(
title="CURT Inventory Assistant API",
description="LLM-powered inventory assistant with function calling (Phase 2).",
version="2.0.0",
lifespan=lifespan,
)
# Allow the Streamlit app (and local tools) to call the API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# --------------------------------------------------------------------------
# Schemas
# --------------------------------------------------------------------------
class ChatRequest(BaseModel):
session_id: str = Field(..., min_length=1, description="Conversation/session id")
message: str = Field(..., min_length=1, description="The user's question")
class ChatResponse(BaseModel):
session_id: str
reply: str
# --------------------------------------------------------------------------
# Endpoints
# --------------------------------------------------------------------------
@app.post("/chat", response_model=ChatResponse)
def chat(req: ChatRequest) -> ChatResponse:
"""Send a user message; get the assistant's reply. Memory is per session_id."""
try:
reply = llm_assistant.chat(req.session_id, req.message)
except Exception as exc:
raise HTTPException(status_code=502, detail=f"LLM backend error: {exc}") from exc
return ChatResponse(session_id=req.session_id, reply=reply)
@app.get("/inventory")
def inventory() -> dict:
"""Return the current inventory as JSON."""
parts = db.get_all_parts()
return {
"count": len(parts),
"total_units": sum(p["quantity"] for p in parts),
"parts": parts,
}
@app.get("/health")
def health() -> dict:
return {"status": "ok", "model": llm_assistant.MODEL}
@app.post("/reset/{session_id}")
def reset(session_id: str) -> dict:
"""Forget a session's conversation history (handy while demoing)."""
llm_assistant.reset_session(session_id)
return {"session_id": session_id, "reset": True}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)