ai-hello-world / api.py
SubZtep's picture
feat: flexible modal message handling
47b302d
raw
history blame contribute delete
666 Bytes
from fastapi import FastAPI
from pydantic import BaseModel
from ai_engine import generate
app = FastAPI(title="Hello World AI")
# VERY simple in-memory store - now storing tuples (user_msg, assistant_msg)
sessions: dict[str, list[tuple[str, str]]] = {}
class ChatRequest(BaseModel):
session_id: str
message: str
class ChatResponse(BaseModel):
reply: str
@app.post("/chat", response_model=ChatResponse)
def chat(req: ChatRequest):
history = sessions.setdefault(req.session_id, [])
reply = generate(req.message, history)
# Store as tuple (user_msg, assistant_msg)
history.append((req.message, reply))
return {"reply": reply}