Enhance LLM integration by adding OpenAI client support and enabling dynamic routing between DeepSeek and OpenAI services. Update environment configuration to include LLM_SERVICE_CODE for service selection, and modify API server to accommodate new request parameters for language and model. Implement streaming response improvements for chat interactions, allowing for segmented SSE output. Update documentation and impact analysis to reflect these changes.

This commit is contained in:
陈辅元
2026-04-16 09:15:01 +08:00
parent 85ad31348e
commit df33657099
17 changed files with 958 additions and 64 deletions
+20
View File
@@ -12,6 +12,10 @@
- **新话题**:不注入历史摘要(`max_pairs=0`)
- **目的**:减少多话题情况下历史 SQL/条件回流导致的串话与错误 SQL。
- **新增(本次补充)**:`backend/llm/openai_client.py`
- 提供 `OpenAIClient.chat` / `chat_with_json`(以及与 DeepSeekClient 对齐的若干便捷方法),用于接入 OpenAI 或 OpenAI 兼容网关。
- `.env` 增补 `OPENAI_MODEL/OPENAI_CHAT_MODEL` 的注释示例(不影响现有配置)。
## 影响与风险
- **破坏性变更**:否(对外 API 不变)
@@ -26,6 +30,22 @@
- 同一 session:先做话题A数据查询,再提一个完全不同话题B(无“再/按上面”等续问词),应不再引用 A 的表/过滤条件。
- 同一 session:首轮查询后,第二轮使用“再/按上面/沿用口径”等续问词,应仍能续用上轮口径生成 SQL。
- 已执行:
- `python -m py_compile backend/llm/openai_client.py`
## 配置说明(补充)
- **LLM 切换**:通过 `.env` 的 `LLM_SERVICE_CODE` 在 `deepseek` / `openai` 之间切换(留空则按 Key 自动选择,优先 deepseek)。
## 前端参数适配(补充)
- **lang_code**:支持 `zh`(简体)、`tc`(繁体)、`en`(英语);在对话分支的默认引导文案/空输入提示中生效。
- **model**:请求体可传 `model` 覆盖默认模型名(与 `service_code` 搭配使用),用于按请求临时切换模型。
## 流式输出(补充)
- **`/g3sb/api/nl/chat/stream`**:`chat` 与 `sql_gen` 阶段的正文改为多段 SSE 分片推送(默认每段约 64 字符,可用环境变量 `SSE_STREAM_CHUNK_CHARS` 调整),与前端 `onDelta` 累加逻辑一致。
## 后续事项
- 若误判率偏高,可迭代 `is_likely_follow_up` 规则(补充关键词/短语),或升级为轻量 LLM 判别器(方案B)。