diff --git a/web/src/components/edit-tag/index.tsx b/web/src/components/edit-tag/index.tsx index aac8143b400..b336a087814 100644 --- a/web/src/components/edit-tag/index.tsx +++ b/web/src/components/edit-tag/index.tsx @@ -7,11 +7,11 @@ import React, { useEffect, useRef, useState } from 'react'; import styles from './index.less'; interface EditTagsProps { - tags?: string[]; - setTags?: (tags: string[]) => void; + value?: string[]; + onChange?: (tags: string[]) => void; } -const EditTag = ({ tags, setTags }: EditTagsProps) => { +const EditTag = ({ value = [], onChange }: EditTagsProps) => { const { token } = theme.useToken(); const [inputVisible, setInputVisible] = useState(false); const [inputValue, setInputValue] = useState(''); @@ -24,8 +24,8 @@ const EditTag = ({ tags, setTags }: EditTagsProps) => { }, [inputVisible]); const handleClose = (removedTag: string) => { - const newTags = tags?.filter((tag) => tag !== removedTag); - setTags?.(newTags ?? []); + const newTags = value?.filter((tag) => tag !== removedTag); + onChange?.(newTags ?? []); }; const showInput = () => { @@ -37,12 +37,12 @@ const EditTag = ({ tags, setTags }: EditTagsProps) => { }; const handleInputConfirm = () => { - if (inputValue && tags) { + if (inputValue && value) { const newTags = inputValue .split(';') .map((tag) => tag.trim()) - .filter((tag) => tag && !tags.includes(tag)); - setTags?.([...tags, ...newTags]); + .filter((tag) => tag && !value.includes(tag)); + onChange?.([...value, ...newTags]); } setInputVisible(false); setInputValue(''); @@ -64,7 +64,7 @@ const EditTag = ({ tags, setTags }: EditTagsProps) => { ); }; - const tagChild = tags?.map(forMap); + const tagChild = value?.map(forMap); const tagPlusStyle: React.CSSProperties = { background: token.colorBgContainer, diff --git a/web/src/components/entity-types-item.tsx b/web/src/components/entity-types-item.tsx index 46314209b9f..1916cef0910 100644 --- a/web/src/components/entity-types-item.tsx +++ b/web/src/components/entity-types-item.tsx @@ -18,8 +18,6 @@ const EntityTypesItem = () => { label={t('entityTypes')} rules={[{ required: true }]} initialValue={initialEntityTypes} - valuePropName="tags" - trigger="setTags" > diff --git a/web/src/hooks/chunk-hooks.ts b/web/src/hooks/chunk-hooks.ts index 052f2d17c62..cb363ca7568 100644 --- a/web/src/hooks/chunk-hooks.ts +++ b/web/src/hooks/chunk-hooks.ts @@ -194,6 +194,7 @@ export const useFetchChunk = (chunkId?: string): ResponseType => { queryKey: ['fetchChunk'], enabled: !!chunkId, initialData: {}, + gcTime: 0, queryFn: async () => { const data = await kbService.get_chunk({ chunk_id: chunkId, diff --git a/web/src/hooks/knowledge-hooks.ts b/web/src/hooks/knowledge-hooks.ts index 32317adcefe..4d1abdbbd17 100644 --- a/web/src/hooks/knowledge-hooks.ts +++ b/web/src/hooks/knowledge-hooks.ts @@ -20,6 +20,7 @@ import { } from '@tanstack/react-query'; import { useDebounce } from 'ahooks'; import { message } from 'antd'; +import { useState } from 'react'; import { useSearchParams } from 'umi'; import { useHandleSearchChange } from './logic-hooks'; import { useSetPaginationParams } from './route-hook'; @@ -34,9 +35,9 @@ export const useKnowledgeBaseId = (): string => { export const useFetchKnowledgeBaseConfiguration = () => { const knowledgeBaseId = useKnowledgeBaseId(); - const { data, isFetching: loading } = useQuery({ + const { data, isFetching: loading } = useQuery({ queryKey: ['fetchKnowledgeDetail'], - initialData: {}, + initialData: {} as IKnowledge, gcTime: 0, queryFn: async () => { const { data } = await kbService.get_kb_detail({ @@ -341,4 +342,24 @@ export const useTagIsRenaming = () => { return useIsMutating({ mutationKey: ['renameTag'] }) > 0; }; +export const useFetchTagListByKnowledgeIds = () => { + const [knowledgeIds, setKnowledgeIds] = useState([]); + + const { data, isFetching: loading } = useQuery>({ + queryKey: ['fetchTagListByKnowledgeIds'], + enabled: knowledgeIds.length > 0, + initialData: [], + gcTime: 0, // https://tanstack.com/query/latest/docs/framework/react/guides/caching?from=reactQueryV3 + queryFn: async () => { + const { data } = await kbService.listTagByKnowledgeIds({ + kb_ids: knowledgeIds.join(','), + }); + const list = data?.data || []; + return list; + }, + }); + + return { list: data, loading, setKnowledgeIds }; +}; + //#endregion diff --git a/web/src/hooks/logic-hooks.ts b/web/src/hooks/logic-hooks.ts index 5c8afebb1ed..5ec62811750 100644 --- a/web/src/hooks/logic-hooks.ts +++ b/web/src/hooks/logic-hooks.ts @@ -405,30 +405,6 @@ export interface IRemoveMessageById { removeMessageById(messageId: string): void; } -export const useRemoveMessageById = ( - setCurrentConversation: ( - callback: (state: IClientConversation) => IClientConversation, - ) => void, -) => { - const removeMessageById = useCallback( - (messageId: string) => { - setCurrentConversation((pre) => { - const nextMessages = - pre.message?.filter( - (x) => getMessagePureId(x.id) !== getMessagePureId(messageId), - ) ?? []; - return { - ...pre, - message: nextMessages, - }; - }); - }, - [setCurrentConversation], - ); - - return { removeMessageById }; -}; - export const useRemoveMessagesAfterCurrentMessage = ( setCurrentConversation: ( callback: (state: IClientConversation) => IClientConversation, diff --git a/web/src/interfaces/database/knowledge.ts b/web/src/interfaces/database/knowledge.ts index 10a06774f9a..9c7c6fbd647 100644 --- a/web/src/interfaces/database/knowledge.ts +++ b/web/src/interfaces/database/knowledge.ts @@ -11,7 +11,7 @@ export interface IKnowledge { doc_num: number; id: string; name: string; - parser_config: Parserconfig; + parser_config: ParserConfig; parser_id: string; permission: string; similarity_threshold: number; @@ -25,9 +25,22 @@ export interface IKnowledge { nickname?: string; } -export interface Parserconfig { - from_page: number; - to_page: number; +export interface Raptor { + use_raptor: boolean; +} + +export interface ParserConfig { + from_page?: number; + to_page?: number; + auto_keywords?: number; + auto_questions?: number; + chunk_token_num?: number; + delimiter?: string; + html4excel?: boolean; + layout_recognize?: boolean; + raptor?: Raptor; + tag_kb_ids?: string[]; + topn_tags?: number; } export interface IKnowledgeFileParserConfig { @@ -83,8 +96,11 @@ export interface IChunk { doc_id: string; doc_name: string; img_id: string; - important_kwd: any[]; + important_kwd?: string[]; + question_kwd?: string[]; // keywords + tag_kwd?: string[]; positions: number[][]; + tag_feas?: Record; } export interface ITestingChunk { diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts index 9dafaf96070..757ac1ed367 100644 --- a/web/src/locales/en.ts +++ b/web/src/locales/en.ts @@ -338,6 +338,8 @@ This procedure will improve precision of retrieval by adding more information to `, topnTags: 'Top-N Tags', + tags: 'Tags', + addTag: 'Add tag', }, chunk: { chunk: 'Chunk', diff --git a/web/src/locales/zh-traditional.ts b/web/src/locales/zh-traditional.ts index d1db43cf564..270adbef429 100644 --- a/web/src/locales/zh-traditional.ts +++ b/web/src/locales/zh-traditional.ts @@ -322,6 +322,8 @@ export default {
  • 關鍵字由 LLM 生成,既昂貴又耗時。 `, + tags: '標籤', + addTag: '增加標籤', }, chunk: { chunk: '解析塊', diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts index ecc9ef14019..c0b2aacc447 100644 --- a/web/src/locales/zh.ts +++ b/web/src/locales/zh.ts @@ -339,6 +339,8 @@ export default {
  • 关键字由 LLM 生成,这既昂贵又耗时。
  • `, + tags: '标签', + addTag: '增加标签', }, chunk: { chunk: '解析块', diff --git a/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx b/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx index cf1748c81dd..abcf1742c23 100644 --- a/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx +++ b/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/index.tsx @@ -1,15 +1,23 @@ import EditTag from '@/components/edit-tag'; import { useFetchChunk } from '@/hooks/chunk-hooks'; import { IModalProps } from '@/interfaces/common'; -import { DeleteOutlined, QuestionCircleOutlined } from '@ant-design/icons'; -import { Divider, Form, Input, Modal, Space, Switch, Tooltip } from 'antd'; -import React, { useEffect, useState } from 'react'; +import { IChunk } from '@/interfaces/database/knowledge'; +import { DeleteOutlined } from '@ant-design/icons'; +import { Divider, Form, Input, Modal, Space, Switch } from 'antd'; +import React, { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useDeleteChunkByIds } from '../../hooks'; +import { + transformTagFeaturesArrayToObject, + transformTagFeaturesObjectToArray, +} from '../../utils'; +import { TagFeatureItem } from './tag-feature-item'; + +type FieldType = Pick< + IChunk, + 'content_with_weight' | 'tag_kwd' | 'question_kwd' | 'important_kwd' +>; -type FieldType = { - content?: string; -}; interface kFProps { doc_id: string; chunkId: string | undefined; @@ -26,62 +34,48 @@ const ChunkCreatingModal: React.FC & kFProps> = ({ }) => { const [form] = Form.useForm(); const [checked, setChecked] = useState(false); - const [keywords, setKeywords] = useState([]); - const [question, setQuestion] = useState([]); - const [tagKeyWords, setTagKeyWords] = useState([]); const { removeChunk } = useDeleteChunkByIds(); const { data } = useFetchChunk(chunkId); const { t } = useTranslation(); const isTagParser = parserId === 'tag'; - useEffect(() => { - if (data?.code === 0) { - const { - content_with_weight, - important_kwd = [], - available_int, - question_kwd = [], - tag_kwd = [], - } = data.data; - form.setFieldsValue({ content: content_with_weight }); - setKeywords(important_kwd); - setQuestion(question_kwd); - setTagKeyWords(tag_kwd); - setChecked(available_int !== 0); - } - - if (!chunkId) { - setKeywords([]); - setQuestion([]); - setTagKeyWords([]); - form.setFieldsValue({ content: undefined }); - } - }, [data, form, chunkId]); - - const handleOk = async () => { + const handleOk = useCallback(async () => { try { const values = await form.validateFields(); + console.log('🚀 ~ handleOk ~ values:', values); + onOk?.({ - content: values.content, - keywords, // keywords - question_kwd: question, - tag_kwd: tagKeyWords, + ...values, + tag_feas: transformTagFeaturesArrayToObject(values.tag_feas), available_int: checked ? 1 : 0, // available_int }); } catch (errorInfo) { console.log('Failed:', errorInfo); } - }; + }, [checked, form, onOk]); - const handleRemove = () => { + const handleRemove = useCallback(() => { if (chunkId) { return removeChunk([chunkId], doc_id); } - }; - const handleCheck = () => { + }, [chunkId, doc_id, removeChunk]); + + const handleCheck = useCallback(() => { setChecked(!checked); - }; + }, [checked]); + + useEffect(() => { + if (data?.code === 0) { + const { available_int, tag_feas } = data.data; + form.setFieldsValue({ + ...(data.data || {}), + tag_feas: transformTagFeaturesObjectToArray(tag_feas), + }); + + setChecked(available_int !== 0); + } + }, [data, form, chunkId]); return ( & kFProps> = ({
    label={t('chunk.chunk')} - name="content" + name="content_with_weight" rules={[{ required: true, message: t('chunk.chunkMessage') }]} > + + label={t('chunk.keyword')} name="important_kwd"> + + + + label={t('chunk.question')} + name="question_kwd" + tooltip={t('chunk.questionTip')} + > + + + {isTagParser && ( + + label={t('knowledgeConfiguration.tagName')} + name="tag_kwd" + > + + + )} + + {!isTagParser && } -
    -

    {t('chunk.keyword')}

    - -
    -
    -
    - {t('chunk.question')} - - - -
    - -
    - {isTagParser && ( -
    -

    {t('knowledgeConfiguration.tagName')}

    - -
    - )} + {chunkId && (
    diff --git a/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/tag-feature-item.tsx b/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/tag-feature-item.tsx new file mode 100644 index 00000000000..64c192eb79f --- /dev/null +++ b/web/src/pages/add-knowledge/components/knowledge-chunk/components/chunk-creating-modal/tag-feature-item.tsx @@ -0,0 +1,107 @@ +import { + useFetchKnowledgeBaseConfiguration, + useFetchTagListByKnowledgeIds, +} from '@/hooks/knowledge-hooks'; +import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'; +import { Button, Form, InputNumber, Select } from 'antd'; +import { useCallback, useEffect, useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; +import { FormListItem } from '../../utils'; + +const FieldKey = 'tag_feas'; + +export const TagFeatureItem = () => { + const form = Form.useFormInstance(); + const { t } = useTranslation(); + const { data: knowledgeConfiguration } = useFetchKnowledgeBaseConfiguration(); + + const { setKnowledgeIds, list } = useFetchTagListByKnowledgeIds(); + + const tagKnowledgeIds = useMemo(() => { + return knowledgeConfiguration?.parser_config?.tag_kb_ids ?? []; + }, [knowledgeConfiguration?.parser_config?.tag_kb_ids]); + + const options = useMemo(() => { + return list.map((x) => ({ + value: x[0], + label: x[0], + })); + }, [list]); + + const filterOptions = useCallback( + (index: number) => { + const tags: FormListItem[] = form.getFieldValue(FieldKey) ?? []; + + // Exclude it's own current data + const list = tags + .filter((x, idx) => x && index !== idx) + .map((x) => x.tag); + + // Exclude the selected data from other options from one's own options. + return options.filter((x) => !list.some((y) => x.value === y)); + }, + [form, options], + ); + + useEffect(() => { + setKnowledgeIds(tagKnowledgeIds); + }, [setKnowledgeIds, tagKnowledgeIds]); + + return ( + + + {(fields, { add, remove }) => ( + <> + {fields.map(({ key, name, ...restField }) => ( +
    +
    + +