This commit is contained in:
Tian jianyong 2025-12-17 15:14:46 +08:00
parent 65cf68a012
commit b9fc511806
2 changed files with 20 additions and 3 deletions

View File

@ -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的延迟

View File

@ -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')