Skip to content

Commit

Permalink
feat: select the corresponding parsing method according to the file t…
Browse files Browse the repository at this point in the history
…ype and after the document is successfully uploaded, use the ChunkMethodModal to select the parsing method. and remove ChunkMethodModal from knowledge-file (infiniflow#158)

* feat: select the corresponding parsing method according to the file type

* feat: after the document is successfully uploaded, use the ChunkMethodModal  to select the parsing method.

* feat: add pdf types to ParserListMap

* feat: remove ChunkMethodModal from knowledge-file
  • Loading branch information
cike8899 authored Mar 27, 2024
1 parent a8bc4c1 commit c72d31b
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 219 deletions.
82 changes: 82 additions & 0 deletions web/src/components/chunk-method-modal/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {
useFetchTenantInfo,
useSelectParserList,
} from '@/hooks/userSettingHook';
import { useEffect, useMemo, useState } from 'react';

const ParserListMap = new Map([
[
['pdf'],
[
'naive',
'resume',
'manual',
'paper',
'book',
'laws',
'presentation',
'one',
],
],
[
['doc', 'docx'],
['naive', 'resume', 'book', 'laws', 'one'],
],
[
['xlsx', 'xls'],
['naive', 'qa', 'table', 'one'],
],
[['ppt', 'pptx'], ['presentation']],
[
['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tif', 'tiff', 'webp', 'svg', 'ico'],
['picture'],
],
[['txt'], ['naive', 'resume', 'book', 'laws', 'one', 'qa', 'table']],
[['csv'], ['naive', 'resume', 'book', 'laws', 'one', 'qa', 'table']],
]);

const getParserList = (
values: string[],
parserList: Array<{
value: string;
label: string;
}>,
) => {
return parserList.filter((x) => values?.some((y) => y === x.value));
};

export const useFetchParserListOnMount = (
parserId: string,
documentExtension: string,
) => {
const [selectedTag, setSelectedTag] = useState('');
const parserList = useSelectParserList();

const nextParserList = useMemo(() => {
const key = [...ParserListMap.keys()].find((x) =>
x.some((y) => y === documentExtension),
);
if (key) {
const values = ParserListMap.get(key);
return getParserList(values ?? [], parserList);
}

return getParserList(
['naive', 'resume', 'book', 'laws', 'one', 'qa', 'table'],
parserList,
);
}, [parserList, documentExtension]);

useFetchTenantInfo();

useEffect(() => {
setSelectedTag(parserId);
}, [parserId]);

const handleChange = (tag: string, checked: boolean) => {
const nextSelectedTag = checked ? tag : selectedTag;
setSelectedTag(nextSelectedTag);
};

return { parserList: nextParserList, handleChange, selectedTag };
};
10 changes: 10 additions & 0 deletions web/src/components/chunk-method-modal/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.pageInputNumber {
width: 220px;
}

.questionIcon {
margin-inline-start: 4px;
color: rgba(0, 0, 0, 0.45);
cursor: help;
writing-mode: horizontal-tb;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface IProps extends Omit<IModalManagerChildrenProps, 'showModal'> {
showModal?(): void;
parserId: string;
parserConfig: IKnowledgeFileParserConfig;
documentType: string;
documentExtension: string;
}

const hidePagesChunkMethods = ['qa', 'table', 'picture', 'resume', 'one'];
Expand All @@ -45,11 +45,13 @@ const ChunkMethodModal: React.FC<IProps> = ({
onOk,
hideModal,
visible,
documentType,
documentExtension,
parserConfig,
}) => {
const { parserList, handleChange, selectedTag } =
useFetchParserListOnMount(parserId);
const { parserList, handleChange, selectedTag } = useFetchParserListOnMount(
parserId,
documentExtension,
);
const [form] = Form.useForm();

const handleOk = async () => {
Expand All @@ -62,11 +64,8 @@ const ChunkMethodModal: React.FC<IProps> = ({
};

const showPages = useMemo(() => {
return (
documentType === 'pdf' &&
hidePagesChunkMethods.every((x) => x !== selectedTag)
);
}, [documentType, selectedTag]);
return hidePagesChunkMethods.every((x) => x !== selectedTag);
}, [selectedTag]);

const showOne = useMemo(() => {
return showPages || selectedTag === 'one';
Expand Down Expand Up @@ -114,7 +113,7 @@ const ChunkMethodModal: React.FC<IProps> = ({
</Space>
<Divider></Divider>

{
{documentExtension === 'pdf' && (
<Form name="dynamic_form_nest_item" autoComplete="off" form={form}>
{showPages && (
<>
Expand Down Expand Up @@ -271,7 +270,7 @@ const ChunkMethodModal: React.FC<IProps> = ({

{selectedTag === 'naive' && <MaxTokenNumber></MaxTokenNumber>}
</Form>
}
)}
</Modal>
);
};
Expand Down
45 changes: 45 additions & 0 deletions web/src/hooks/documentHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IChunk, IKnowledgeFile } from '@/interfaces/database/knowledge';
import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
import { api_host } from '@/utils/api';
import { buildChunkHighlights } from '@/utils/documentUtils';
import { UploadFile } from 'antd';
import { useCallback, useMemo, useState } from 'react';
import { IHighlight } from 'react-pdf-highlighter';
import { useDispatch, useSelector } from 'umi';
Expand Down Expand Up @@ -174,3 +175,47 @@ export const useRemoveDocument = (documentId: string) => {

return removeDocument;
};

export const useUploadDocument = () => {
const dispatch = useDispatch();
const { knowledgeId } = useGetKnowledgeSearchParams();

const uploadDocument = useCallback(
(file: UploadFile) => {
try {
return dispatch<any>({
type: 'kFModel/upload_document',
payload: {
file,
kb_id: knowledgeId,
},
});
} catch (errorInfo) {
console.log('Failed:', errorInfo);
}
},
[dispatch, knowledgeId],
);

return uploadDocument;
};

export const useRunDocument = () => {
const dispatch = useDispatch();

const runDocumentByIds = useCallback(
(ids: string[]) => {
try {
return dispatch<any>({
type: 'kFModel/document_run',
payload: { doc_ids: ids, run: 1 },
});
} catch (errorInfo) {
console.log('Failed:', errorInfo);
}
},
[dispatch],
);

return runDocumentByIds;
};
47 changes: 47 additions & 0 deletions web/src/hooks/logicHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { IKnowledgeFile } from '@/interfaces/database/knowledge';
import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
import { useCallback, useState } from 'react';
import { useSetModalState } from './commonHooks';
import { useSetDocumentParser } from './documentHooks';
import { useOneNamespaceEffectsLoading } from './storeHooks';

export const useChangeDocumentParser = (documentId: string) => {
const setDocumentParser = useSetDocumentParser();

const {
visible: changeParserVisible,
hideModal: hideChangeParserModal,
showModal: showChangeParserModal,
} = useSetModalState();
const loading = useOneNamespaceEffectsLoading('kFModel', [
'document_change_parser',
]);

const onChangeParserOk = useCallback(
async (parserId: string, parserConfig: IChangeParserConfigRequestBody) => {
const ret = await setDocumentParser(parserId, documentId, parserConfig);
if (ret === 0) {
hideChangeParserModal();
}
},
[hideChangeParserModal, setDocumentParser, documentId],
);

return {
changeParserLoading: loading,
onChangeParserOk,
changeParserVisible,
hideChangeParserModal,
showChangeParserModal,
};
};

export const useSetSelectedRecord = <T = IKnowledgeFile>() => {
const [currentRecord, setCurrentRecord] = useState<T>({} as T);

const setRecord = (record: T) => {
setCurrentRecord(record);
};

return { currentRecord, setRecord };
};
Loading

0 comments on commit c72d31b

Please sign in to comment.