You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { ReactComponent as ChatConfigurationAtom } from '@/assets/svg/chat-configuration-atom.svg';
  2. import { IModalManagerChildrenProps } from '@/components/modal-manager';
  3. import {
  4. ModelVariableType,
  5. settledModelVariableMap,
  6. } from '@/constants/knowledge';
  7. import { IDialog } from '@/interfaces/database/chat';
  8. import { Divider, Flex, Form, Modal, Segmented, UploadFile } from 'antd';
  9. import { SegmentedValue } from 'antd/es/segmented';
  10. import camelCase from 'lodash/camelCase';
  11. import { useEffect, useRef, useState } from 'react';
  12. import { IPromptConfigParameters } from '../interface';
  13. import AssistantSetting from './assistant-setting';
  14. import { useFetchLlmModelOnVisible, useFetchModelId } from './hooks';
  15. import ModelSetting from './model-setting';
  16. import PromptEngine from './prompt-engine';
  17. import { useTranslate } from '@/hooks/commonHooks';
  18. import { getBase64FromUploadFileList } from '@/utils/fileUtil';
  19. import { removeUselessFieldsFromValues } from '@/utils/form';
  20. import styles from './index.less';
  21. const layout = {
  22. labelCol: { span: 9 },
  23. wrapperCol: { span: 15 },
  24. };
  25. const validateMessages = {
  26. required: '${label} is required!',
  27. types: {
  28. email: '${label} is not a valid email!',
  29. number: '${label} is not a valid number!',
  30. },
  31. number: {
  32. range: '${label} must be between ${min} and ${max}',
  33. },
  34. };
  35. enum ConfigurationSegmented {
  36. AssistantSetting = 'Assistant Setting',
  37. PromptEngine = 'Prompt Engine',
  38. ModelSetting = 'Model Setting',
  39. }
  40. const segmentedMap = {
  41. [ConfigurationSegmented.AssistantSetting]: AssistantSetting,
  42. [ConfigurationSegmented.ModelSetting]: ModelSetting,
  43. [ConfigurationSegmented.PromptEngine]: PromptEngine,
  44. };
  45. interface IProps extends IModalManagerChildrenProps {
  46. initialDialog: IDialog;
  47. loading: boolean;
  48. onOk: (dialog: IDialog) => void;
  49. clearDialog: () => void;
  50. }
  51. const ChatConfigurationModal = ({
  52. visible,
  53. hideModal,
  54. initialDialog,
  55. loading,
  56. onOk,
  57. clearDialog,
  58. }: IProps) => {
  59. const [form] = Form.useForm();
  60. const [value, setValue] = useState<ConfigurationSegmented>(
  61. ConfigurationSegmented.AssistantSetting,
  62. );
  63. const promptEngineRef = useRef<Array<IPromptConfigParameters>>([]);
  64. const modelId = useFetchModelId(visible);
  65. const { t } = useTranslate('chat');
  66. const handleOk = async () => {
  67. const values = await form.validateFields();
  68. const nextValues: any = removeUselessFieldsFromValues(
  69. values,
  70. 'llm_setting.',
  71. );
  72. const emptyResponse = nextValues.prompt_config?.empty_response ?? '';
  73. const icon = await getBase64FromUploadFileList(values.icon);
  74. const finalValues = {
  75. dialog_id: initialDialog.id,
  76. ...nextValues,
  77. vector_similarity_weight: 1 - nextValues.vector_similarity_weight,
  78. prompt_config: {
  79. ...nextValues.prompt_config,
  80. parameters: promptEngineRef.current,
  81. empty_response: emptyResponse,
  82. },
  83. icon,
  84. };
  85. onOk(finalValues);
  86. };
  87. const handleCancel = () => {
  88. hideModal();
  89. };
  90. const handleSegmentedChange = (val: SegmentedValue) => {
  91. setValue(val as ConfigurationSegmented);
  92. };
  93. const handleModalAfterClose = () => {
  94. clearDialog();
  95. form.resetFields();
  96. };
  97. useFetchLlmModelOnVisible(visible);
  98. const title = (
  99. <Flex gap={16}>
  100. <ChatConfigurationAtom></ChatConfigurationAtom>
  101. <div>
  102. <b>{t('chatConfiguration')}</b>
  103. <div className={styles.chatConfigurationDescription}>
  104. {t('chatConfigurationDescription')}
  105. </div>
  106. </div>
  107. </Flex>
  108. );
  109. useEffect(() => {
  110. if (visible) {
  111. const icon = initialDialog.icon;
  112. let fileList: UploadFile[] = [];
  113. if (icon) {
  114. fileList = [{ uid: '1', name: 'file', thumbUrl: icon, status: 'done' }];
  115. }
  116. form.setFieldsValue({
  117. ...initialDialog,
  118. llm_setting:
  119. initialDialog.llm_setting ??
  120. settledModelVariableMap[ModelVariableType.Precise],
  121. icon: fileList,
  122. llm_id: initialDialog.llm_id ?? modelId,
  123. vector_similarity_weight:
  124. 1 - (initialDialog.vector_similarity_weight ?? 0.3),
  125. });
  126. }
  127. }, [initialDialog, form, visible, modelId]);
  128. return (
  129. <Modal
  130. title={title}
  131. width={688}
  132. open={visible}
  133. onOk={handleOk}
  134. onCancel={handleCancel}
  135. confirmLoading={loading}
  136. destroyOnClose
  137. afterClose={handleModalAfterClose}
  138. >
  139. <Segmented
  140. size={'large'}
  141. value={value}
  142. onChange={handleSegmentedChange}
  143. options={Object.values(ConfigurationSegmented).map((x) => ({
  144. label: t(camelCase(x)),
  145. value: x,
  146. }))}
  147. block
  148. />
  149. <Divider></Divider>
  150. <Form
  151. {...layout}
  152. name="nest-messages"
  153. form={form}
  154. style={{ maxWidth: 600 }}
  155. validateMessages={validateMessages}
  156. colon={false}
  157. >
  158. {Object.entries(segmentedMap).map(([key, Element]) => (
  159. <Element
  160. key={key}
  161. show={key === value}
  162. form={form}
  163. {...(key === ConfigurationSegmented.ModelSetting
  164. ? { initialLlmSetting: initialDialog.llm_setting, visible }
  165. : {})}
  166. {...(key === ConfigurationSegmented.PromptEngine
  167. ? { ref: promptEngineRef }
  168. : {})}
  169. ></Element>
  170. ))}
  171. </Form>
  172. </Modal>
  173. );
  174. };
  175. export default ChatConfigurationModal;