diff --git a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py index 0fbaf87..9b68d83 100644 --- a/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py +++ b/ruoyi-fastapi-backend/module_admin/controller/ragflow_controller.py @@ -92,7 +92,13 @@ async def converse_with_chat_assistant( if converse_params.stream: return StreamingResponse( stream_ragflow_response(result, converse_params.chat_id, start_time), - media_type='text/event-stream' + media_type='text/event-stream', + headers={ + 'Cache-Control': 'no-cache', + 'Connection': 'keep-alive', + 'X-Accel-Buffering': 'no', + 'Transfer-Encoding': 'chunked' + } ) # 非流式响应 @@ -115,12 +121,15 @@ async def converse_with_chat_assistant( def stream_ragflow_response(result: Generator, chat_id: str, start_time: float) -> Generator[str, None, None]: """ - 流式处理RAGFlow响应 - 同步版本 + 流式处理RAGFlow响应 - 同步版本,修复首token延迟 """ last_answer = "" first_token_received = False start_stream_time = time.perf_counter() + # 立即发送连接建立消息,解决首token延迟 + yield "event: ping\ndata: {\"status\": \"connected\"}\n\n" + try: for chunk in result: # 检查第一个token的延迟 diff --git a/ruoyi-fastapi-backend/utils/ragflow_util.py b/ruoyi-fastapi-backend/utils/ragflow_util.py index 8908c4f..61fb26f 100644 --- a/ruoyi-fastapi-backend/utils/ragflow_util.py +++ b/ruoyi-fastapi-backend/utils/ragflow_util.py @@ -53,12 +53,20 @@ class RAGFlowClient: return result def _stream_request(self, method: str, endpoint: str, **kwargs) -> Generator[Dict[str, Any], None, None]: - """发送流式HTTP请求""" + """发送流式HTTP请求,修复SSE缓冲问题""" url = f"{self.base_url}{endpoint}" headers = kwargs.pop('headers', self.headers.copy()) + # 添加防止缓冲的响应头 + headers.update({ + 'Cache-Control': 'no-cache', + 'Connection': 'keep-alive', + 'X-Accel-Buffering': 'no' # Nginx禁用缓冲 + }) + response = requests.request(method, url, headers=headers, stream=True, **kwargs) + # 立即处理响应,yield每个chunk for line in response.iter_lines(): if line: line = line.decode('utf-8')