InfoModal.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import * as React from 'react';
  2. import { Modal, Spin, Form, Input, Select, message } from 'antd';
  3. import { apis, CreateOrModifyKnowledgeLibApiParams } from '@/apis';
  4. const FormItem = Form.Item;
  5. const { Option } = Select;
  6. const { TextArea } = Input;
  7. interface Props {
  8. id: string,
  9. open: boolean,
  10. onClickConfirm: (id: string, data: CreateOrModifyKnowledgeLibApiParams) => Promise<any>,
  11. onClickCancel: () => void,
  12. };
  13. const InfoModal: React.FC<Props> = (props: Props) => {
  14. const {
  15. id,
  16. open,
  17. onClickConfirm,
  18. onClickCancel
  19. } = props;
  20. const [form] = Form.useForm();
  21. const [loading, setLoading] = React.useState<boolean>(false);
  22. type EmbeddingList = {
  23. label: string,
  24. value: string,
  25. }[];
  26. type AppTypeList = {
  27. label: string,
  28. value: string,
  29. children: {
  30. label: string,
  31. value: string,
  32. }[],
  33. }[];
  34. const [embeddingList, setEmbeddingList] = React.useState<EmbeddingList>([]);
  35. const [appVisibleList, setAppVisibleList] = React.useState<AppTypeList>([]); // 是否公开
  36. const getTitle = () => {
  37. if (id) {
  38. return '修改知识库';
  39. } else {
  40. return '创建知识库';
  41. }
  42. };
  43. // 获取向量化模型列表
  44. const fetchEmbeddingList = async () => {
  45. try {
  46. const res = await apis.fetchEmbeddingList();
  47. const list = res.data.list.map((item: any) => {
  48. return {
  49. label: item.name,
  50. value: item.id,
  51. }
  52. });
  53. setEmbeddingList(list);
  54. } catch (error: any) {
  55. console.error(error);
  56. }
  57. }
  58. // 获取知识库详情
  59. const fetchDetail = async () => {
  60. try {
  61. const res = await apis.fetchTakaiKnowledgeLibDetail(props.id);
  62. const { name, embeddingId, description,visible } = res.data;
  63. form.setFieldsValue({
  64. name: name,
  65. embeddingId: embeddingId,
  66. description: description,
  67. visible: visible, // 是否公开
  68. });
  69. } catch (error: any) {
  70. message.error(error.msg);
  71. }
  72. };
  73. // 获取是否公开类型
  74. const fetchAppVisible = async (id: string) => {
  75. try {
  76. const res = await apis.fetchTakaiAppTypeList('app_visible');
  77. console.log('res.data', res.data)
  78. const list = res.data.map((item: any) => {
  79. return {
  80. label: item.dictLabel,
  81. value: item.dictValue,
  82. }
  83. });
  84. setAppVisibleList(list);
  85. if (!id) {
  86. form.setFieldsValue({
  87. visible: list[0]?.value
  88. });
  89. }
  90. } catch (error: any) {
  91. console.error(error);
  92. }
  93. }
  94. const init = async () => {
  95. setLoading(true);
  96. // await fetchEmbeddingList();
  97. if (props.id) {
  98. await fetchDetail();
  99. }
  100. await fetchAppVisible(props.id);
  101. setLoading(false);
  102. };
  103. React.useEffect(() => {
  104. init();
  105. }, []);
  106. // 点击确定
  107. const handleClickConfirm = () => {
  108. form.validateFields().then(async (values) => {
  109. const data = { ...values };
  110. await onClickConfirm(props.id, data);
  111. }).catch((error) => {
  112. console.error(error);
  113. });
  114. };
  115. return (
  116. <Modal
  117. width={500}
  118. title={getTitle()}
  119. destroyOnClose={true}
  120. maskClosable={false}
  121. centered={true}
  122. open={open}
  123. onOk={handleClickConfirm}
  124. onCancel={onClickCancel}
  125. >
  126. <Spin spinning={loading}>
  127. <Form form={form} layout='vertical'>
  128. <FormItem
  129. label='知识库名称'
  130. name='name'
  131. rules={[{ required: true, message: '知识库名称不能为空', whitespace: true }]}
  132. >
  133. <Input placeholder='请输入知识库名称' />
  134. </FormItem>
  135. <FormItem
  136. label='向量化模型'
  137. name='embeddingId'
  138. rules={[{ required: true, message: '向量化模型不能为空' }]}
  139. >
  140. {
  141. getTitle() === '创建知识库' &&
  142. <Select
  143. style={{ width: '100%' }}
  144. placeholder='请选择向量化模型'
  145. allowClear={true}
  146. >
  147. <Option value='multilingual-e5-large-instruct'>multilingual-e5-large-instruct</Option>
  148. </Select>
  149. }
  150. {
  151. getTitle() === '修改知识库' &&
  152. <Select
  153. style={{ width: '100%' }}
  154. placeholder='请选择向量化模型'
  155. allowClear={true}
  156. disabled={true}
  157. >
  158. <Option value='multilingual-e5-large-instruct'>multilingual-e5-large-instruct</Option>
  159. </Select>
  160. }
  161. </FormItem>
  162. <FormItem
  163. label='是否公开'
  164. name='visible'
  165. >
  166. <Select
  167. placeholder='请选择是否公开'
  168. allowClear={true}
  169. >
  170. {
  171. appVisibleList.map((item, index) => {
  172. return <Option value={item.value} key={index}>
  173. {item.label}
  174. </Option>
  175. })
  176. }
  177. </Select>
  178. </FormItem>
  179. <FormItem
  180. label='知识库描述'
  181. name='description'
  182. rules={[{ required: true, message: '知识库描述不能为空', whitespace: true }]}
  183. >
  184. <TextArea
  185. placeholder='知识库描述需要描述清楚知识库中的文件内容,搭建应用时模型会将此作为判断是否获取知识库内容的依据'
  186. showCount={true}
  187. rows={4}
  188. maxLength={100}
  189. />
  190. </FormItem>
  191. <div style={{ width: '100%', height: 10 }}></div>
  192. </Form>
  193. </Spin>
  194. </Modal>
  195. );
  196. };
  197. export default InfoModal;