|
|
@@ -2,38 +2,41 @@
|
|
|
|
|
|
## 1. 接口用途
|
|
|
|
|
|
-用于文档编辑页中,针对“当前选中章节”发起 AI 对话。当前接口支持两类能力:
|
|
|
+`/sgbx/document_chat` 用于文档编辑页中,围绕“当前选中章节”发起 AI 对话。当前支持两类能力:
|
|
|
|
|
|
- 章节问答:总结、解释、分析、判断当前章节是否合理或完整。
|
|
|
- 章节修改:润色、扩写、改写、补充、压缩、优化当前章节正文,并返回修改草案。
|
|
|
|
|
|
注意:
|
|
|
|
|
|
-- 本接口只用于文档编辑 AI 对话,不影响方案编写、大纲生成、章节续写等 `construction_write` 接口。
|
|
|
-- SSE 中的 `reasoning` 是可展示的处理过程,不是模型原始思维链。
|
|
|
-- 原始 `<think>...</think>` 推理内容不会透出。
|
|
|
-- 修改类请求只返回草案和 diff,不直接保存或替换章节。
|
|
|
+- 本接口只处理文档编辑 AI 对话,不影响方案编写、大纲生成、章节续写等 `construction_write` 接口。
|
|
|
+- 修改类请求只返回草案,不直接保存或替换章节。
|
|
|
+- 当前版本不生成 diff;返回体中的 `diff`、`old_content_hash`、`new_content_hash`、`diff_granularity` 是保留字段,默认为空。
|
|
|
- `references` 只返回通过质量门控、实际提交给大模型的知识库参考。
|
|
|
+- SSE 中的 `reasoning` 是可展示的处理过程,不是模型原始思维链;原始 `<think>...</think>` 内容不会透出。
|
|
|
+- 当前 SSE 会流式推送流程事件,但 `chunk` 仍是在模型生成完成后一次性推送完整回答或完整草案,不是 token 级逐字输出。
|
|
|
|
|
|
-## 2. 判断问答还是内容编写
|
|
|
+## 2. 意图判定
|
|
|
|
|
|
-接口不是根据最终大模型返回文本来判断“问答”或“编写”,而是在工作流前置阶段先做意图识别。
|
|
|
+接口不是根据最终文本判断“问答”或“修改”,而是在工作流前置阶段先执行意图识别。
|
|
|
|
|
|
### 2.1 判定入口
|
|
|
|
|
|
-意图识别节点:`recognize_intent`
|
|
|
+- 工作流节点:`recognize_intent`
|
|
|
+- 模型功能名:`document_chat_intent`
|
|
|
+- 可调用技能白名单:`document-answer`、`document-modify`
|
|
|
|
|
|
-模型功能名:`document_chat_intent`
|
|
|
+意图识别模型的核心输入:
|
|
|
|
|
|
-输入给意图识别模型的核心信息:
|
|
|
-
|
|
|
-- `message`:用户本轮输入。
|
|
|
-- `selected_section.index/title/code/content_preview`:当前选中章节信息和正文预览。
|
|
|
-- `project_info`:项目信息。
|
|
|
-- `document_context`:前后文、同级章节、检索范围。
|
|
|
-- `available_skills`:当前允许调用的技能列表。
|
|
|
+| 字段 | 说明 |
|
|
|
+| --- | --- |
|
|
|
+| `message` | 用户本轮输入 |
|
|
|
+| `selected_section.index/title/code/content_preview` | 当前选中章节信息和正文预览 |
|
|
|
+| `project_info` | 项目信息 |
|
|
|
+| `document_context` | 前后文、同级章节、检索范围 |
|
|
|
+| `available_skills` | 后端允许调用的技能列表 |
|
|
|
|
|
|
-意图识别模型必须返回 JSON:
|
|
|
+意图识别模型返回 JSON,示例:
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
@@ -50,42 +53,25 @@
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-### 2.2 判定规则
|
|
|
+### 2.2 路由规则
|
|
|
|
|
|
| 判定结果 | 条件 | 后续执行 | 最终 `response_type` |
|
|
|
| --- | --- | --- | --- |
|
|
|
-| 普通问答 | `skill_name=document-answer` | 执行 `DocumentAnswerSkill` | `answer` |
|
|
|
-| 内容编写/修改 | `skill_name=document-modify` | 执行 `DocumentModifySkill`,再生成 diff | `proposal` |
|
|
|
-| 需要澄清 | `needs_clarification=true` 或 `confidence < 0.65` | 返回澄清问题 | `clarify` |
|
|
|
+| 章节问答 | `skill_name=document-answer` | 执行 `DocumentAnswerSkill` | `answer` |
|
|
|
+| 章节修改 | `skill_name=document-modify` | 执行 `DocumentModifySkill` | `proposal` |
|
|
|
+| 需要澄清 | `needs_clarification=true`、`intent=clarify` 或 `confidence < 0.65` | 返回澄清问题 | `clarify` |
|
|
|
| 不支持 | `intent=unsupported` 或 skill 不在白名单 | 返回不支持说明 | `unsupported` |
|
|
|
-| 异常 | 工作流或模型调用异常 | 返回错误 | `error` |
|
|
|
-
|
|
|
-### 2.3 白名单保护
|
|
|
-
|
|
|
-`skill_name` 只能从后端加载的 skill 白名单中选择:
|
|
|
-
|
|
|
-- `document-answer`
|
|
|
-- `document-modify`
|
|
|
-
|
|
|
-`intent` 与 `skill_name` 必须一致:
|
|
|
+| 异常 | 工作流或模型调用异常 | 返回错误信息 | `error` |
|
|
|
|
|
|
-- `document-answer` 对应 `intent=document_answer`。
|
|
|
-- `document-modify` 对应 `intent=document_modify`。
|
|
|
-- 如果模型返回 `intent=unsupported` 但 `skill_name=document-answer` 或 `document-modify`,后端会按白名单 skill 自动修正并继续执行对应能力。
|
|
|
+后端会做白名单归一化:如果模型返回的 `intent` 与 `skill_name` 不一致,但 `skill_name` 命中白名单且不需要澄清,则优先信任白名单 skill 并修正 `intent`。
|
|
|
|
|
|
-如果模型返回了不存在的 skill:
|
|
|
+### 2.3 意图识别失败兜底
|
|
|
|
|
|
-- `intent=document_modify` 时,后端会修正为 `document-modify`。
|
|
|
-- `intent=document_answer` 时,后端会修正为 `document-answer`。
|
|
|
-- 仍无法匹配时,判定为 `unsupported`。
|
|
|
-
|
|
|
-### 2.4 模型失败时的兜底规则
|
|
|
-
|
|
|
-如果意图识别模型异常或返回非 JSON,后端会使用关键词兜底:
|
|
|
+如果意图识别模型异常或返回非 JSON,后端使用关键词兜底:
|
|
|
|
|
|
| 用户输入包含 | 兜底意图 |
|
|
|
| --- | --- |
|
|
|
-| 怎么完善、如何完善、完善建议、修改建议、优化建议、补充建议、怎么改、如何改 | `document_answer` |
|
|
|
+| 怎么完善、如何完善、怎样完善、完善建议、修改建议、优化建议、补充建议、怎么改、如何改 | `document_answer` |
|
|
|
| 润色、扩写、改写、修改、补充、完善、压缩、简化、优化、替换、重写 | `document_modify` |
|
|
|
| 解释、说明、总结、分析、是否、为什么、哪里、问题、合理、缺少 | `document_answer` |
|
|
|
| 空消息 | `clarify` |
|
|
|
@@ -99,7 +85,7 @@
|
|
|
POST /sgbx/document_chat
|
|
|
```
|
|
|
|
|
|
-### 3.2 SSE 流式
|
|
|
+### 3.2 SSE
|
|
|
|
|
|
```http
|
|
|
POST /sgbx/document_chat?stream=true
|
|
|
@@ -113,21 +99,36 @@ POST /sgbx/document_chat?stream=true
|
|
|
}
|
|
|
```
|
|
|
|
|
|
+当 query 参数 `stream=true` 或请求体 `response_mode=sse` 任一成立时,接口返回 `text/event-stream`。
|
|
|
+
|
|
|
### 3.3 健康检查
|
|
|
|
|
|
```http
|
|
|
GET /sgbx/document_chat/health
|
|
|
```
|
|
|
|
|
|
+返回示例:
|
|
|
+
|
|
|
+```json
|
|
|
+{
|
|
|
+ "status": "healthy",
|
|
|
+ "module": "document_chat",
|
|
|
+ "workflow": "langgraph",
|
|
|
+ "skills": ["document-answer", "document-modify"]
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
## 4. 请求参数
|
|
|
|
|
|
+请求体不允许传入未定义字段。
|
|
|
+
|
|
|
| 字段 | 类型 | 必填 | 说明 |
|
|
|
| --- | --- | --- | --- |
|
|
|
| `user_id` | string | 是 | 用户 ID |
|
|
|
-| `message` | string | 是 | 用户问题或修改要求 |
|
|
|
+| `message` | string | 是 | 用户问题或修改要求,不能为空 |
|
|
|
| `selected_section` | object | 是 | 当前选中章节 |
|
|
|
-| `conversation_id` | string | 否 | 会话 ID |
|
|
|
-| `task_id` | string | 否 | 业务任务 ID |
|
|
|
+| `conversation_id` | string/null | 否 | 会话 ID |
|
|
|
+| `task_id` | string/null | 否 | 业务任务 ID |
|
|
|
| `project_info` | object | 否 | 项目信息 |
|
|
|
| `document_context` | object | 否 | 章节上下文和检索范围 |
|
|
|
| `conversation_history` | array | 否 | 历史对话 |
|
|
|
@@ -141,8 +142,8 @@ GET /sgbx/document_chat/health
|
|
|
| `title` | string | 是 | 章节标题 |
|
|
|
| `content` | string | 否 | 当前章节正文 |
|
|
|
| `code` | string | 否 | 章节编码 |
|
|
|
-| `chapter_level_1` | string | 否 | 一级章节分类,用于 RAG 检索 |
|
|
|
-| `chapter_level_2` | string | 否 | 二级章节分类,用于 RAG 检索 |
|
|
|
+| `chapter_level_1` | string | 否 | 一级章节分类,用于相似章节检索 |
|
|
|
+| `chapter_level_2` | string | 否 | 二级章节分类,用于相似章节检索 |
|
|
|
|
|
|
`document_context`:
|
|
|
|
|
|
@@ -151,6 +152,7 @@ GET /sgbx/document_chat/health
|
|
|
| `before` | string | 当前章节前文 |
|
|
|
| `after` | string | 当前章节后文 |
|
|
|
| `siblings` | array | 同级章节摘要 |
|
|
|
+| `references` | array | 入参允许传入,但生成阶段会被后端质量门控后的知识库参考覆盖 |
|
|
|
| `retrieval_filters` | object | RAG 检索范围 |
|
|
|
|
|
|
`retrieval_filters` 常用字段:
|
|
|
@@ -164,6 +166,8 @@ GET /sgbx/document_chat/health
|
|
|
}
|
|
|
```
|
|
|
|
|
|
+检索范围还可以从 `selected_section.chapter_level_1`、`selected_section.chapter_level_2` 或 `project_info` 中补齐。
|
|
|
+
|
|
|
## 5. 请求示例
|
|
|
|
|
|
### 5.1 章节问答
|
|
|
@@ -230,6 +234,8 @@ GET /sgbx/document_chat/health
|
|
|
|
|
|
## 6. 普通 JSON 返回
|
|
|
|
|
|
+### 6.1 问答成功
|
|
|
+
|
|
|
```json
|
|
|
{
|
|
|
"code": 200,
|
|
|
@@ -237,8 +243,19 @@ GET /sgbx/document_chat/health
|
|
|
"data": {
|
|
|
"callback_task_id": "doc_chat_abc123",
|
|
|
"response_type": "answer",
|
|
|
- "intent_result": {},
|
|
|
- "answer": "回答内容",
|
|
|
+ "intent_result": {
|
|
|
+ "intent": "document_answer",
|
|
|
+ "confidence": 0.86,
|
|
|
+ "skill_name": "document-answer",
|
|
|
+ "operation": "answer",
|
|
|
+ "target_scope": "selected_section",
|
|
|
+ "normalized_instruction": "总结当前章节并判断是否完整",
|
|
|
+ "needs_clarification": false,
|
|
|
+ "clarification_question": "",
|
|
|
+ "reason": "",
|
|
|
+ "warnings": []
|
|
|
+ },
|
|
|
+ "answer": "本节主要介绍工程概况、施工对象和主要施工内容。当前内容覆盖了主要结构类型,但现场条件、施工准备和关键工程特点仍可补充。",
|
|
|
"proposed_content": null,
|
|
|
"old_content_hash": null,
|
|
|
"new_content_hash": null,
|
|
|
@@ -247,33 +264,85 @@ GET /sgbx/document_chat/health
|
|
|
"change_summary": [],
|
|
|
"references": [],
|
|
|
"retrieval_status": "low_confidence",
|
|
|
- "retrieval_metrics": {},
|
|
|
+ "retrieval_metrics": {
|
|
|
+ "approved_count": 0,
|
|
|
+ "retrieval_method": "chapter_similarity"
|
|
|
+ },
|
|
|
+ "warnings": ["未找到可信度足够的知识库片段,本次未引用向量库内容。"],
|
|
|
+ "selected_section": {
|
|
|
+ "index": "2.1",
|
|
|
+ "code": "overview_DesignSummary_ProjectIntroduction",
|
|
|
+ "title": "工程简介"
|
|
|
+ },
|
|
|
+ "error_message": null
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+### 6.2 修改成功
|
|
|
+
|
|
|
+```json
|
|
|
+{
|
|
|
+ "code": 200,
|
|
|
+ "message": "success",
|
|
|
+ "data": {
|
|
|
+ "callback_task_id": "doc_chat_def456",
|
|
|
+ "response_type": "proposal",
|
|
|
+ "intent_result": {
|
|
|
+ "intent": "document_modify",
|
|
|
+ "confidence": 0.88,
|
|
|
+ "skill_name": "document-modify",
|
|
|
+ "operation": "modify",
|
|
|
+ "target_scope": "selected_section",
|
|
|
+ "normalized_instruction": "补充当前章节施工准备、现场条件和工程特点描述",
|
|
|
+ "needs_clarification": false,
|
|
|
+ "clarification_question": "",
|
|
|
+ "reason": "",
|
|
|
+ "warnings": []
|
|
|
+ },
|
|
|
+ "answer": null,
|
|
|
+ "proposed_content": "本工程为某桥梁施工项目,主要包括桩基、承台、墩柱及上部结构施工。施工前应完成图纸会审、测量复核、技术交底和临时设施布置...",
|
|
|
+ "old_content_hash": null,
|
|
|
+ "new_content_hash": null,
|
|
|
+ "diff": [],
|
|
|
+ "diff_granularity": null,
|
|
|
+ "change_summary": ["补充施工准备", "增加现场条件描述"],
|
|
|
+ "references": [],
|
|
|
+ "retrieval_status": "low_confidence",
|
|
|
+ "retrieval_metrics": {
|
|
|
+ "approved_count": 0,
|
|
|
+ "retrieval_method": "chapter_similarity"
|
|
|
+ },
|
|
|
"warnings": [],
|
|
|
- "selected_section": {},
|
|
|
+ "selected_section": {
|
|
|
+ "index": "2.1",
|
|
|
+ "code": "overview_DesignSummary_ProjectIntroduction",
|
|
|
+ "title": "工程简介"
|
|
|
+ },
|
|
|
"error_message": null
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-`data` 字段说明:
|
|
|
+### 6.3 字段说明
|
|
|
|
|
|
| 字段 | 类型 | 说明 |
|
|
|
| --- | --- | --- |
|
|
|
| `callback_task_id` | string | 本次请求 ID |
|
|
|
-| `response_type` | string | 返回类型 |
|
|
|
+| `response_type` | string | 返回类型,见下表 |
|
|
|
| `intent_result` | object/null | 意图识别结果 |
|
|
|
-| `answer` | string/null | 问答结果或澄清说明 |
|
|
|
+| `answer` | string/null | 问答结果、澄清问题或不支持说明 |
|
|
|
| `proposed_content` | string/null | 修改后的完整章节正文草案 |
|
|
|
-| `old_content_hash` | string/null | 原正文 hash |
|
|
|
-| `new_content_hash` | string/null | 新正文 hash |
|
|
|
-| `diff` | array | 新旧内容对比 |
|
|
|
-| `diff_granularity` | string/null | `line` 或 `full_content` |
|
|
|
-| `change_summary` | array | 修改摘要 |
|
|
|
-| `references` | array | 实际引用的知识库参考 |
|
|
|
+| `old_content_hash` | string/null | 保留字段,当前为 `null` |
|
|
|
+| `new_content_hash` | string/null | 保留字段,当前为 `null` |
|
|
|
+| `diff` | array | 保留字段,当前为空数组 |
|
|
|
+| `diff_granularity` | string/null | 保留字段,当前为 `null` |
|
|
|
+| `change_summary` | array | 修改摘要,仅 `proposal` 常见 |
|
|
|
+| `references` | array | 通过质量门控并提交给大模型的知识库参考 |
|
|
|
| `retrieval_status` | string/null | RAG 状态 |
|
|
|
| `retrieval_metrics` | object | RAG 指标 |
|
|
|
| `warnings` | array | 提示信息 |
|
|
|
-| `selected_section` | object | 当前章节摘要 |
|
|
|
+| `selected_section` | object | 当前章节摘要,只返回 `index/code/title` |
|
|
|
| `error_message` | string/null | 错误信息 |
|
|
|
|
|
|
`response_type` 取值:
|
|
|
@@ -281,32 +350,56 @@ GET /sgbx/document_chat/health
|
|
|
| 值 | 说明 |
|
|
|
| --- | --- |
|
|
|
| `answer` | 普通问答 |
|
|
|
-| `proposal` | 内容编写/修改草案 |
|
|
|
+| `proposal` | 内容修改草案 |
|
|
|
| `clarify` | 需要用户补充说明 |
|
|
|
| `unsupported` | 当前能力不支持 |
|
|
|
| `error` | 执行异常 |
|
|
|
|
|
|
+普通 JSON 模式下,工作流内错误通常返回 `code=500` 且 `data.response_type=error`;请求处理层未捕获的异常会返回 HTTP 500。
|
|
|
+
|
|
|
## 7. RAG 状态
|
|
|
|
|
|
-| `retrieval_status` | 说明 | `references` |
|
|
|
+| `retrieval_status` | 出现场景 | 最终 `references` |
|
|
|
| --- | --- | --- |
|
|
|
| `usable` | 有高质量参考,已提交给大模型 | 非空 |
|
|
|
-| `no_scope` | 缺少可靠检索范围 | 空数组 |
|
|
|
+| `low_confidence` | 召回或重排内容质量不足,未通过质量门控 | 空数组 |
|
|
|
+| `no_scope` | 缺少可靠检索范围,且不允许无范围检索 | 空数组 |
|
|
|
| `no_recall` | 没有召回内容 | 空数组 |
|
|
|
| `rerank_failed` | 重排失败 | 空数组 |
|
|
|
-| `low_confidence` | 召回内容质量不足 | 空数组 |
|
|
|
-| `disabled` | RAG 关闭 | 空数组 |
|
|
|
-| `null` | 未进入 RAG,例如澄清或不支持 | 空数组 |
|
|
|
+| `disabled` | RAG 配置关闭 | 空数组 |
|
|
|
+| `recalled` | 已召回,通常只在中间状态出现 | 以最终结果为准 |
|
|
|
+| `reranked` | 已重排,通常只在中间状态出现 | 以最终结果为准 |
|
|
|
+| `null` | 未进入 RAG,例如澄清、不支持或早期异常 | 空数组 |
|
|
|
|
|
|
说明:
|
|
|
|
|
|
-- 只有 `retrieval_status=usable` 时,`references` 才表示本次实际引用的知识库内容。
|
|
|
-- 召回但未过质量门控的内容不会进入最终 `references`。
|
|
|
-- SSE 调试事件中可能返回候选片段预览,正式结果仍以最终 `references` 为准。
|
|
|
+- 最终 `references` 只取质量门控后的 `approved_references`。
|
|
|
+- 召回但未通过质量门控的内容不会进入最终 `references`。
|
|
|
+- SSE 的 `retrieval_result` 是重排阶段的过程预览,不等同于最终 `references`。
|
|
|
+
|
|
|
+`retrieval_method` 常见取值:
|
|
|
+
|
|
|
+| retrieval_method | 说明 |
|
|
|
+| --- | --- |
|
|
|
+| `chapter_similarity` | 根据 `chapter_level_1` 和 `chapter_level_2` 走相似章节片段检索 |
|
|
|
+| `milvus_hybrid_vector` | 走 Milvus hybrid search 检索 |
|
|
|
+| `disabled` | RAG 配置关闭 |
|
|
|
+| `empty_query` | 未构建出有效检索 query |
|
|
|
+| `no_scope` | 缺少可靠检索范围,且不允许无范围检索 |
|
|
|
+| `unknown` | 检索异常或未能识别方式 |
|
|
|
|
|
|
## 8. SSE 事件
|
|
|
|
|
|
-SSE 格式:
|
|
|
+SSE 响应头:
|
|
|
+
|
|
|
+```http
|
|
|
+Content-Type: text/event-stream
|
|
|
+Cache-Control: no-cache
|
|
|
+Connection: keep-alive
|
|
|
+X-Accel-Buffering: no
|
|
|
+```
|
|
|
+
|
|
|
+SSE 数据格式:
|
|
|
|
|
|
```text
|
|
|
event: event_name
|
|
|
@@ -314,36 +407,48 @@ data: {"callback_task_id":"doc_chat_abc123"}
|
|
|
|
|
|
```
|
|
|
|
|
|
-### 8.1 事件顺序
|
|
|
+### 8.1 典型事件顺序
|
|
|
|
|
|
-典型问答或修改流程:
|
|
|
+问答或修改流程:
|
|
|
|
|
|
```text
|
|
|
connected
|
|
|
processing
|
|
|
-reasoning
|
|
|
+reasoning # recognize_intent
|
|
|
intent
|
|
|
-reasoning
|
|
|
-retrieval_query
|
|
|
-reasoning
|
|
|
-retrieval_recalled
|
|
|
-reasoning
|
|
|
-retrieval_reranked
|
|
|
-reasoning
|
|
|
-retrieval_approved
|
|
|
-retrieval
|
|
|
+reasoning # rerank_context
|
|
|
+retrieval_result
|
|
|
skill_started
|
|
|
-reasoning
|
|
|
-diff_ready # 仅 proposal 场景可能出现
|
|
|
-chunk
|
|
|
-answer_completed # answer/clarify/unsupported
|
|
|
-proposal_completed # proposal
|
|
|
+reasoning # run_answer_skill 或 run_modify_skill
|
|
|
+chunk # 完整回答或完整草案,一次性推送
|
|
|
+answer_completed # answer
|
|
|
+proposal_completed # proposal
|
|
|
completed
|
|
|
```
|
|
|
|
|
|
-实际事件会根据流程分支变化。例如 `clarify` 和 `unsupported` 不会进入 RAG 检索。
|
|
|
+澄清或不支持流程:
|
|
|
+
|
|
|
+```text
|
|
|
+connected
|
|
|
+processing
|
|
|
+reasoning # recognize_intent
|
|
|
+intent
|
|
|
+answer_completed # response_type=clarify 或 unsupported
|
|
|
+completed
|
|
|
+```
|
|
|
+
|
|
|
+错误流程:
|
|
|
+
|
|
|
+```text
|
|
|
+connected
|
|
|
+processing
|
|
|
+reasoning # error_handler,视错误发生位置而定
|
|
|
+error
|
|
|
+```
|
|
|
+
|
|
|
+实际事件会根据流程分支变化。当前不会发送 `retrieval_query`、`retrieval_recalled`、`retrieval_reranked`、`retrieval_approved`、`retrieval`、`diff_ready` 等事件。
|
|
|
|
|
|
-### 8.2 基础事件
|
|
|
+### 8.2 事件清单
|
|
|
|
|
|
| event | 说明 |
|
|
|
| --- | --- |
|
|
|
@@ -351,90 +456,81 @@ completed
|
|
|
| `processing` | 工作流启动 |
|
|
|
| `reasoning` | 可展示处理过程 |
|
|
|
| `intent` | 意图识别结果 |
|
|
|
+| `retrieval_result` | 重排阶段的参考片段预览 |
|
|
|
| `skill_started` | 技能开始执行 |
|
|
|
-| `chunk` | 最终回答或草案文本块 |
|
|
|
+| `chunk` | 完整回答或完整草案文本 |
|
|
|
| `answer_completed` | 问答、澄清或不支持流程完成 |
|
|
|
| `proposal_completed` | 修改草案完成 |
|
|
|
-| `error` | 错误 |
|
|
|
| `completed` | SSE 流程结束 |
|
|
|
+| `error` | 错误 |
|
|
|
|
|
|
-### 8.3 新增过程事件
|
|
|
+### 8.3 事件 payload
|
|
|
|
|
|
-#### reasoning
|
|
|
+#### connected
|
|
|
+
|
|
|
+```json
|
|
|
+{
|
|
|
+ "callback_task_id": "doc_chat_abc123",
|
|
|
+ "status": "connected",
|
|
|
+ "timestamp": 1779696000
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
-可展示处理过程,不是模型原始思维链。
|
|
|
+#### processing
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
"callback_task_id": "doc_chat_abc123",
|
|
|
- "stage_name": "recognize_intent",
|
|
|
+ "stage_name": "workflow_started",
|
|
|
"status": "processing",
|
|
|
- "message": "已完成用户意图识别"
|
|
|
+ "message": "文档 AI 对话工作流已启动"
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-常见 `stage_name`:
|
|
|
-
|
|
|
-| stage_name | 说明 |
|
|
|
-| --- | --- |
|
|
|
-| `validate_input` | 校验输入 |
|
|
|
-| `load_context` | 整理上下文 |
|
|
|
-| `load_skill_registry` | 加载技能 |
|
|
|
-| `recognize_intent` | 识别意图 |
|
|
|
-| `route_intent` | 路由到问答、修改、澄清或不支持 |
|
|
|
-| `build_retrieval_query` | 构建 RAG 检索问题 |
|
|
|
-| `vector_recall` | 向量召回 |
|
|
|
-| `rerank_context` | 重排召回片段 |
|
|
|
-| `quality_gate` | 质量门控 |
|
|
|
-| `run_answer_skill` | 生成问答 |
|
|
|
-| `run_modify_skill` | 生成修改草案 |
|
|
|
-| `build_diff` | 生成 diff |
|
|
|
-| `complete` | 流程完成 |
|
|
|
-
|
|
|
-#### retrieval_query
|
|
|
-
|
|
|
-返回本次 RAG 查询文本。
|
|
|
+#### reasoning
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
"callback_task_id": "doc_chat_abc123",
|
|
|
- "query": "项目名称:某桥梁施工方案\n工程类型:桥梁工程\n章节:2.1 工程简介\n用户需求:总结一下这一节..."
|
|
|
+ "stage_name": "recognize_intent",
|
|
|
+ "status": "processing",
|
|
|
+ "message": "已完成用户意图识别"
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-#### retrieval_recalled
|
|
|
+当前会主动转成 `reasoning` 的阶段:
|
|
|
+
|
|
|
+| `stage_name` | `message` |
|
|
|
+| --- | --- |
|
|
|
+| `recognize_intent` | 已完成用户意图识别 |
|
|
|
+| `rerank_context` | 知识库内容检索重排完成 |
|
|
|
+| `run_answer_skill` | 已生成章节问答结果 |
|
|
|
+| `run_modify_skill` | 已生成章节修改草案 |
|
|
|
+| `error_handler` | 流程异常,已进入错误处理 |
|
|
|
|
|
|
-返回向量召回结果预览。`candidates` 最多返回 8 条,每条内容最多约 600 字。
|
|
|
+#### intent
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
"callback_task_id": "doc_chat_abc123",
|
|
|
- "retrieval_status": "recalled",
|
|
|
- "retrieval_method": "chapter_similarity",
|
|
|
- "retrieval_metrics": {
|
|
|
- "recall_count": 18,
|
|
|
- "max_vector_similarity": 0.78
|
|
|
- },
|
|
|
- "candidate_count": 18,
|
|
|
- "candidates": [
|
|
|
- {
|
|
|
- "source": "相似施工方案A",
|
|
|
- "snippet": "施工准备包括图纸会审、测量复核、临时设施布置...",
|
|
|
- "vector_similarity": 0.78,
|
|
|
- "metadata": {
|
|
|
- "knowledge_base_id": "kb-bridge-001",
|
|
|
- "file_name": "相似施工方案A",
|
|
|
- "source_scope_valid": true
|
|
|
- }
|
|
|
- }
|
|
|
- ],
|
|
|
- "warnings": []
|
|
|
+ "intent_result": {
|
|
|
+ "intent": "document_answer",
|
|
|
+ "confidence": 0.86,
|
|
|
+ "skill_name": "document-answer",
|
|
|
+ "operation": "answer",
|
|
|
+ "target_scope": "selected_section",
|
|
|
+ "normalized_instruction": "总结当前章节并判断是否完整",
|
|
|
+ "needs_clarification": false,
|
|
|
+ "clarification_question": "",
|
|
|
+ "reason": "",
|
|
|
+ "warnings": []
|
|
|
+ }
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-#### retrieval_reranked
|
|
|
+#### retrieval_result
|
|
|
|
|
|
-返回重排后的参考片段预览。
|
|
|
+`references` 最多 8 条,每条 `content` 最多约 600 字。该事件用于过程展示或调试,最终引用以完成事件中的 `references` 为准。
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
@@ -443,6 +539,7 @@ completed
|
|
|
"retrieval_method": "chapter_similarity",
|
|
|
"retrieval_metrics": {
|
|
|
"recall_count": 18,
|
|
|
+ "max_vector_similarity": 0.78,
|
|
|
"rerank_count": 8,
|
|
|
"max_rerank_score": 0.86
|
|
|
},
|
|
|
@@ -455,7 +552,10 @@ completed
|
|
|
"rerank_score": 0.86,
|
|
|
"metadata": {
|
|
|
"knowledge_base_id": "kb-bridge-001",
|
|
|
- "file_name": "相似施工方案A"
|
|
|
+ "file_name": "相似施工方案A",
|
|
|
+ "chapter_level_1": "technology",
|
|
|
+ "chapter_level_2": "MethodsOverview",
|
|
|
+ "source_scope_valid": true
|
|
|
}
|
|
|
}
|
|
|
],
|
|
|
@@ -463,122 +563,134 @@ completed
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-#### retrieval_approved
|
|
|
-
|
|
|
-返回通过质量门控、实际提交给大模型的参考资料。前端默认应展示这个事件,而不是默认展示全部召回候选。
|
|
|
+#### skill_started
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
"callback_task_id": "doc_chat_abc123",
|
|
|
- "retrieval_status": "usable",
|
|
|
- "retrieval_method": "chapter_similarity",
|
|
|
- "retrieval_metrics": {
|
|
|
- "recall_count": 18,
|
|
|
- "rerank_count": 8,
|
|
|
- "approved_count": 1,
|
|
|
- "max_rerank_score": 0.86
|
|
|
- },
|
|
|
- "approved_count": 1,
|
|
|
- "references": [
|
|
|
- {
|
|
|
- "source": "相似施工方案A",
|
|
|
- "content": "施工准备包括图纸会审、测量复核、临时设施布置...",
|
|
|
- "vector_similarity": 0.78,
|
|
|
- "rerank_score": 0.86,
|
|
|
- "metadata": {
|
|
|
- "knowledge_base_id": "kb-bridge-001",
|
|
|
- "file_name": "相似施工方案A"
|
|
|
- }
|
|
|
- }
|
|
|
- ],
|
|
|
- "warnings": []
|
|
|
+ "skill_name": "document-answer",
|
|
|
+ "response_type": "answer"
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-#### retrieval
|
|
|
+#### chunk
|
|
|
|
|
|
-兼容旧事件,当前 payload 与 `retrieval_approved` 一致。
|
|
|
+```json
|
|
|
+{
|
|
|
+ "callback_task_id": "doc_chat_abc123",
|
|
|
+ "chunk": "本节主要介绍工程概况、施工对象和主要施工内容..."
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
-#### diff_ready
|
|
|
+#### answer_completed
|
|
|
|
|
|
-修改草案生成 diff 后返回摘要。
|
|
|
+`answer`、`clarify`、`unsupported` 都使用该事件完成。payload 为完整 `DocumentChatData`。
|
|
|
|
|
|
```json
|
|
|
{
|
|
|
"callback_task_id": "doc_chat_abc123",
|
|
|
- "diff_granularity": "line",
|
|
|
- "diff_count": 3,
|
|
|
- "old_content_hash": "xxx",
|
|
|
- "new_content_hash": "yyy"
|
|
|
+ "response_type": "answer",
|
|
|
+ "intent_result": {},
|
|
|
+ "answer": "本节主要介绍工程概况...",
|
|
|
+ "proposed_content": null,
|
|
|
+ "change_summary": [],
|
|
|
+ "references": [],
|
|
|
+ "retrieval_status": "low_confidence",
|
|
|
+ "retrieval_metrics": {},
|
|
|
+ "warnings": [],
|
|
|
+ "selected_section": {
|
|
|
+ "index": "2.1",
|
|
|
+ "code": "overview_DesignSummary_ProjectIntroduction",
|
|
|
+ "title": "工程简介"
|
|
|
+ },
|
|
|
+ "error_message": null
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-## 9. SSE 完成事件示例
|
|
|
-
|
|
|
-### 9.1 问答完成
|
|
|
+#### proposal_completed
|
|
|
|
|
|
-```text
|
|
|
-event: chunk
|
|
|
-data: {"callback_task_id":"doc_chat_abc123","chunk":"本节主要介绍工程概况、施工对象和主要施工内容..."}
|
|
|
+payload 为完整 `DocumentChatData`。
|
|
|
|
|
|
-event: answer_completed
|
|
|
-data: {"callback_task_id":"doc_chat_abc123","response_type":"answer","answer":"本节主要介绍工程概况...","references":[]}
|
|
|
+```json
|
|
|
+{
|
|
|
+ "callback_task_id": "doc_chat_def456",
|
|
|
+ "response_type": "proposal",
|
|
|
+ "intent_result": {},
|
|
|
+ "answer": null,
|
|
|
+ "proposed_content": "本工程为某桥梁施工项目,主要包括桩基...",
|
|
|
+ "change_summary": ["补充施工准备", "增加现场条件描述"],
|
|
|
+ "references": [],
|
|
|
+ "retrieval_status": "low_confidence",
|
|
|
+ "retrieval_metrics": {},
|
|
|
+ "warnings": [],
|
|
|
+ "selected_section": {
|
|
|
+ "index": "2.1",
|
|
|
+ "code": "overview_DesignSummary_ProjectIntroduction",
|
|
|
+ "title": "工程简介"
|
|
|
+ },
|
|
|
+ "error_message": null
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
-event: completed
|
|
|
-data: {"callback_task_id":"doc_chat_abc123","status":"completed","duration":3.218}
|
|
|
+#### completed
|
|
|
|
|
|
+```json
|
|
|
+{
|
|
|
+ "callback_task_id": "doc_chat_abc123",
|
|
|
+ "status": "completed",
|
|
|
+ "duration": 3.218
|
|
|
+}
|
|
|
```
|
|
|
|
|
|
-### 9.2 修改完成
|
|
|
+#### error
|
|
|
|
|
|
-```text
|
|
|
-event: chunk
|
|
|
-data: {"callback_task_id":"doc_chat_def456","chunk":"本工程为某桥梁施工项目,主要包括桩基..."}
|
|
|
-
|
|
|
-event: proposal_completed
|
|
|
-data: {"callback_task_id":"doc_chat_def456","response_type":"proposal","proposed_content":"本工程为某桥梁施工项目...","diff":[...],"change_summary":["补充施工准备","增加现场条件描述"]}
|
|
|
+```json
|
|
|
+{
|
|
|
+ "callback_task_id": "doc_chat_abc123",
|
|
|
+ "response_type": "error",
|
|
|
+ "error_message": "错误信息"
|
|
|
+}
|
|
|
+```
|
|
|
|
|
|
-event: completed
|
|
|
-data: {"callback_task_id":"doc_chat_def456","status":"completed","duration":5.642}
|
|
|
+如果 SSE 生成器外层捕获到异常,`error` payload 可能是:
|
|
|
|
|
|
+```json
|
|
|
+{
|
|
|
+ "callback_task_id": "doc_chat_abc123",
|
|
|
+ "status": "error",
|
|
|
+ "message": "错误信息"
|
|
|
+}
|
|
|
```
|
|
|
|
|
|
-## 10. 前端处理建议
|
|
|
+## 9. 前端处理建议
|
|
|
|
|
|
-- `intent`:只用于展示本轮识别为“问答”或“修改”,不要把 `intent_result.reason` 当成最终 assistant 消息。
|
|
|
-- `reasoning`:展示为处理进度,例如“正在检索参考资料”“已完成重排”。
|
|
|
-- `retrieval_query`、`retrieval_recalled`、`retrieval_reranked`:建议放在调试详情或折叠面板。
|
|
|
-- `retrieval_approved` 或 `retrieval`:展示“本次引用资料”。
|
|
|
-- `response_type=answer`:展示 `answer`。
|
|
|
-- `response_type=proposal`:展示 `proposed_content` 和 `diff`,用户确认后替换当前章节。
|
|
|
-- `response_type=clarify`:展示 `answer`,引导用户补充说明。
|
|
|
-- `response_type=unsupported`:展示 `answer` 或不支持说明。
|
|
|
-- `response_type=error`:展示 `error_message`。
|
|
|
-- 替换和保存章节由前端或业务后端完成,本 AI 服务不保存文档。
|
|
|
+- 按 `callback_task_id` 归并同一次请求的 SSE 事件。
|
|
|
+- `intent` 只用于展示本轮识别为“问答”或“修改”,不要把 `intent_result.reason` 当成最终 assistant 消息。
|
|
|
+- `reasoning` 展示为处理进度,例如“已完成用户意图识别”“知识库内容检索重排完成”。
|
|
|
+- `retrieval_result` 建议放在“检索详情”或折叠面板;正式引用资料以完成事件和普通 JSON 返回中的 `references` 为准。
|
|
|
+- `response_type=answer` 时展示 `answer`。
|
|
|
+- `response_type=proposal` 时展示 `proposed_content` 和 `change_summary`;用户确认后由前端或业务后端替换当前章节。
|
|
|
+- `response_type=clarify` 时展示 `answer`,引导用户补充说明。
|
|
|
+- `response_type=unsupported` 时展示 `answer` 或不支持说明。
|
|
|
+- `response_type=error` 时展示 `error_message` 或 `message`。
|
|
|
+- 不要依赖当前保留的 diff 字段;当前服务端不生成 diff。
|
|
|
|
|
|
-## 11. 对接边界
|
|
|
+## 10. 对接边界
|
|
|
|
|
|
- 本文档只适用于 `/sgbx/document_chat`。
|
|
|
-- 方案编写接口,例如 `/sgbx/generating_outline`、`/sgbx/content_completion`,不返回文档对话的 `reasoning`、`retrieval_*`、`diff_ready` 事件。
|
|
|
+- 方案编写接口,例如 `/sgbx/generating_outline`、`/sgbx/content_completion`,不复用本文档的事件语义。
|
|
|
- 如果前端同时对接方案编写和文档编辑 AI 对话,应按接口路径区分事件处理逻辑。
|
|
|
+- AI 服务不保存文档,也不直接替换章节;保存和版本管理由前端或业务后端完成。
|
|
|
|
|
|
-## 12. 服务端日志
|
|
|
+## 11. 服务端日志
|
|
|
|
|
|
-文档编辑 AI 对话会写入独立日志目录:
|
|
|
+文档编辑 AI 对话会写入独立模块日志:
|
|
|
|
|
|
```text
|
|
|
logs/document_chat/
|
|
|
```
|
|
|
|
|
|
-主要文件:
|
|
|
-
|
|
|
-| 文件 | 说明 |
|
|
|
-| --- | --- |
|
|
|
-| `document_chat_info.log` | 正常请求、RAG、输出结果 |
|
|
|
-| `document_chat_error.log` | 异常请求 |
|
|
|
-| `document_chat_debug.log` | debug 及以上级别日志 |
|
|
|
-
|
|
|
-日志按 `callback_task_id` 串联一次请求,日志消息体为 JSON 字符串,核心事件如下:
|
|
|
+日志按 `callback_task_id` 串联一次请求,日志消息体为 JSON 字符串。核心事件:
|
|
|
|
|
|
| event | 记录内容 |
|
|
|
| --- | --- |
|
|
|
@@ -589,16 +701,7 @@ logs/document_chat/
|
|
|
| `rag_rerank_skipped` | 未进入重排时的 RAG 状态和原因 |
|
|
|
| `rag_quality_gate_completed` | 质量门控状态、重排结果、最终可引用结果 |
|
|
|
| `rag_quality_gate_skipped` | 未进入质量门控时的 RAG 状态和原因 |
|
|
|
-| `response_completed` | 最终输出结果,包括 `answer`、`proposed_content`、`diff`、`references` |
|
|
|
+| `response_completed` | 最终输出结果 |
|
|
|
| `request_failed` | 异常信息和请求参数 |
|
|
|
|
|
|
-`retrieval_method` 常见取值:
|
|
|
-
|
|
|
-| retrieval_method | 说明 |
|
|
|
-| --- | --- |
|
|
|
-| `chapter_similarity` | 根据 `chapter_level_1` 和 `chapter_level_2` 走相似章节片段检索 |
|
|
|
-| `milvus_hybrid_vector` | 走 Milvus hybrid search 检索 |
|
|
|
-| `disabled` | RAG 配置关闭 |
|
|
|
-| `empty_query` | 未构建出有效检索 query |
|
|
|
-| `no_scope` | 缺少可靠检索范围,且不允许无范围检索 |
|
|
|
-| `unknown` | 检索异常或未能识别方式 |
|
|
|
+`response_completed` 当前不包含服务端生成的 diff;如需前端展示新旧内容对比,需要由前端或后续专门接口生成。
|