mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-23 00:46:42 +08:00
Refa: migrate chunk APIs to RESTful routes (#14291)
### What problem does this PR solve? migrate chunk APIs to RESTful routes ### Type of change - [x] Refactoring
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
||||
KnowledgeRouteKey,
|
||||
KnowledgeSearchParams,
|
||||
} from '@/constants/knowledge';
|
||||
import { Routes } from '@/routes';
|
||||
import { useCallback } from 'react';
|
||||
import { useLocation, useNavigate, useSearchParams } from 'react-router';
|
||||
|
||||
@@ -27,13 +28,16 @@ export const useThirdPathName = () => {
|
||||
|
||||
export const useGetKnowledgeSearchParams = () => {
|
||||
const [currentQueryParameters] = useSearchParams();
|
||||
const { pathname } = useLocation();
|
||||
const isDataflowResultPage = pathname === Routes.DataflowResult;
|
||||
|
||||
return {
|
||||
type: currentQueryParameters.get(KnowledgeSearchParams.Type) || '',
|
||||
documentId:
|
||||
currentQueryParameters.get(KnowledgeSearchParams.DocumentId) || '',
|
||||
knowledgeId:
|
||||
currentQueryParameters.get(KnowledgeSearchParams.KnowledgeId) || '',
|
||||
knowledgeId: isDataflowResultPage
|
||||
? currentQueryParameters.get('knowledgeId') || ''
|
||||
: currentQueryParameters.get(KnowledgeSearchParams.KnowledgeId) || '',
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ export const useSelectChunkList = () => {
|
||||
export const useDeleteChunk = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { setPaginationParams } = useSetPaginationParams();
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
@@ -47,7 +48,10 @@ export const useDeleteChunk = () => {
|
||||
} = useMutation({
|
||||
mutationKey: ['deleteChunk'],
|
||||
mutationFn: async (params: { chunkIds: string[]; doc_id: string }) => {
|
||||
const { data } = await kbService.rmChunk(params);
|
||||
const { data } = await kbService.rmChunk({
|
||||
...params,
|
||||
kb_id: knowledgeId,
|
||||
});
|
||||
if (data.code === 0) {
|
||||
setPaginationParams(1);
|
||||
queryClient.invalidateQueries({ queryKey: ['fetchChunkList'] });
|
||||
@@ -62,6 +66,7 @@ export const useDeleteChunk = () => {
|
||||
export const useCreateChunk = () => {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
|
||||
const {
|
||||
data,
|
||||
@@ -74,7 +79,10 @@ export const useCreateChunk = () => {
|
||||
if (payload.chunk_id) {
|
||||
service = kbService.setChunk;
|
||||
}
|
||||
const { data } = await service(payload);
|
||||
const { data } = await service({
|
||||
...payload,
|
||||
kb_id: payload.kb_id || knowledgeId,
|
||||
});
|
||||
if (data.code === 0) {
|
||||
message.success(t('message.created'));
|
||||
setTimeout(() => {
|
||||
@@ -88,14 +96,20 @@ export const useCreateChunk = () => {
|
||||
return { data, loading, createChunk: mutateAsync };
|
||||
};
|
||||
|
||||
export const useFetchChunk = (chunkId?: string): ResponseType<any> => {
|
||||
export const useFetchChunk = (
|
||||
chunkId?: string,
|
||||
documentId?: string,
|
||||
): ResponseType<any> => {
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const { data } = useQuery({
|
||||
queryKey: ['fetchChunk'],
|
||||
enabled: !!chunkId,
|
||||
queryKey: ['fetchChunk', knowledgeId, documentId, chunkId],
|
||||
enabled: !!chunkId && !!documentId && !!knowledgeId,
|
||||
initialData: {},
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
const data = await kbService.getChunk({
|
||||
kb_id: knowledgeId,
|
||||
doc_id: documentId,
|
||||
chunk_id: chunkId,
|
||||
});
|
||||
|
||||
@@ -115,7 +129,7 @@ export const useFetchNextChunkList = (
|
||||
}> &
|
||||
IChunkListResult => {
|
||||
const { pagination, setPagination } = useGetPaginationWithRouter();
|
||||
const { documentId } = useGetKnowledgeSearchParams();
|
||||
const { documentId, knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const { searchString, handleInputChange } = useHandleSearchChange();
|
||||
const [available, setAvailable] = useState<number | undefined>();
|
||||
const debouncedSearchString = useDebounce(searchString, { wait: 500 });
|
||||
@@ -127,6 +141,7 @@ export const useFetchNextChunkList = (
|
||||
} = useQuery({
|
||||
queryKey: [
|
||||
'fetchChunkList',
|
||||
knowledgeId,
|
||||
documentId,
|
||||
pagination.current,
|
||||
pagination.pageSize,
|
||||
@@ -136,9 +151,10 @@ export const useFetchNextChunkList = (
|
||||
placeholderData: (previousData: any) =>
|
||||
previousData ?? { data: [], total: 0, documentInfo: {} }, // https://github.com/TanStack/query/issues/8183
|
||||
gcTime: 0,
|
||||
enabled,
|
||||
enabled: enabled && !!knowledgeId && !!documentId,
|
||||
queryFn: async () => {
|
||||
const { data } = await kbService.chunkList({
|
||||
kb_id: knowledgeId,
|
||||
doc_id: documentId,
|
||||
page: pagination.current,
|
||||
size: pagination.pageSize,
|
||||
@@ -195,6 +211,7 @@ export const useFetchNextChunkList = (
|
||||
|
||||
export const useSwitchChunk = () => {
|
||||
const { t } = useTranslation();
|
||||
const { knowledgeId } = useGetKnowledgeSearchParams();
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
@@ -206,7 +223,10 @@ export const useSwitchChunk = () => {
|
||||
available_int?: number;
|
||||
doc_id: string;
|
||||
}) => {
|
||||
const { data } = await kbService.switchChunk(params);
|
||||
const { data } = await kbService.switchChunk({
|
||||
...params,
|
||||
kb_id: knowledgeId,
|
||||
});
|
||||
if (data.code === 0) {
|
||||
message.success(t('message.modified'));
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import type { ChunkDocType } from '@/interfaces/database/knowledge';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { FieldValues, FormProvider, useForm } from 'react-hook-form';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useDeleteChunkByIds } from '../../hooks';
|
||||
import {
|
||||
transformTagFeaturesArrayToObject,
|
||||
transformTagFeaturesObjectToArray,
|
||||
@@ -75,8 +74,7 @@ const ChunkCreatingModal: React.FC<IModalProps<any> & kFProps> = ({
|
||||
},
|
||||
});
|
||||
const [checked, setChecked] = useState(false);
|
||||
const { removeChunk } = useDeleteChunkByIds();
|
||||
const { data } = useFetchChunk(chunkId);
|
||||
const { data } = useFetchChunk(chunkId, doc_id);
|
||||
const { t } = useTranslation();
|
||||
const isEditMode = !!chunkId;
|
||||
|
||||
@@ -99,12 +97,6 @@ const ChunkCreatingModal: React.FC<IModalProps<any> & kFProps> = ({
|
||||
|
||||
const handleOk = form.handleSubmit(onSubmit);
|
||||
|
||||
const handleRemove = useCallback(() => {
|
||||
if (chunkId) {
|
||||
return removeChunk([chunkId], doc_id);
|
||||
}
|
||||
}, [chunkId, doc_id, removeChunk]);
|
||||
|
||||
const handleCheck = useCallback(() => {
|
||||
setChecked(!checked);
|
||||
}, [checked]);
|
||||
|
||||
@@ -57,7 +57,7 @@ const ChunkCreatingModal: React.FC<IModalProps<any> & kFProps> = ({
|
||||
});
|
||||
const [checked, setChecked] = useState(false);
|
||||
const { removeChunk } = useDeleteChunkByIds();
|
||||
const { data } = useFetchChunk(chunkId);
|
||||
const { data } = useFetchChunk(chunkId, doc_id);
|
||||
const { t } = useTranslation();
|
||||
|
||||
const isTagParser = parserId === 'tag';
|
||||
|
||||
@@ -51,7 +51,7 @@ import { DocumentLog, FileLogsTableProps, IFileLogItem } from './interface';
|
||||
export const getFileLogsTableColumns = (
|
||||
t: TFunction<'translation', string>,
|
||||
showLog: (row: Row<IFileLogItem & DocumentLog>, active: LogTabs) => void,
|
||||
kowledgeId: string,
|
||||
knowledgeId: string,
|
||||
navigateToDataflowResult: (
|
||||
props: NavigateToDataflowResultProps,
|
||||
) => () => void,
|
||||
@@ -210,7 +210,8 @@ export const getFileLogsTableColumns = (
|
||||
size="icon-sm"
|
||||
onClick={navigateToDataflowResult({
|
||||
id: row.original.id,
|
||||
[PipelineResultSearchParams.KnowledgeId]: kowledgeId,
|
||||
[PipelineResultSearchParams.KnowledgeId]:
|
||||
row.original.kb_id || knowledgeId,
|
||||
[PipelineResultSearchParams.DocumentId]:
|
||||
row.original.document_id,
|
||||
[PipelineResultSearchParams.IsReadOnly]: 'false',
|
||||
@@ -358,7 +359,7 @@ const FileLogsTable: FC<FileLogsTableProps> = ({
|
||||
const [isModalVisible, setIsModalVisible] = useState(false);
|
||||
const { navigateToDataflowResult } = useNavigatePage();
|
||||
const [logInfo, setLogInfo] = useState<IFileLogItem>();
|
||||
const kowledgeId = useParams().id;
|
||||
const knowledgeId = useParams().id;
|
||||
const showLog = (row: Row<IFileLogItem & DocumentLog>) => {
|
||||
const logDetail = {
|
||||
taskId: row.original?.dsl?.task_id,
|
||||
@@ -382,7 +383,7 @@ const FileLogsTable: FC<FileLogsTableProps> = ({
|
||||
? getFileLogsTableColumns(
|
||||
t,
|
||||
showLog,
|
||||
kowledgeId || '',
|
||||
knowledgeId || '',
|
||||
navigateToDataflowResult,
|
||||
dataSourceInfo,
|
||||
)
|
||||
|
||||
@@ -21,12 +21,6 @@ const {
|
||||
documentCreate,
|
||||
documentChangeParser,
|
||||
documentThumbnails,
|
||||
chunkList,
|
||||
createChunk,
|
||||
setChunk,
|
||||
getChunk,
|
||||
switchChunk,
|
||||
rmChunk,
|
||||
retrievalTest,
|
||||
documentRun,
|
||||
documentUpload,
|
||||
@@ -97,31 +91,6 @@ const methods = {
|
||||
url: setMeta,
|
||||
method: 'post',
|
||||
},
|
||||
// chunk管理
|
||||
chunkList: {
|
||||
url: chunkList,
|
||||
method: 'post',
|
||||
},
|
||||
createChunk: {
|
||||
url: createChunk,
|
||||
method: 'post',
|
||||
},
|
||||
setChunk: {
|
||||
url: setChunk,
|
||||
method: 'post',
|
||||
},
|
||||
getChunk: {
|
||||
url: getChunk,
|
||||
method: 'get',
|
||||
},
|
||||
switchChunk: {
|
||||
url: switchChunk,
|
||||
method: 'post',
|
||||
},
|
||||
rmChunk: {
|
||||
url: rmChunk,
|
||||
method: 'post',
|
||||
},
|
||||
retrievalTest: {
|
||||
url: retrievalTest,
|
||||
method: 'post',
|
||||
@@ -178,7 +147,139 @@ const methods = {
|
||||
},
|
||||
};
|
||||
|
||||
const kbService = registerServer<keyof typeof methods>(methods, request);
|
||||
const baseKbService = registerServer<keyof typeof methods>(methods, request);
|
||||
|
||||
const getDatasetId = (params: Record<string, any>) =>
|
||||
params.dataset_id || params.kb_id || params.knowledge_id;
|
||||
|
||||
const getDocumentId = (params: Record<string, any>) =>
|
||||
params.document_id || params.doc_id;
|
||||
|
||||
const mapChunkToLegacy = (chunk: Record<string, any>) => ({
|
||||
...chunk,
|
||||
chunk_id: chunk.chunk_id || chunk.id,
|
||||
content_with_weight: chunk.content_with_weight || chunk.content,
|
||||
doc_id: chunk.doc_id || chunk.document_id,
|
||||
doc_name: chunk.doc_name || chunk.docnm_kwd,
|
||||
image_id: chunk.image_id || chunk.img_id,
|
||||
important_kwd: chunk.important_kwd || chunk.important_keywords || [],
|
||||
question_kwd: chunk.question_kwd || chunk.questions || [],
|
||||
available_int: chunk.available_int ?? (chunk.available === false ? 0 : 1),
|
||||
positions: chunk.positions || chunk.position_int || [],
|
||||
});
|
||||
|
||||
const mapDocumentToLegacy = (doc: Record<string, any>) => ({
|
||||
...doc,
|
||||
chunk_num: doc.chunk_num ?? doc.chunk_count,
|
||||
kb_id: doc.kb_id || doc.dataset_id,
|
||||
});
|
||||
|
||||
const mapChunkPayloadToRest = (payload: Record<string, any>) => ({
|
||||
content: payload.content ?? payload.content_with_weight,
|
||||
important_keywords: payload.important_keywords ?? payload.important_kwd,
|
||||
questions: payload.questions ?? payload.question_kwd,
|
||||
tag_kwd: payload.tag_kwd,
|
||||
tag_feas: payload.tag_feas,
|
||||
positions: payload.positions,
|
||||
available:
|
||||
payload.available ??
|
||||
(payload.available_int === undefined
|
||||
? undefined
|
||||
: payload.available_int === 1),
|
||||
image_base64: payload.image_base64,
|
||||
});
|
||||
|
||||
const getAvailableParam = (available?: number) => {
|
||||
if (available === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return available === 1 ? 'true' : 'false';
|
||||
};
|
||||
|
||||
const chunkService = {
|
||||
chunkList: async (params: Record<string, any>) => {
|
||||
const datasetId = getDatasetId(params);
|
||||
const documentId = getDocumentId(params);
|
||||
const response = await request.get(api.chunkList(datasetId, documentId), {
|
||||
params: {
|
||||
page: params.page,
|
||||
page_size: params.page_size || params.size,
|
||||
keywords: params.keywords,
|
||||
available: getAvailableParam(params.available_int),
|
||||
},
|
||||
});
|
||||
|
||||
if (response.data?.code === 0) {
|
||||
response.data.data = {
|
||||
...response.data.data,
|
||||
chunks: (response.data.data?.chunks || []).map(mapChunkToLegacy),
|
||||
doc: mapDocumentToLegacy(response.data.data?.doc || {}),
|
||||
};
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
createChunk: async (payload: Record<string, any>) => {
|
||||
const datasetId = getDatasetId(payload);
|
||||
const documentId = getDocumentId(payload);
|
||||
const response = await request.post(api.chunkList(datasetId, documentId), {
|
||||
data: mapChunkPayloadToRest(payload),
|
||||
});
|
||||
|
||||
if (response.data?.code === 0 && response.data.data?.chunk) {
|
||||
response.data.data.chunk = mapChunkToLegacy(response.data.data.chunk);
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
setChunk: (payload: Record<string, any>) => {
|
||||
const datasetId = getDatasetId(payload);
|
||||
const documentId = getDocumentId(payload);
|
||||
const chunkId = payload.chunk_id || payload.id;
|
||||
return request.patch(api.chunkDetail(datasetId, documentId, chunkId), {
|
||||
data: mapChunkPayloadToRest(payload),
|
||||
});
|
||||
},
|
||||
getChunk: async (params: Record<string, any>) => {
|
||||
const datasetId = getDatasetId(params);
|
||||
const documentId = getDocumentId(params);
|
||||
const chunkId = params.chunk_id || params.id;
|
||||
const response = await request.get(
|
||||
api.chunkDetail(datasetId, documentId, chunkId),
|
||||
);
|
||||
|
||||
if (response.data?.code === 0) {
|
||||
response.data.data = mapChunkToLegacy(response.data.data || {});
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
switchChunk: (params: Record<string, any>) => {
|
||||
const datasetId = getDatasetId(params);
|
||||
const documentId = getDocumentId(params);
|
||||
return request.patch(api.chunkList(datasetId, documentId), {
|
||||
data: {
|
||||
chunk_ids: params.chunk_ids || params.chunkIds,
|
||||
available_int: params.available_int,
|
||||
},
|
||||
});
|
||||
},
|
||||
rmChunk: (params: Record<string, any>) => {
|
||||
const datasetId = getDatasetId(params);
|
||||
const documentId = getDocumentId(params);
|
||||
return request.delete(api.chunkList(datasetId, documentId), {
|
||||
data: {
|
||||
chunk_ids: params.chunk_ids || params.chunkIds,
|
||||
delete_all: params.delete_all,
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
const kbService = {
|
||||
...baseKbService,
|
||||
...chunkService,
|
||||
};
|
||||
|
||||
export const listTag = (knowledgeId: string) =>
|
||||
request.get(api.listTag(knowledgeId));
|
||||
|
||||
@@ -99,12 +99,10 @@ export default {
|
||||
renameTag: (knowledgeId: string) => `${webAPI}/kb/${knowledgeId}/rename_tag`,
|
||||
|
||||
// chunk
|
||||
chunkList: `${webAPI}/chunk/list`,
|
||||
createChunk: `${webAPI}/chunk/create`,
|
||||
setChunk: `${webAPI}/chunk/set`,
|
||||
getChunk: `${webAPI}/chunk/get`,
|
||||
switchChunk: `${webAPI}/chunk/switch`,
|
||||
rmChunk: `${webAPI}/chunk/rm`,
|
||||
chunkList: (datasetId: string, documentId: string) =>
|
||||
`${restAPIv1}/datasets/${datasetId}/documents/${documentId}/chunks`,
|
||||
chunkDetail: (datasetId: string, documentId: string, chunkId: string) =>
|
||||
`${restAPIv1}/datasets/${datasetId}/documents/${documentId}/chunks/${chunkId}`,
|
||||
retrievalTest: `${webAPI}/chunk/retrieval_test`,
|
||||
knowledgeGraph: `${webAPI}/chunk/knowledge_graph`,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user