feat: complete Day 21-24 knowledge base features
- Day 21-22: fix CORS and file upload issues - Day 23-24: implement @knowledge base search and conversation integration - Backend: integrate Dify RAG into conversation system - Frontend: load knowledge base list and @ reference feature
This commit is contained in:
@@ -2,6 +2,7 @@ import { prisma } from '../config/database.js';
|
||||
import { LLMFactory } from '../adapters/LLMFactory.js';
|
||||
import { Message, ModelType, StreamChunk } from '../adapters/types.js';
|
||||
import { agentService } from './agentService.js';
|
||||
import * as knowledgeBaseService from './knowledgeBaseService.js';
|
||||
|
||||
interface CreateConversationData {
|
||||
userId: string;
|
||||
@@ -217,8 +218,44 @@ export class ConversationService {
|
||||
// 获取知识库上下文(如果有@知识库)
|
||||
let knowledgeBaseContext = '';
|
||||
if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
|
||||
// TODO: 调用Dify RAG获取知识库上下文
|
||||
knowledgeBaseContext = '相关文献内容...';
|
||||
const knowledgeResults: string[] = [];
|
||||
|
||||
// 对每个知识库进行检索
|
||||
for (const kbId of knowledgeBaseIds) {
|
||||
try {
|
||||
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
|
||||
userId,
|
||||
kbId,
|
||||
content,
|
||||
3 // 每个知识库返回3个最相关的段落
|
||||
);
|
||||
|
||||
// 格式化检索结果
|
||||
if (searchResult.records && searchResult.records.length > 0) {
|
||||
const kbInfo = await prisma.knowledgeBase.findUnique({
|
||||
where: { id: kbId },
|
||||
select: { name: true },
|
||||
});
|
||||
|
||||
knowledgeResults.push(
|
||||
`【知识库:${kbInfo?.name || '未命名'}】\n` +
|
||||
searchResult.records
|
||||
.map((record: any, index: number) => {
|
||||
const score = (record.score * 100).toFixed(1);
|
||||
return `${index + 1}. [相关度${score}%] ${record.segment.content}`;
|
||||
})
|
||||
.join('\n\n')
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to search knowledge base ${kbId}:`, error);
|
||||
// 检索失败不阻止对话,继续处理
|
||||
}
|
||||
}
|
||||
|
||||
if (knowledgeResults.length > 0) {
|
||||
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
|
||||
}
|
||||
}
|
||||
|
||||
// 组装上下文
|
||||
@@ -299,8 +336,44 @@ export class ConversationService {
|
||||
// 获取知识库上下文(如果有@知识库)
|
||||
let knowledgeBaseContext = '';
|
||||
if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
|
||||
// TODO: 调用Dify RAG获取知识库上下文
|
||||
knowledgeBaseContext = '相关文献内容...';
|
||||
const knowledgeResults: string[] = [];
|
||||
|
||||
// 对每个知识库进行检索
|
||||
for (const kbId of knowledgeBaseIds) {
|
||||
try {
|
||||
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
|
||||
userId,
|
||||
kbId,
|
||||
content,
|
||||
3 // 每个知识库返回3个最相关的段落
|
||||
);
|
||||
|
||||
// 格式化检索结果
|
||||
if (searchResult.records && searchResult.records.length > 0) {
|
||||
const kbInfo = await prisma.knowledgeBase.findUnique({
|
||||
where: { id: kbId },
|
||||
select: { name: true },
|
||||
});
|
||||
|
||||
knowledgeResults.push(
|
||||
`【知识库:${kbInfo?.name || '未命名'}】\n` +
|
||||
searchResult.records
|
||||
.map((record: any, index: number) => {
|
||||
const score = (record.score * 100).toFixed(1);
|
||||
return `${index + 1}. [相关度${score}%] ${record.segment.content}`;
|
||||
})
|
||||
.join('\n\n')
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to search knowledge base ${kbId}:`, error);
|
||||
// 检索失败不阻止对话,继续处理
|
||||
}
|
||||
}
|
||||
|
||||
if (knowledgeResults.length > 0) {
|
||||
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
|
||||
}
|
||||
}
|
||||
|
||||
// 组装上下文
|
||||
|
||||
Reference in New Issue
Block a user