052591281d
- Complete agent system with planning and execution - Bridge API with FastAPI server - WhatsApp-style mobile dashboard - Enhanced web search with query improvement - Security guards and policy checks - Comprehensive documentation
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
"""Chat API routes — user-facing conversation endpoint.
|
|
|
|
POST /chat
|
|
Accepts a user message, generates a CJK-safe Russian reply via Ollama.
|
|
This is the ONLY path through which LLM text reaches a user.
|
|
All output is hardened by chat.guard before being returned.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from chat.responder import generate_reply
|
|
from models.schemas import ChatRequest, ChatResponse
|
|
|
|
router = APIRouter(prefix="/chat", tags=["chat"])
|
|
|
|
|
|
@router.post("", response_model=ChatResponse, status_code=200)
|
|
def chat(body: ChatRequest) -> ChatResponse:
|
|
"""Generate a CJK-safe reply for the given user message.
|
|
|
|
- LLM output is scanned for CJK before being returned.
|
|
- On detection: one retry with strict Russian-only instruction.
|
|
- On second failure: safe Russian fallback returned.
|
|
- Diagnostic artifact saved to data/diagnostics/chat_cjk_<trace_id>.json on any CJK incident.
|
|
"""
|
|
trace_id = body.trace_id or f"chat-{str(uuid.uuid4())[:8]}"
|
|
reply, meta = generate_reply(user_message=body.message, trace_id=trace_id)
|
|
return ChatResponse(
|
|
reply=reply,
|
|
cjk_detected=meta["cjk_detected"],
|
|
fallback_used=meta["fallback_used"],
|
|
model=meta["model"],
|
|
trace_id=trace_id,
|
|
)
|