Download app/routers/chat.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/routers/chat.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/routers/chat.py
-
curl -L -o chat.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/routers/chat.py
5.29 kB
| 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": "", | |
| } | |
| 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, | |
| ) | |
| 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", | |
| }, | |
| ) | |