提高
This commit is contained in:
parent
65cf68a012
commit
b9fc511806
@ -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的延迟
|
||||
|
||||
@ -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')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user