From 43b4279e2d032386ed6e46000129fef4b8368239 Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 29 May 2025 18:06:04 +0800 Subject: [PATCH] . --- .../components/chat/chat-helpers/index.ts | 69 +++++++++++++------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/chatdesk-ui/components/chat/chat-helpers/index.ts b/chatdesk-ui/components/chat/chat-helpers/index.ts index f5e6871..5f68819 100644 --- a/chatdesk-ui/components/chat/chat-helpers/index.ts +++ b/chatdesk-ui/components/chat/chat-helpers/index.ts @@ -96,42 +96,69 @@ export const handleRetrieval = async ( // return filteredResults + // // 阈值设定 + // const HIGH_THRESHOLD = 0.8 + // const LOW_THRESHOLD = 0.5 + + // let filteredResults: RetrievedFileItem[] = [] + + // // 找出所有 ≥ 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 + + // 阈值设定 const HIGH_THRESHOLD = 0.8 - const LOW_THRESHOLD = 0.5 + const LOW_THRESHOLD = 0.51 let filteredResults: RetrievedFileItem[] = [] - // 找出所有 ≥ 0.8 的结果 - const highSimilarity = results.filter(item => item.similarity >= HIGH_THRESHOLD) + // 收集所有 ≥ 0.51 的结果(中等和高) + const acceptable = results.filter(item => item.similarity >= LOW_THRESHOLD) - if (highSimilarity.length > 0) { - // ✅ 高相似度:返回全部 - filteredResults = highSimilarity + if (acceptable.length === 0) { + // 🚫 全部太低,直接返回空 + filteredResults = [] } 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] - } + // ✅ 返回所有 ≥ 0.51 的 + filteredResults = acceptable } console.log("🔍 筛选后结果:", { total: results.length, - ">=80": highSimilarity.length, - ">=50": results.filter(r => r.similarity >= LOW_THRESHOLD).length, + ">=80": results.filter(r => r.similarity >= HIGH_THRESHOLD).length, + ">=50": acceptable.length, finalReturned: filteredResults.length, topMatchScore: filteredResults[0]?.similarity }) - return filteredResults - + return filteredResults }