解决 SSE 协议解析问题
This commit is contained in:
parent
234121a503
commit
25925f6ee5
19
README.md
19
README.md
@ -210,4 +210,21 @@ COMPREFACE_API_KEY=your-api-key
|
||||
|
||||
## 许可证
|
||||
|
||||
本项目基于Apache License 2.0开源协议。
|
||||
本项目基于Apache License 2.0开源协议。
|
||||
|
||||
RAGFlow服务使用固定的chat-id,该id在RAGFlow服务端配置,用于标识聊天助手会话:
|
||||
{
|
||||
"chatId": "db4bb966895b11f08cda0242ac130006",
|
||||
"question": "你好,请用简洁的语言介绍你自己",
|
||||
"stream": true,
|
||||
"sessionId": "38d765e48a3811f0be310242ac130006"
|
||||
}
|
||||
|
||||
测试 Token:
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "登录成功",
|
||||
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMSIsInVzZXJfbmFtZSI6ImFkbWluIiwiZGVwdF9uYW1lIjoiXHU3ODE0XHU1M2QxXHU5MGU4XHU5NWU4Iiwic2Vzc2lvbl9pZCI6IjQ3OTBlZmFmLTU2OTktNDE4Zi04MTQ0LWJmMGI3Y2UwNmRhNCIsImxvZ2luX2luZm8iOnsiaXBhZGRyIjpudWxsLCJsb2dpbkxvY2F0aW9uIjoiXHU2NzJhXHU3N2U1IiwiYnJvd3NlciI6Ik90aGVyIiwib3MiOiJPdGhlciIsImxvZ2luVGltZSI6IjIwMjUtMTItMTYgMTM6MDY6MjQifSwiZXhwIjoxNzY4NDUzNTg0fQ.ijL1R8b-6s-OGL1f0uxcD3-b0Na6N3SnxygIvlM3EgM",
|
||||
"success": true,
|
||||
"time": "2025-12-16T13:06:24.888622"
|
||||
}
|
||||
@ -93,7 +93,7 @@ class AsyncRAGFlowClient:
|
||||
return result
|
||||
|
||||
async def _stream_request(self, method: str, endpoint: str, **kwargs) -> AsyncGenerator[Dict[str, Any], None]:
|
||||
"""发送流式HTTP请求"""
|
||||
"""发送流式HTTP请求 - 处理SSE协议"""
|
||||
if not self._session:
|
||||
await self.create_session()
|
||||
|
||||
@ -104,12 +104,24 @@ class AsyncRAGFlowClient:
|
||||
async for line in response.content:
|
||||
if line:
|
||||
line_str = line.decode('utf-8').strip()
|
||||
|
||||
# 处理SSE数据行
|
||||
if line_str.startswith('data:'):
|
||||
# 提取数据部分(去掉 "data: " 前缀)
|
||||
data_part = line_str[5:].strip()
|
||||
|
||||
# 检查是否为结束标记
|
||||
if data_part == '[DONE]':
|
||||
break
|
||||
|
||||
# 尝试解析JSON
|
||||
try:
|
||||
data = json.loads(line_str[5:].strip())
|
||||
data = json.loads(data_part)
|
||||
yield data
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
# 如果不是有效JSON,作为文本返回
|
||||
if data_part:
|
||||
yield {'content': data_part, 'text': data_part}
|
||||
|
||||
# ====================
|
||||
# OpenAI兼容API
|
||||
|
||||
Loading…
Reference in New Issue
Block a user