feat: 知识库设置页面增加 retrieval_model 检索配置功能

1. 召回测试页面增加 Score 阈值参数配置
2. 知识库设置页面新增检索模型配置:
   - 检索方式 (向量/全文/混合/关键字检索)
   - Reranking 模型 (默认开启,不可关闭)
   - Top K 返回数量
   - Score 阈值 (默认开启,可调节数值)
3. 修复 Dify API 字段名问题 (retrieval_model_dict)
4. 优化数据加载流程,使用详情接口获取完整配置

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-05 22:07:16 +08:00
parent 5f9ce2fe9f
commit d53742948d
9 changed files with 477 additions and 65 deletions
@@ -9,7 +9,12 @@ import type { SearchMethod } from '~/types/dify-dataset-manager/retrieve-test';
* 构建完整的 retrieval_model 参数(匹配 Dify API 规范)
* 根据检索方式启用 Reranking(语义搜索和混合搜索需要启用)
*/
function buildRetrievalModel(searchMethod: SearchMethod, topK: number): RetrievalModel {
function buildRetrievalModel(
searchMethod: SearchMethod,
topK: number,
scoreThresholdEnabled: boolean,
scoreThreshold: number
): RetrievalModel {
// 语义搜索和混合搜索需要启用 Reranking
const needReranking = searchMethod === 'semantic_search' || searchMethod === 'hybrid_search';
@@ -23,8 +28,8 @@ function buildRetrievalModel(searchMethod: SearchMethod, topK: number): Retrieva
},
weights: null,
top_k: topK,
score_threshold_enabled: false,
score_threshold: null,
score_threshold_enabled: scoreThresholdEnabled,
score_threshold: scoreThresholdEnabled ? scoreThreshold : null,
};
}
@@ -38,6 +43,9 @@ export function useRetrieveTest(datasetId: string) {
// 默认使用语义搜索
const [searchMethod, setSearchMethod] = useState<SearchMethod>('semantic_search');
const [topK, setTopK] = useState<number>(5);
// Score 阈值相关状态
const [scoreThresholdEnabled, setScoreThresholdEnabled] = useState(false);
const [scoreThreshold, setScoreThreshold] = useState<number>(0.5);
/**
* 执行检索
@@ -55,7 +63,7 @@ export function useRetrieveTest(datasetId: string) {
setRetrieving(true);
try {
const retrievalModel = buildRetrievalModel(searchMethod, topK);
const retrievalModel = buildRetrievalModel(searchMethod, topK, scoreThresholdEnabled, scoreThreshold);
console.log('[Hook] 检索参数:', { datasetId, query: searchQuery, retrievalModel });
const response = await retrieveDataset(datasetId, searchQuery, retrievalModel);
@@ -69,7 +77,7 @@ export function useRetrieveTest(datasetId: string) {
} finally {
setRetrieving(false);
}
}, [datasetId, searchQuery, searchMethod, topK]);
}, [datasetId, searchQuery, searchMethod, topK, scoreThresholdEnabled, scoreThreshold]);
return {
// 状态
@@ -81,6 +89,10 @@ export function useRetrieveTest(datasetId: string) {
setSearchMethod,
topK,
setTopK,
scoreThresholdEnabled,
setScoreThresholdEnabled,
scoreThreshold,
setScoreThreshold,
// 方法
handleRetrieve,