Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import {
  2. Dialog,
  3. DialogContent,
  4. DialogFooter,
  5. DialogHeader,
  6. DialogTitle,
  7. } from '@/components/ui/dialog';
  8. import {
  9. Form,
  10. FormControl,
  11. FormField,
  12. FormItem,
  13. FormLabel,
  14. FormMessage,
  15. } from '@/components/ui/form';
  16. import { DocumentParserType } from '@/constants/knowledge';
  17. import { useFetchKnowledgeBaseConfiguration } from '@/hooks/use-knowledge-request';
  18. import { IModalProps } from '@/interfaces/common';
  19. import { IParserConfig } from '@/interfaces/database/document';
  20. import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
  21. import { zodResolver } from '@hookform/resolvers/zod';
  22. import get from 'lodash/get';
  23. import omit from 'lodash/omit';
  24. import {} from 'module';
  25. import { useEffect, useMemo } from 'react';
  26. import { useForm, useWatch } from 'react-hook-form';
  27. import { useTranslation } from 'react-i18next';
  28. import { z } from 'zod';
  29. import {
  30. AutoKeywordsFormField,
  31. AutoQuestionsFormField,
  32. } from '../auto-keywords-form-field';
  33. import { DatasetConfigurationContainer } from '../dataset-configuration-container';
  34. import { DelimiterFormField } from '../delimiter-form-field';
  35. import { EntityTypesFormField } from '../entity-types-form-field';
  36. import { ExcelToHtmlFormField } from '../excel-to-html-form-field';
  37. import { LayoutRecognizeFormField } from '../layout-recognize-form-field';
  38. import { MaxTokenNumberFormField } from '../max-token-number-from-field';
  39. import {
  40. UseGraphRagFormField,
  41. showGraphRagItems,
  42. } from '../parse-configuration/graph-rag-form-fields';
  43. import RaptorFormFields, {
  44. showRaptorParseConfiguration,
  45. } from '../parse-configuration/raptor-form-fields';
  46. import { ButtonLoading } from '../ui/button';
  47. import { Input } from '../ui/input';
  48. import { RAGFlowSelect } from '../ui/select';
  49. import { DynamicPageRange } from './dynamic-page-range';
  50. import { useFetchParserListOnMount, useShowAutoKeywords } from './hooks';
  51. import {
  52. useDefaultParserValues,
  53. useFillDefaultValueOnMount,
  54. } from './use-default-parser-values';
  55. const FormId = 'ChunkMethodDialogForm';
  56. interface IProps
  57. extends IModalProps<{
  58. parserId: string;
  59. parserConfig: IChangeParserConfigRequestBody;
  60. }> {
  61. loading: boolean;
  62. parserId: string;
  63. parserConfig: IParserConfig;
  64. documentExtension: string;
  65. documentId: string;
  66. }
  67. const hidePagesChunkMethods = [
  68. DocumentParserType.Qa,
  69. DocumentParserType.Table,
  70. DocumentParserType.Picture,
  71. DocumentParserType.Resume,
  72. DocumentParserType.One,
  73. DocumentParserType.KnowledgeGraph,
  74. ];
  75. export function ChunkMethodDialog({
  76. hideModal,
  77. onOk,
  78. parserId,
  79. documentId,
  80. documentExtension,
  81. visible,
  82. parserConfig,
  83. loading,
  84. }: IProps) {
  85. const { t } = useTranslation();
  86. const { parserList } = useFetchParserListOnMount(
  87. documentId,
  88. parserId,
  89. documentExtension,
  90. // form,
  91. );
  92. const { data: knowledgeDetails } = useFetchKnowledgeBaseConfiguration();
  93. const useGraphRag = useMemo(() => {
  94. return knowledgeDetails.parser_config?.graphrag?.use_graphrag;
  95. }, [knowledgeDetails.parser_config?.graphrag?.use_graphrag]);
  96. const defaultParserValues = useDefaultParserValues();
  97. const fillDefaultParserValue = useFillDefaultValueOnMount();
  98. const FormSchema = z.object({
  99. parser_id: z
  100. .string()
  101. .min(1, {
  102. message: t('common.pleaseSelect'),
  103. })
  104. .trim(),
  105. parser_config: z.object({
  106. task_page_size: z.coerce.number().optional(),
  107. layout_recognize: z.string().optional(),
  108. chunk_token_num: z.coerce.number().optional(),
  109. delimiter: z.string().optional(),
  110. auto_keywords: z.coerce.number().optional(),
  111. auto_questions: z.coerce.number().optional(),
  112. html4excel: z.boolean().optional(),
  113. raptor: z
  114. .object({
  115. use_raptor: z.boolean().optional(),
  116. prompt: z.string().optional().optional(),
  117. max_token: z.coerce.number().optional(),
  118. threshold: z.coerce.number().optional(),
  119. max_cluster: z.coerce.number().optional(),
  120. random_seed: z.coerce.number().optional(),
  121. })
  122. .optional(),
  123. graphrag: z.object({
  124. use_graphrag: z.boolean().optional(),
  125. }),
  126. entity_types: z.array(z.string()).optional(),
  127. pages: z
  128. .array(z.object({ from: z.coerce.number(), to: z.coerce.number() }))
  129. .optional(),
  130. }),
  131. });
  132. const form = useForm<z.infer<typeof FormSchema>>({
  133. resolver: zodResolver(FormSchema),
  134. defaultValues: {
  135. parser_id: parserId,
  136. parser_config: defaultParserValues,
  137. },
  138. });
  139. const layoutRecognize = useWatch({
  140. name: 'parser_config.layout_recognize',
  141. control: form.control,
  142. });
  143. const selectedTag = useWatch({
  144. name: 'parser_id',
  145. control: form.control,
  146. });
  147. const isPdf = documentExtension === 'pdf';
  148. const showPages = useMemo(() => {
  149. return isPdf && hidePagesChunkMethods.every((x) => x !== selectedTag);
  150. }, [selectedTag, isPdf]);
  151. const showOne = useMemo(() => {
  152. return (
  153. isPdf &&
  154. hidePagesChunkMethods
  155. .filter((x) => x !== DocumentParserType.One)
  156. .every((x) => x !== selectedTag)
  157. );
  158. }, [selectedTag, isPdf]);
  159. const showMaxTokenNumber =
  160. selectedTag === DocumentParserType.Naive ||
  161. selectedTag === DocumentParserType.KnowledgeGraph;
  162. const showEntityTypes = selectedTag === DocumentParserType.KnowledgeGraph;
  163. const showExcelToHtml =
  164. selectedTag === DocumentParserType.Naive && documentExtension === 'xlsx';
  165. const showAutoKeywords = useShowAutoKeywords();
  166. async function onSubmit(data: z.infer<typeof FormSchema>) {
  167. console.log('🚀 ~ onSubmit ~ data:', data);
  168. const nextData = {
  169. ...data,
  170. parser_config: {
  171. ...data.parser_config,
  172. pages: data.parser_config?.pages?.map((x: any) => [x.from, x.to]) ?? [],
  173. },
  174. };
  175. console.log('🚀 ~ onSubmit ~ nextData:', nextData);
  176. const ret = await onOk?.(nextData);
  177. if (ret) {
  178. hideModal?.();
  179. }
  180. }
  181. useEffect(() => {
  182. if (visible) {
  183. const pages =
  184. parserConfig?.pages?.map((x) => ({ from: x[0], to: x[1] })) ?? [];
  185. form.reset({
  186. parser_id: parserId,
  187. parser_config: fillDefaultParserValue({
  188. pages: pages.length > 0 ? pages : [{ from: 1, to: 1024 }],
  189. ...omit(parserConfig, 'pages'),
  190. graphrag: {
  191. use_graphrag: get(
  192. parserConfig,
  193. 'graphrag.use_graphrag',
  194. useGraphRag,
  195. ),
  196. },
  197. }),
  198. });
  199. }
  200. }, [
  201. fillDefaultParserValue,
  202. form,
  203. knowledgeDetails.parser_config,
  204. parserConfig,
  205. parserId,
  206. useGraphRag,
  207. visible,
  208. ]);
  209. return (
  210. <Dialog open onOpenChange={hideModal}>
  211. <DialogContent className="max-w-[50vw]">
  212. <DialogHeader>
  213. <DialogTitle>{t('knowledgeDetails.chunkMethod')}</DialogTitle>
  214. </DialogHeader>
  215. <Form {...form}>
  216. <form
  217. onSubmit={form.handleSubmit(onSubmit)}
  218. className="space-y-6 max-h-[70vh] overflow-auto"
  219. id={FormId}
  220. >
  221. <FormField
  222. control={form.control}
  223. name="parser_id"
  224. render={({ field }) => (
  225. <FormItem>
  226. <FormLabel>{t('knowledgeDetails.chunkMethod')}</FormLabel>
  227. <FormControl>
  228. <RAGFlowSelect
  229. {...field}
  230. options={parserList}
  231. ></RAGFlowSelect>
  232. </FormControl>
  233. <FormMessage />
  234. </FormItem>
  235. )}
  236. />
  237. {showPages && <DynamicPageRange></DynamicPageRange>}
  238. {showPages && layoutRecognize && (
  239. <FormField
  240. control={form.control}
  241. name="parser_config.task_page_size"
  242. render={({ field }) => (
  243. <FormItem>
  244. <FormLabel tooltip={t('knowledgeDetails.taskPageSizeTip')}>
  245. {t('knowledgeDetails.taskPageSize')}
  246. </FormLabel>
  247. <FormControl>
  248. <Input
  249. {...field}
  250. type={'number'}
  251. min={1}
  252. max={128}
  253. ></Input>
  254. </FormControl>
  255. <FormMessage />
  256. </FormItem>
  257. )}
  258. />
  259. )}
  260. <DatasetConfigurationContainer
  261. show={showOne || showMaxTokenNumber}
  262. className="space-y-3"
  263. >
  264. {showOne && <LayoutRecognizeFormField></LayoutRecognizeFormField>}
  265. {showMaxTokenNumber && (
  266. <>
  267. <MaxTokenNumberFormField
  268. max={
  269. selectedTag === DocumentParserType.KnowledgeGraph
  270. ? 8192 * 2
  271. : 2048
  272. }
  273. ></MaxTokenNumberFormField>
  274. <DelimiterFormField></DelimiterFormField>
  275. </>
  276. )}
  277. </DatasetConfigurationContainer>
  278. <DatasetConfigurationContainer
  279. show={showAutoKeywords(selectedTag) || showExcelToHtml}
  280. className="space-y-3"
  281. >
  282. {showAutoKeywords(selectedTag) && (
  283. <>
  284. <AutoKeywordsFormField></AutoKeywordsFormField>
  285. <AutoQuestionsFormField></AutoQuestionsFormField>
  286. </>
  287. )}
  288. {showExcelToHtml && <ExcelToHtmlFormField></ExcelToHtmlFormField>}
  289. </DatasetConfigurationContainer>
  290. {showRaptorParseConfiguration(
  291. selectedTag as DocumentParserType,
  292. ) && (
  293. <DatasetConfigurationContainer>
  294. <RaptorFormFields></RaptorFormFields>
  295. </DatasetConfigurationContainer>
  296. )}
  297. {showGraphRagItems(selectedTag as DocumentParserType) &&
  298. useGraphRag && <UseGraphRagFormField></UseGraphRagFormField>}
  299. {showEntityTypes && <EntityTypesFormField></EntityTypesFormField>}
  300. </form>
  301. </Form>
  302. <DialogFooter>
  303. <ButtonLoading type="submit" form={FormId} loading={loading}>
  304. {t('common.save')}
  305. </ButtonLoading>
  306. </DialogFooter>
  307. </DialogContent>
  308. </Dialog>
  309. );
  310. }