Use async model streaming for chat
This commit is contained in:
@@ -247,3 +247,34 @@
|
||||
2. 不修改数据库 schema。
|
||||
3. 不把当前工作区已有的 5 个后端未提交文件混入阶段 3A 提交。
|
||||
4. 如果 async 模型流式验证失败,保留同步流式入口,便于快速回退。
|
||||
|
||||
## 阶段 3A 实施记录
|
||||
|
||||
2026-07-08 完成模型长连接异步化第一版。
|
||||
|
||||
已完成:
|
||||
|
||||
1. `/chat/completions` 的 SSE 生成器已切换为 async 生成器。
|
||||
2. 排队等待改为 `asyncio.to_thread(...)`,避免排队中的请求阻塞事件循环。
|
||||
3. `ChatStreamService` 新增 `stream_answer_async`,保留原同步 `stream_answer` 作为兼容入口。
|
||||
4. `ModelStreamService` 新增 async 模型流式入口:
|
||||
- OpenAI 兼容协议使用 `httpx.AsyncClient.stream`。
|
||||
- Anthropic Messages 协议使用 `httpx.AsyncClient.stream`。
|
||||
5. mock、未命中知识库、关闭流式、MiniMax/Gemini 等路径保留安全降级;需要同步调用模型时使用 `asyncio.to_thread(...)` 放到线程中执行。
|
||||
6. SSE 事件协议未变化,用户端无需改动。
|
||||
|
||||
验证记录:
|
||||
|
||||
1. 本机 `compileall` 检查通过。
|
||||
2. 容器内 `compileall` 检查通过。
|
||||
3. OpenAI 兼容协议 async 模拟流解析通过,输出格式为 `<think>思考</think>答案`。
|
||||
4. Anthropic Messages 协议 async 模拟流解析通过,输出格式为 `<think>想</think>答`。
|
||||
5. 后端健康检查通过。
|
||||
6. 真实 `/chat/completions` 验证通过:首个事件为 `generating`,随后连续返回 `content`,最后返回 `[DONE]`。
|
||||
7. 真实会话历史验证通过:流式完成后用户消息和助手消息均已落库。
|
||||
|
||||
阶段限制:
|
||||
|
||||
1. 飞书检索、RAG 构建、数据库读写仍是同步逻辑;本阶段只优化模型长连接流式调用。
|
||||
2. 首字时间仍会受到飞书读取和 RAG 构建耗时影响。
|
||||
3. 队列仍是进程内队列,多 worker 全局并发控制需要阶段 4 Redis 队列。
|
||||
|
||||
Reference in New Issue
Block a user