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
+49 -11
View File
@@ -54,15 +54,22 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
}
/**
* PATCH /api/dataset/datasets/:datasetId - 修改知识库名称
* PATCH /api/dataset/datasets/:datasetId - 修改知识库设置
*
* Dify API: PATCH /datasets/{dataset_id}
*
* 请求体: { "name": "新的知识库名称" }
* 请求体支持以下字段:
* - name (string): 知识库名称(必填)
* - retrieval_model (object): 检索模型配置(选填)
* - search_method: 'keyword_search' | 'semantic_search' | 'full_text_search' | 'hybrid_search'
* - reranking_enable: boolean
* - reranking_model: { reranking_provider_name, reranking_model_name }
* - weights: number | null (混合检索的语义权重)
* - top_k: number
* - score_threshold_enabled: boolean
* - score_threshold: number | null
*
* 注意:
* - 仅允许修改知识库名称,其他字段不开放修改
* - 删除知识库功能不对外开放
* 注意:删除知识库功能不对外开放
*/
export async function action({ request, params }: ActionFunctionArgs) {
try {
@@ -89,7 +96,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
if (method === 'PATCH') {
const body = await request.json();
// 只允许修改 name 字段
// name 是必填字段
if (!body.name || typeof body.name !== 'string') {
return new Response(
JSON.stringify({ error: '请提供有效的知识库名称 (name)' }),
@@ -97,17 +104,48 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
// 只传递 name 字段,忽略其他字段
const allowedBody = { name: body.name.trim() };
if (allowedBody.name.length === 0) {
const trimmedName = body.name.trim();
if (trimmedName.length === 0) {
return new Response(
JSON.stringify({ error: '知识库名称不能为空' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API] Update Dataset Name:', { datasetId, name: allowedBody.name });
// 构建允许的请求体
const allowedBody: Record<string, any> = {
name: trimmedName,
};
// 可选: retrieval_model 检索模型配置
if (body.retrieval_model && typeof body.retrieval_model === 'object') {
const rm = body.retrieval_model;
// 验证 search_method
const validSearchMethods = ['keyword_search', 'semantic_search', 'full_text_search', 'hybrid_search'];
if (rm.search_method && !validSearchMethods.includes(rm.search_method)) {
return new Response(
JSON.stringify({ error: '无效的检索方法 (search_method)' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
allowedBody.retrieval_model = {
search_method: rm.search_method,
reranking_enable: rm.reranking_enable ?? false,
reranking_mode: rm.reranking_mode ?? null,
reranking_model: rm.reranking_model ?? {
reranking_provider_name: '',
reranking_model_name: '',
},
weights: rm.weights ?? null,
top_k: rm.top_k ?? 3,
score_threshold_enabled: rm.score_threshold_enabled ?? false,
score_threshold: rm.score_threshold_enabled ? (rm.score_threshold ?? null) : null,
};
}
console.log('[API] Update Dataset Settings:', { datasetId, body: allowedBody });
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
const response = await fetch(apiUrl, {