File size: 5,288 Bytes
481dfe6 | 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import json
import logging
import uuid
from typing import AsyncIterator
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from langchain_core.messages import HumanMessage
from app.agent.graph import get_graph
from app.memory.session_store import get_or_create_session
from app.monitoring.langfuse_setup import get_langfuse_callback_handler
from app.schemas.chat import ChatRequest, ChatResponse, Source
from app.config import settings
router = APIRouter(prefix="/api", tags=["chat"])
logger = logging.getLogger(__name__)
def _build_initial_state(query: str, history_messages: list) -> dict:
"""Build the initial AgentState for graph.invoke()."""
return {
"messages": history_messages + [HumanMessage(content=query)],
"query": query,
"retrieved_documents": [],
"relevant_documents": [],
"generation": "",
"sources": [],
"confidence": 0.0,
"retry_count": 0,
"route": "",
}
@router.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
"""Main chat endpoint — runs the full agentic RAG pipeline."""
session_id, buffer = get_or_create_session(request.session_id)
history = buffer.get_messages()
# Build LangGraph config with Langfuse callback for monitoring
# Langfuse will trace the entire graph execution and log metrics
handler = get_langfuse_callback_handler()
config = {"callbacks": [handler]} if handler else {}
try:
graph = get_graph()
initial_state = _build_initial_state(request.message, history)
result = await graph.ainvoke(initial_state, config=config)
except Exception as e:
logger.exception("Graph invocation error: %s", e)
raise HTTPException(status_code=500, detail=f"Internal error: {str(e)}")
answer = result.get("generation", settings.fallback_response)
sources_raw = result.get("sources", [])
confidence = float(result.get("confidence", 0.0))
# Update session memory
buffer.add_user_message(request.message)
buffer.add_ai_message(answer)
sources = [Source(**s) for s in sources_raw]
return ChatResponse(
answer=answer,
sources=sources,
session_id=session_id,
confidence=confidence,
)
@router.post("/chat/stream")
async def chat_stream(request: ChatRequest):
"""SSE streaming chat endpoint — streams tokens as they are generated."""
session_id, buffer = get_or_create_session(request.session_id)
history = buffer.get_messages()
# Create Langfuse callback handler for monitoring the streaming request
handler = get_langfuse_callback_handler()
config = {"callbacks": [handler]} if handler else {}
async def event_generator() -> AsyncIterator[str]:
try:
graph = get_graph()
initial_state = _build_initial_state(request.message, history)
# Stream events from the graph
final_answer = ""
final_sources = []
final_confidence = 0.0
async for event in graph.astream_events(initial_state, config=config, version="v2"):
kind = event.get("event", "")
# Stream LLM tokens from the generate node
if kind == "on_chat_model_stream":
chunk = event.get("data", {}).get("chunk")
if chunk and hasattr(chunk, "content") and chunk.content:
token = chunk.content
final_answer += token
yield f"event: token\ndata: {json.dumps({'token': token})}\n\n"
# Capture final state on graph completion
elif kind == "on_chain_end" and event.get("name") == "LangGraph":
output = event.get("data", {}).get("output", {})
if isinstance(output, dict):
final_sources = output.get("sources", [])
final_confidence = float(output.get("confidence", 0.0))
gen = output.get("generation", "")
if gen and not final_answer:
final_answer = gen
yield f"event: token\ndata: {json.dumps({'token': gen})}\n\n"
# Update session memory after full response
buffer.add_user_message(request.message)
buffer.add_ai_message(final_answer or settings.fallback_response)
# Send sources and done events
yield f"event: sources\ndata: {json.dumps({'sources': final_sources})}\n\n"
yield f"event: done\ndata: {json.dumps({'session_id': session_id, 'confidence': final_confidence})}\n\n"
except Exception as e:
logger.exception("Streaming error: %s", e)
error_payload = json.dumps({"error": str(e)})
yield f"event: error\ndata: {error_payload}\n\n"
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
|