d53742948d
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>
180 lines
6.8 KiB
TypeScript
180 lines
6.8 KiB
TypeScript
import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
|
|
import { API_BASE_URL } from '~/config/api-config';
|
|
|
|
/**
|
|
* GET /api/dataset/datasets/:datasetId - 获取知识库详情
|
|
* Dify API: GET /datasets/{dataset_id}
|
|
*/
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
try {
|
|
const { getUserSession } = await import("~/api/login/auth.server");
|
|
const { frontendJWT } = await getUserSession(request);
|
|
|
|
if (!frontendJWT) {
|
|
return new Response(
|
|
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
|
|
{ status: 401, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
const { datasetId } = params;
|
|
if (!datasetId) {
|
|
return new Response(
|
|
JSON.stringify({ error: '缺少 datasetId 参数' }),
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
console.log('[API] Dataset Detail:', { datasetId });
|
|
|
|
// 转发请求到 FastAPI -> Dify API
|
|
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
|
|
const response = await fetch(apiUrl, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${frontendJWT}`,
|
|
},
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
return new Response(JSON.stringify(data), {
|
|
status: response.status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
|
|
} catch (error: any) {
|
|
console.error('[API] Dataset Detail - Error:', error.message);
|
|
return new Response(
|
|
JSON.stringify({ error: error.message || 'Failed to get dataset' }),
|
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* PATCH /api/dataset/datasets/:datasetId - 修改知识库设置
|
|
*
|
|
* Dify API: PATCH /datasets/{dataset_id}
|
|
*
|
|
* 请求体支持以下字段:
|
|
* - 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 {
|
|
const { getUserSession } = await import("~/api/login/auth.server");
|
|
const { frontendJWT } = await getUserSession(request);
|
|
|
|
if (!frontendJWT) {
|
|
return new Response(
|
|
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
|
|
{ status: 401, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
const { datasetId } = params;
|
|
if (!datasetId) {
|
|
return new Response(
|
|
JSON.stringify({ error: '缺少 datasetId 参数' }),
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
const method = request.method;
|
|
|
|
if (method === 'PATCH') {
|
|
const body = await request.json();
|
|
|
|
// name 是必填字段
|
|
if (!body.name || typeof body.name !== 'string') {
|
|
return new Response(
|
|
JSON.stringify({ error: '请提供有效的知识库名称 (name)' }),
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
const trimmedName = body.name.trim();
|
|
if (trimmedName.length === 0) {
|
|
return new Response(
|
|
JSON.stringify({ error: '知识库名称不能为空' }),
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
// 构建允许的请求体
|
|
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, {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${frontendJWT}`,
|
|
},
|
|
body: JSON.stringify(allowedBody),
|
|
});
|
|
|
|
const data = await response.json();
|
|
return new Response(JSON.stringify(data), {
|
|
status: response.status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
return new Response(
|
|
JSON.stringify({ error: 'Method not allowed' }),
|
|
{ status: 405, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
|
|
} catch (error: any) {
|
|
console.error('[API] Dataset Action - Error:', error.message);
|
|
return new Response(
|
|
JSON.stringify({ error: error.message || 'Failed to process dataset request' }),
|
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
}
|