import asyncio from fastapi import APIRouter from fastapi.responses import StreamingResponse from app.deps import AuthDep from app.services.lightrag_service import lightrag_service router = APIRouter(prefix="/chat") @router.get("") async def chat(query: str, document_id: str, auth: AuthDep) -> StreamingResponse: # noqa: ARG001 """ SSE 流式占位。后续会调用 LightRAG + OpenAI。 当前直接返回 mock 文字,确保前端链路可用。 """ async def event_stream() -> asyncio.AsyncGenerator[str, None]: reply = await lightrag_service.query(query_text=query, user_id="placeholder-user") chunks = [reply[: len(reply) // 2 or 1], reply[len(reply) // 2 or 1 :]] for chunk in chunks: yield f"data: {chunk}\n\n" await asyncio.sleep(0.1) yield "data: [DONE]\n\n" return StreamingResponse(event_stream(), media_type="text/event-stream")