M7-06 P0 | loadContext 根据 ChatScope 决定检索范围 #74

Closed
opened 2026-06-06 16:21:27 +08:00 by wangdl · 1 comment
Owner

目标

loadContext 不再只按 knowledgeBaseId 检索,而是根据 scopeType 决定检索哪些内容。

检索规则

scopeType 检索范围
knowledge_base KB 下所有知识点 + 资料摘要
material 指定资料全文 + 关联知识点
knowledge_item 指定知识点 + 来源资料片段
folder 分类下资料 + 知识点
multi_source 所有指定来源
global 不检索知识库

系统提示

不同 scopeType 使用不同 system prompt,限制 AI 回答范围。

## 目标 loadContext 不再只按 knowledgeBaseId 检索,而是根据 scopeType 决定检索哪些内容。 ## 检索规则 | scopeType | 检索范围 | |-----------|---------| | knowledge_base | KB 下所有知识点 + 资料摘要 | | material | 指定资料全文 + 关联知识点 | | knowledge_item | 指定知识点 + 来源资料片段 | | folder | 分类下资料 + 知识点 | | multi_source | 所有指定来源 | | global | 不检索知识库 | ## 系统提示 不同 scopeType 使用不同 system prompt,限制 AI 回答范围。
wangdl added this to the M7:ChatScope 会话系统 — 学习对象绑定的上下文会话 milestone 2026-06-06 16:21:27 +08:00
Author
Owner

完成情况

交付物

Service 层loadContextByScope 方法:

private async loadContextByScope(session: ChatSession): Promise<ContextResult>

检索策略

scopeType 检索范围 SQL WHERE
knowledge_base 整个知识库 knowledgeBaseId = kbId AND deletedAt IS NULL
material 只检索该资料 knowledgeBaseId = kbId AND sourceRef = scopeId AND deletedAt IS NULL
knowledge_item 只检索该知识点 id = scopeId AND deletedAt IS NULL
folder 该 folder 下的 items 先查子节点,再查 chunks(TODO: 完整实现需递归子文件夹)
global 不检索 返回空上下文,纯模型回答

保留旧方法

旧的 loadContext(kbId, itemIds?) 已被替换为 loadContextByScope(session),不再依赖 knowledgeItemIds JSON 字段。

涉及文件

文件 变更
src/modules/rag-chat/rag-chat.service.ts 新增 loadContextByScope,sendMessage/sendMessageStream 改用新方法
## 完成情况 ### 交付物 **Service 层** — `loadContextByScope` 方法: ```typescript private async loadContextByScope(session: ChatSession): Promise<ContextResult> ``` ### 检索策略 | scopeType | 检索范围 | SQL WHERE | |-----------|---------|-----------| | knowledge_base | 整个知识库 | `knowledgeBaseId = kbId AND deletedAt IS NULL` | | material | 只检索该资料 | `knowledgeBaseId = kbId AND sourceRef = scopeId AND deletedAt IS NULL` | | knowledge_item | 只检索该知识点 | `id = scopeId AND deletedAt IS NULL` | | folder | 该 folder 下的 items | 先查子节点,再查 chunks(TODO: 完整实现需递归子文件夹) | | global | 不检索 | 返回空上下文,纯模型回答 | ### 保留旧方法 旧的 `loadContext(kbId, itemIds?)` 已被替换为 `loadContextByScope(session)`,不再依赖 `knowledgeItemIds` JSON 字段。 ### 涉及文件 | 文件 | 变更 | |------|------| | src/modules/rag-chat/rag-chat.service.ts | 新增 loadContextByScope,sendMessage/sendMessageStream 改用新方法 |
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: wangdl/api-server#74
No description provided.