temp:临时备份,完成一半知识库管理模块

This commit is contained in:
PingChuan
2025-12-01 12:33:53 +08:00
parent 754ec2c7b5
commit 0c1b81cfb2
25 changed files with 3564 additions and 560 deletions
@@ -0,0 +1,93 @@
import { type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* PATCH /api/dataset/datasets/:datasetId/documents/:documentId/segments/:segmentId/child_chunks/:childChunkId - 更新子分段
* DELETE /api/dataset/datasets/:datasetId/documents/:documentId/segments/:segmentId/child_chunks/:childChunkId - 删除子分段
*
* Dify API:
* - PATCH /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}/child_chunks/{child_chunk_id}
* - DELETE /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}/child_chunks/{child_chunk_id}
*/
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, documentId, segmentId, childChunkId } = params;
if (!datasetId || !documentId || !segmentId || !childChunkId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const method = request.method;
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`;
if (method === 'DELETE') {
console.log('[API] Delete Child Chunk:', { datasetId, documentId, segmentId, childChunkId });
const response = await fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
});
// Dify 删除子分段返回 204 No Content
if (response.status === 204) {
return new Response(
JSON.stringify({ result: 'success' }),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
}
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
}
if (method === 'PATCH') {
const body = await request.json();
console.log('[API] Update Child Chunk:', { datasetId, documentId, segmentId, childChunkId, body });
const response = await fetch(apiUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
});
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] Child Chunk Action - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to process child chunk request' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -0,0 +1,126 @@
import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* GET /api/dataset/datasets/:datasetId/documents/:documentId/segments/:segmentId/child_chunks - 获取子分段列表
* Dify API: GET /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}/child_chunks
*/
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, documentId, segmentId } = params;
if (!datasetId || !documentId || !segmentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
// 获取查询参数
const url = new URL(request.url);
const page = url.searchParams.get('page') || '1';
const limit = url.searchParams.get('limit') || '20';
const keyword = url.searchParams.get('keyword') || '';
console.log('[API] Child Chunks List:', { datasetId, documentId, segmentId, page, limit, keyword });
// 构建查询参数
const queryParams = new URLSearchParams({ page, limit });
if (keyword) queryParams.append('keyword', keyword);
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks?${queryParams}`;
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] Child Chunks List - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to get child chunks' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
/**
* POST /api/dataset/datasets/:datasetId/documents/:documentId/segments/:segmentId/child_chunks - 新增子分段
* Dify API: POST /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}/child_chunks
* 请求体: { content: string }
*/
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, documentId, segmentId } = params;
if (!datasetId || !documentId || !segmentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
if (request.method !== 'POST') {
return new Response(
JSON.stringify({ error: 'Method not allowed' }),
{ status: 405, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
console.log('[API] Create Child Chunk:', { datasetId, documentId, segmentId });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
});
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] Create Child Chunk - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to create child chunk' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -1,4 +1,4 @@
import { type LoaderFunctionArgs } from '@remix-run/node';
import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
@@ -64,3 +64,65 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
}
/**
* POST /api/dataset/datasets/:datasetId/documents/:documentId/segments - 新增分段(批量)
* Dify API: POST /datasets/{dataset_id}/documents/{document_id}/segments
* 请求体: { segments: [{ content, answer?, keywords? }] }
*/
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, documentId } = params;
if (!datasetId || !documentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
if (request.method !== 'POST') {
return new Response(
JSON.stringify({ error: 'Method not allowed' }),
{ status: 405, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
console.log('[API] Create Segments:', { datasetId, documentId, segmentsCount: body.segments?.length });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
});
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] Create Segments - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to create segments' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -0,0 +1,87 @@
import { type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* POST /api/dataset/datasets/:datasetId/retrieve - 检索知识库
* Dify API: POST /datasets/{dataset_id}/retrieve
*
* 请求体:
* {
* query: string, // 检索关键词
* retrieval_model?: {
* search_method: 'keyword_search' | 'semantic_search' | 'full_text_search' | 'hybrid_search',
* reranking_enable?: boolean,
* reranking_model?: {
* reranking_provider_name: string,
* reranking_model_name: string
* },
* top_k?: number,
* score_threshold_enabled?: boolean,
* score_threshold?: number
* },
* metadata_filtering_conditions?: {
* logical_operator: 'and' | 'or',
* conditions: Array<{
* name: string,
* comparison_operator: string,
* value?: string | number
* }>
* }
* }
*/
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: '缺少知识库ID' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
if (request.method !== 'POST') {
return new Response(
JSON.stringify({ error: 'Method not allowed' }),
{ status: 405, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
console.log('[API] Retrieve Dataset:', { datasetId, query: body.query });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/retrieve`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
});
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] Retrieve Dataset - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to retrieve dataset' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
+26 -20
View File
@@ -54,27 +54,15 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
}
/**
* PATCH /api/dataset/datasets/:datasetId - 修改知识库详情
* PATCH /api/dataset/datasets/:datasetId - 修改知识库名称
*
* Dify API: PATCH /datasets/{dataset_id}
*
* 请求体示例:
* {
* "name": "知识库名称",
* "description": "描述",
* "indexing_technique": "high_quality",
* "permission": "only_me",
* "embedding_model_provider": "zhipuai",
* "embedding_model": "embedding-3",
* "retrieval_model": {
* "search_method": "semantic_search",
* "reranking_enable": false,
* "top_k": 2,
* "score_threshold_enabled": false
* }
* }
* 请求体: { "name": "新的知识库名称" }
*
* 注意:删除知识库功能不对外开放
* 注意:
* - 仅允许修改知识库名称,其他字段不开放修改
* - 删除知识库功能不对外开放
*/
export async function action({ request, params }: ActionFunctionArgs) {
try {
@@ -99,9 +87,27 @@ export async function action({ request, params }: ActionFunctionArgs) {
const method = request.method;
if (method === 'PATCH') {
// 修改知识库详情
const body = await request.json();
console.log('[API] Update Dataset:', { datasetId, body });
// 只允许修改 name 字段
if (!body.name || typeof body.name !== 'string') {
return new Response(
JSON.stringify({ error: '请提供有效的知识库名称 (name)' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
// 只传递 name 字段,忽略其他字段
const allowedBody = { name: body.name.trim() };
if (allowedBody.name.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 apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
const response = await fetch(apiUrl, {
@@ -110,7 +116,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
body: JSON.stringify(allowedBody),
});
const data = await response.json();
+1 -1
View File
@@ -43,7 +43,7 @@ export const meta: MetaFunction = () => {
export default function ChatWithLLMIndex() {
return (
<div className="chat-container" style={{
height: '93vh', // 使用视口高度的90%
height: '90vh', // 使用视口高度的90%
borderRadius: '0.5rem',
marginBottom: '-20px',
marginTop: '2vh',
+5 -5
View File
@@ -1,7 +1,7 @@
import DatasetManager from "~/components/dify-dataset-manager";
import datasetManagerStyles from "~/styles/components/dify-dataset-manager/index.css?url";
import sidebarStyles from "~/styles/components/dify-dataset-manager/sidebar.css?url";
import documentListStyles from "~/styles/components/dify-dataset-manager/document-list.css?url";
// import sidebarStyles from "~/styles/components/dify-dataset-manager/sidebar.css?url";
// import documentListStyles from "~/styles/components/dify-dataset-manager/document-list.css?url";
/**
* 注册样式
@@ -9,8 +9,8 @@ import documentListStyles from "~/styles/components/dify-dataset-manager/documen
export function links() {
return [
{ rel: "stylesheet", href: datasetManagerStyles },
{ rel: "stylesheet", href: sidebarStyles },
{ rel: "stylesheet", href: documentListStyles },
// { rel: "stylesheet", href: sidebarStyles },
// { rel: "stylesheet", href: documentListStyles },
];
}
@@ -19,7 +19,7 @@ export function links() {
*/
export default function DatasetManagerIndex() {
return (
<div className="dataset-manager-page" style={{ height: '93vh', padding: '16px' }}>
<div className="dataset-manager-page" style={{ height: '90vh', padding: '16px' }}>
<DatasetManager />
</div>
);