debug: add detailed logging for knowledge base search

This commit is contained in:
AI Clinical Dev Team
2025-10-11 21:20:13 +08:00
parent be56c3d35d
commit 99e1a01d70

View File

@@ -196,6 +196,8 @@ export async function searchKnowledgeBase(
query: string,
topK: number = 3
) {
console.log('🔍 [searchKnowledgeBase] 开始检索', { kbId, query, topK });
// 1. 验证权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
@@ -205,10 +207,23 @@ export async function searchKnowledgeBase(
});
if (!knowledgeBase) {
console.error('❌ [searchKnowledgeBase] 知识库不存在', { kbId, userId });
throw new Error('Knowledge base not found or access denied');
}
console.log('📚 [searchKnowledgeBase] 找到知识库', {
id: knowledgeBase.id,
name: knowledgeBase.name,
difyDatasetId: knowledgeBase.difyDatasetId
});
// 2. 调用Dify检索API
console.log('🌐 [searchKnowledgeBase] 调用Dify检索API', {
difyDatasetId: knowledgeBase.difyDatasetId,
query,
topK
});
const results = await difyClient.retrieveKnowledge(
knowledgeBase.difyDatasetId,
query,
@@ -222,6 +237,22 @@ export async function searchKnowledgeBase(
}
);
console.log('✅ [searchKnowledgeBase] Dify返回结果', {
recordCount: results.records?.length || 0,
hasRecords: results.records && results.records.length > 0
});
if (results.records && results.records.length > 0) {
console.log('📄 [searchKnowledgeBase] 检索到的记录:',
results.records.map((r: any) => ({
score: r.score,
contentPreview: r.segment?.content?.substring(0, 100)
}))
);
} else {
console.warn('⚠️ [searchKnowledgeBase] 没有检索到任何记录');
}
return results;
}