Features: - PromptService enhancement: enhanceWithKnowledge(), loadFullKnowledge(), ragSearch() - FULL mode: Load entire knowledge base content - RAG mode: Vector search based on user query - Knowledge config API: PUT /:code/knowledge-config - Test render with knowledge injection support - Frontend: Knowledge config UI in Prompt editor Bug fixes: - Fix knowledge config not returned in getPromptDetail - Fix publish button 400 error (empty request body) - Fix cache not invalidated after publish - Add detailed logging for debugging Documentation: - Add development record 2026-01-28 - Update ADMIN module status to Phase 4.6 - Update system status document to v4.5 Co-authored-by: Cursor <cursoragent@cursor.com>
4.6 KiB
4.6 KiB
2026-01-28 Prompt 知识库集成功能完成
开发概述
完成了 Prompt 管理系统与知识库的集成,支持在 Prompt 中动态引用系统知识库内容,实现知识增强的 AI 对话能力。
功能清单
1. PromptService 知识库增强
后端核心改造 (backend/src/common/prompt/)
| 文件 | 改动 | 说明 |
|---|---|---|
prompt.types.ts |
新增类型 | KnowledgeConfig, InjectionMode 类型定义 |
prompt.service.ts |
核心逻辑 | enhanceWithKnowledge(), loadFullKnowledge(), ragSearch() |
prompt.controller.ts |
API 增强 | saveKnowledgeConfig, testRender 支持知识库 |
prompt.routes.ts |
路由 | PUT /:code/knowledge-config |
知识库注入模式:
- FULL 模式:全量加载知识库内容(适合小型核心知识库)
- RAG 模式:基于用户查询进行向量检索(适合大型知识库)
2. 前端配置界面
Prompt 编辑器增强 (frontend-v2/src/pages/admin/)
| 功能 | 说明 |
|---|---|
| 知识库增强开关 | 启用/禁用知识库注入 |
| 知识库选择 | 多选系统知识库(下拉列表) |
| 注入模式选择 | FULL / RAG 模式 |
| 目标变量配置 | 指定注入到哪个模板变量(默认 context) |
| RAG 参数配置 | top_k, min_score |
| FULL 参数配置 | max_tokens 限制 |
| 测试渲染增强 | 预览知识库注入效果 |
3. Bug 修复
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 测试渲染不注入知识库 | testRender API 不支持知识库增强 |
添加 getEnhancedVariables() 公共方法 |
| 知识库配置不保存 | getPromptDetail 返回缺少 knowledge_config |
添加字段到返回数据 |
| 发布按钮 400 错误 | POST 请求体问题 | 添加空对象作为请求体 |
| 发布后第一次不生效 | 脚本发布未清除缓存 | 使用前端发布,正确调用 invalidateCache() |
代码统计
| 类型 | 文件数 | 新增行数 | 修改行数 |
|---|---|---|---|
| 后端 TypeScript | 4 | ~200 | ~50 |
| 前端 TypeScript | 2 | ~150 | ~30 |
| 测试脚本 | 5 | ~500 | - |
技术亮点
1. 知识库增强流程
promptService.get(code, variables, { userId, userQuery })
↓
获取 template.knowledge_config
↓
enhanceWithKnowledge()
↓
┌─────────────────────────────────────┐
│ FULL 模式 │ RAG 模式 │
│ loadFullKnowledge()│ ragSearch() │
│ 加载全部分块 │ 向量检索 top_k │
└─────────────────────────────────────┘
↓
注入到 variables.context
↓
render(template, enhancedVariables)
2. 配置数据结构
interface KnowledgeConfig {
enabled: boolean; // 是否启用
kb_codes: string[]; // 知识库代码列表
injection_mode: 'FULL' | 'RAG'; // 注入模式
target_variable: string; // 目标变量名(默认 context)
// RAG 模式参数
top_k?: number; // 返回结果数
min_score?: number; // 最低相似度
// FULL 模式参数
max_tokens?: number; // Token 数限制
}
3. 版本发布与缓存
- 发布时自动清除版本缓存
- 知识库配置存储在
prompt_templates.knowledge_config(JSONB) - 每次请求从数据库读取最新配置(无配置缓存)
使用指南
1. 配置知识库增强
- 进入 运营管理端 → Prompt管理
- 选择需要增强的 Prompt
- 右侧面板 → 📚 知识库增强
- 开启开关,选择知识库
- 点击 保存配置
2. 在 Prompt 中使用
在 Prompt 模板中使用 {{context}} 变量:
你是一个专业的临床研究方法学专家。
【参考知识库】
{{context}}
请基于以上参考资料回答用户问题。
3. 测试验证
- 点击 测试渲染 预览效果
- 开启 调试模式 在业务端测试
- 确认无误后 发布 到生产
后续优化建议
- 配置缓存:知识库配置可考虑添加短期缓存
- RAG 优化:支持更多检索参数(如 rerank)
- 批量配置:支持批量为多个 Prompt 配置知识库
- 使用统计:记录知识库引用次数和效果反馈