From 7237a55c37a7ebca99f27e88edbfa338150c1b5c Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 29 May 2025 17:37:51 +0800 Subject: [PATCH] . --- .../components/chat/chat-helpers/index.ts | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/chatdesk-ui/components/chat/chat-helpers/index.ts b/chatdesk-ui/components/chat/chat-helpers/index.ts index 9b04395..1ade550 100644 --- a/chatdesk-ui/components/chat/chat-helpers/index.ts +++ b/chatdesk-ui/components/chat/chat-helpers/index.ts @@ -92,10 +92,46 @@ export const handleRetrieval = async ( }) // ✅ 只保留 similarity >= 0.8 的记录(大于等于 80%) - const filteredResults = results.filter(item => item.similarity >= 0.8) + // const filteredResults = results.filter(item => item.similarity >= 0.8) + // return filteredResults + + // 阈值设定 + const HIGH_THRESHOLD = 0.8 + const LOW_THRESHOLD = 0.5 + + let filteredResults + + // 找出所有 ≥ 0.8 的结果 + const highSimilarity = results.filter(item => item.similarity >= HIGH_THRESHOLD) + + if (highSimilarity.length > 0) { + // ✅ 高相似度:返回全部 + filteredResults = highSimilarity + } else { + // ❌ 无高相似度 + const acceptable = results.filter(item => item.similarity >= LOW_THRESHOLD) + + if (acceptable.length === 0) { + // 🚫 无结果满足最低要求 + filteredResults = [] + } else { + // 🔍 返回最佳项 + const best = acceptable.reduce((a, b) => (a.similarity > b.similarity ? a : b)) + filteredResults = [best] + } + } + + console.log("🔍 筛选后结果:", { + total: results.length, + >=80: highSimilarity.length, + >=50: results.filter(r => r.similarity >= LOW_THRESHOLD).length, + finalReturned: filteredResults.length, + topMatchScore: filteredResults[0]?.similarity + }) return filteredResults + } export const createTempMessages = (