Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

graph-rag-form-fields.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { DocumentParserType } from '@/constants/knowledge';
  2. import { useTranslate } from '@/hooks/common-hooks';
  3. import { cn } from '@/lib/utils';
  4. import { Switch as AntSwitch, Form, Select } from 'antd';
  5. import { upperFirst } from 'lodash';
  6. import { useCallback, useMemo } from 'react';
  7. import { useFormContext } from 'react-hook-form';
  8. import { DatasetConfigurationContainer } from '../dataset-configuration-container';
  9. import EntityTypesItem from '../entity-types-item';
  10. import {
  11. FormControl,
  12. FormField,
  13. FormItem,
  14. FormLabel,
  15. FormMessage,
  16. } from '../ui/form';
  17. import { Switch } from '../ui/switch';
  18. const excludedTagParseMethods = [
  19. DocumentParserType.Table,
  20. DocumentParserType.KnowledgeGraph,
  21. DocumentParserType.Tag,
  22. ];
  23. export const showTagItems = (parserId: DocumentParserType) => {
  24. return !excludedTagParseMethods.includes(parserId);
  25. };
  26. const enum MethodValue {
  27. General = 'general',
  28. Light = 'light',
  29. }
  30. export const excludedParseMethods = [
  31. DocumentParserType.Table,
  32. DocumentParserType.Resume,
  33. DocumentParserType.Picture,
  34. DocumentParserType.KnowledgeGraph,
  35. DocumentParserType.Qa,
  36. DocumentParserType.Tag,
  37. ];
  38. export const showGraphRagItems = (parserId: DocumentParserType | undefined) => {
  39. return !excludedParseMethods.some((x) => x === parserId);
  40. };
  41. type GraphRagItemsProps = {
  42. marginBottom?: boolean;
  43. };
  44. export function UseGraphRagItem() {
  45. const { t } = useTranslate('knowledgeConfiguration');
  46. return (
  47. <Form.Item
  48. name={['parser_config', 'graphrag', 'use_graphrag']}
  49. label={t('useGraphRag')}
  50. initialValue={false}
  51. valuePropName="checked"
  52. tooltip={t('useGraphRagTip')}
  53. >
  54. <AntSwitch />
  55. </Form.Item>
  56. );
  57. }
  58. export function UseGraphRagFormField() {
  59. const form = useFormContext();
  60. const { t } = useTranslate('knowledgeConfiguration');
  61. return (
  62. <FormField
  63. control={form.control}
  64. name="parser_config.graphrag.use_graphrag"
  65. render={({ field }) => (
  66. <FormItem defaultChecked={false}>
  67. <FormLabel tooltip={t('useGraphRagTip')}>
  68. {t('useGraphRag')}
  69. </FormLabel>
  70. <FormControl>
  71. <Switch
  72. checked={field.value}
  73. onCheckedChange={field.onChange}
  74. ></Switch>
  75. </FormControl>
  76. <FormMessage />
  77. </FormItem>
  78. )}
  79. />
  80. );
  81. }
  82. // The three types "table", "resume" and "one" do not display this configuration.
  83. const GraphRagItems = ({ marginBottom = false }: GraphRagItemsProps) => {
  84. const { t } = useTranslate('knowledgeConfiguration');
  85. const methodOptions = useMemo(() => {
  86. return [MethodValue.Light, MethodValue.General].map((x) => ({
  87. value: x,
  88. label: upperFirst(x),
  89. }));
  90. }, []);
  91. const renderWideTooltip = useCallback(
  92. (title: React.ReactNode | string) => {
  93. return {
  94. title: typeof title === 'string' ? t(title) : title,
  95. overlayInnerStyle: { width: '32vw' },
  96. };
  97. },
  98. [t],
  99. );
  100. return (
  101. <DatasetConfigurationContainer className={cn({ 'mb-4': marginBottom })}>
  102. <UseGraphRagItem></UseGraphRagItem>
  103. <Form.Item
  104. shouldUpdate={(prevValues, curValues) =>
  105. prevValues.parser_config.graphrag.use_graphrag !==
  106. curValues.parser_config.graphrag.use_graphrag
  107. }
  108. >
  109. {({ getFieldValue }) => {
  110. const useRaptor = getFieldValue([
  111. 'parser_config',
  112. 'graphrag',
  113. 'use_graphrag',
  114. ]);
  115. return (
  116. useRaptor && (
  117. <>
  118. <EntityTypesItem
  119. field={['parser_config', 'graphrag', 'entity_types']}
  120. ></EntityTypesItem>
  121. <Form.Item
  122. name={['parser_config', 'graphrag', 'method']}
  123. label={t('graphRagMethod')}
  124. tooltip={renderWideTooltip(
  125. <div
  126. dangerouslySetInnerHTML={{
  127. __html: t('graphRagMethodTip'),
  128. }}
  129. ></div>,
  130. )}
  131. initialValue={MethodValue.Light}
  132. >
  133. <Select options={methodOptions} />
  134. </Form.Item>
  135. <Form.Item
  136. name={['parser_config', 'graphrag', 'resolution']}
  137. label={t('resolution')}
  138. tooltip={renderWideTooltip('resolutionTip')}
  139. >
  140. <AntSwitch />
  141. </Form.Item>
  142. <Form.Item
  143. name={['parser_config', 'graphrag', 'community']}
  144. label={t('community')}
  145. tooltip={renderWideTooltip('communityTip')}
  146. >
  147. <AntSwitch />
  148. </Form.Item>
  149. </>
  150. )
  151. );
  152. }}
  153. </Form.Item>
  154. </DatasetConfigurationContainer>
  155. );
  156. };
  157. export default GraphRagItems;