debug: add detailed logging for knowledge base context injection
This commit is contained in:
@@ -141,6 +141,13 @@ export class ConversationService {
|
|||||||
userInput: string,
|
userInput: string,
|
||||||
knowledgeBaseContext?: string
|
knowledgeBaseContext?: string
|
||||||
): Promise<Message[]> {
|
): Promise<Message[]> {
|
||||||
|
console.log('🔧 [assembleContext] 开始组装上下文', {
|
||||||
|
conversationId,
|
||||||
|
agentId,
|
||||||
|
hasKnowledgeBaseContext: !!knowledgeBaseContext,
|
||||||
|
knowledgeBaseContextLength: knowledgeBaseContext?.length || 0
|
||||||
|
});
|
||||||
|
|
||||||
// 获取系统Prompt
|
// 获取系统Prompt
|
||||||
const systemPrompt = agentService.getSystemPrompt(agentId);
|
const systemPrompt = agentService.getSystemPrompt(agentId);
|
||||||
|
|
||||||
@@ -161,6 +168,7 @@ export class ConversationService {
|
|||||||
|
|
||||||
// 判断是否是第一条消息
|
// 判断是否是第一条消息
|
||||||
const isFirstMessage = historyMessages.length === 0;
|
const isFirstMessage = historyMessages.length === 0;
|
||||||
|
console.log(`📜 [assembleContext] 历史消息数: ${historyMessages.length}, 是否首次: ${isFirstMessage}`);
|
||||||
|
|
||||||
// 渲染用户Prompt
|
// 渲染用户Prompt
|
||||||
let userPromptContent: string;
|
let userPromptContent: string;
|
||||||
@@ -172,12 +180,16 @@ export class ConversationService {
|
|||||||
userInput,
|
userInput,
|
||||||
knowledgeBaseContext,
|
knowledgeBaseContext,
|
||||||
});
|
});
|
||||||
|
console.log(`📝 [assembleContext] 首次消息,使用完整模板,长度: ${userPromptContent.length}`);
|
||||||
} else {
|
} else {
|
||||||
// 后续消息:只发送用户输入和知识库上下文(如果有)
|
// 后续消息:只发送用户输入和知识库上下文(如果有)
|
||||||
if (knowledgeBaseContext) {
|
if (knowledgeBaseContext) {
|
||||||
userPromptContent = `${userInput}\n\n## 参考文献(来自知识库)\n${knowledgeBaseContext}`;
|
userPromptContent = `${userInput}\n\n## 参考文献(来自知识库)\n${knowledgeBaseContext}`;
|
||||||
|
console.log(`📝 [assembleContext] 后续消息+知识库,总长度: ${userPromptContent.length}`);
|
||||||
|
console.log(`📋 [assembleContext] userPromptContent预览:\n${userPromptContent.substring(0, 300)}...`);
|
||||||
} else {
|
} else {
|
||||||
userPromptContent = userInput;
|
userPromptContent = userInput;
|
||||||
|
console.log(`📝 [assembleContext] 后续消息,仅用户输入: ${userPromptContent}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,6 +215,7 @@ export class ConversationService {
|
|||||||
content: userPromptContent,
|
content: userPromptContent,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(`✅ [assembleContext] 组装完成,消息总数: ${messages.length}`);
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,6 +347,7 @@ export class ConversationService {
|
|||||||
const conversation = await this.getConversationById(conversationId, userId);
|
const conversation = await this.getConversationById(conversationId, userId);
|
||||||
|
|
||||||
// 获取知识库上下文(如果有@知识库)
|
// 获取知识库上下文(如果有@知识库)
|
||||||
|
console.log('📚 [sendMessageStream] 开始处理知识库', { knowledgeBaseIds });
|
||||||
let knowledgeBaseContext = '';
|
let knowledgeBaseContext = '';
|
||||||
if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
|
if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
|
||||||
const knowledgeResults: string[] = [];
|
const knowledgeResults: string[] = [];
|
||||||
@@ -341,6 +355,7 @@ export class ConversationService {
|
|||||||
// 对每个知识库进行检索
|
// 对每个知识库进行检索
|
||||||
for (const kbId of knowledgeBaseIds) {
|
for (const kbId of knowledgeBaseIds) {
|
||||||
try {
|
try {
|
||||||
|
console.log(`🔎 [sendMessageStream] 检索知识库 ${kbId}`);
|
||||||
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
|
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
|
||||||
userId,
|
userId,
|
||||||
kbId,
|
kbId,
|
||||||
@@ -348,6 +363,11 @@ export class ConversationService {
|
|||||||
3 // 每个知识库返回3个最相关的段落
|
3 // 每个知识库返回3个最相关的段落
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log(`✅ [sendMessageStream] 检索结果`, {
|
||||||
|
kbId,
|
||||||
|
recordCount: searchResult.records?.length || 0
|
||||||
|
});
|
||||||
|
|
||||||
// 格式化检索结果
|
// 格式化检索结果
|
||||||
if (searchResult.records && searchResult.records.length > 0) {
|
if (searchResult.records && searchResult.records.length > 0) {
|
||||||
const kbInfo = await prisma.knowledgeBase.findUnique({
|
const kbInfo = await prisma.knowledgeBase.findUnique({
|
||||||
@@ -355,25 +375,34 @@ export class ConversationService {
|
|||||||
select: { name: true },
|
select: { name: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
knowledgeResults.push(
|
const formattedResult = `【知识库:${kbInfo?.name || '未命名'}】\n` +
|
||||||
`【知识库:${kbInfo?.name || '未命名'}】\n` +
|
|
||||||
searchResult.records
|
searchResult.records
|
||||||
.map((record: any, index: number) => {
|
.map((record: any, index: number) => {
|
||||||
const score = (record.score * 100).toFixed(1);
|
const score = (record.score * 100).toFixed(1);
|
||||||
return `${index + 1}. [相关度${score}%] ${record.segment.content}`;
|
return `${index + 1}. [相关度${score}%] ${record.segment.content}`;
|
||||||
})
|
})
|
||||||
.join('\n\n')
|
.join('\n\n');
|
||||||
);
|
|
||||||
|
console.log(`📄 [sendMessageStream] 格式化结果长度: ${formattedResult.length} 字符`);
|
||||||
|
knowledgeResults.push(formattedResult);
|
||||||
|
} else {
|
||||||
|
console.warn(`⚠️ [sendMessageStream] 知识库 ${kbId} 没有检索到记录`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to search knowledge base ${kbId}:`, error);
|
console.error(`❌ [sendMessageStream] 检索知识库失败 ${kbId}:`, error);
|
||||||
// 检索失败不阻止对话,继续处理
|
// 检索失败不阻止对话,继续处理
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (knowledgeResults.length > 0) {
|
if (knowledgeResults.length > 0) {
|
||||||
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
|
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
|
||||||
|
console.log(`💾 [sendMessageStream] 知识库上下文总长度: ${knowledgeBaseContext.length} 字符`);
|
||||||
|
console.log(`📋 [sendMessageStream] 知识库上下文预览:\n${knowledgeBaseContext.substring(0, 500)}...`);
|
||||||
|
} else {
|
||||||
|
console.warn('⚠️ [sendMessageStream] 没有构建任何知识库上下文');
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log('ℹ️ [sendMessageStream] 未选择知识库');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组装上下文
|
// 组装上下文
|
||||||
|
|||||||
@@ -231,8 +231,6 @@ export async function searchKnowledgeBase(
|
|||||||
retrieval_model: {
|
retrieval_model: {
|
||||||
search_method: 'semantic_search',
|
search_method: 'semantic_search',
|
||||||
top_k: topK,
|
top_k: topK,
|
||||||
score_threshold_enabled: true,
|
|
||||||
score_threshold: 0.3,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user