Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.tsx 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { ReactComponent as MoreModelIcon } from '@/assets/svg/more-model.svg';
  2. import SvgIcon from '@/components/svg-icon';
  3. import { useSetModalState, useTranslate } from '@/hooks/commonHooks';
  4. import {
  5. LlmItem,
  6. useFetchLlmFactoryListOnMount,
  7. useFetchMyLlmListOnMount,
  8. } from '@/hooks/llmHooks';
  9. import {
  10. CloseCircleOutlined,
  11. SettingOutlined,
  12. UserOutlined,
  13. } from '@ant-design/icons';
  14. import {
  15. Avatar,
  16. Button,
  17. Card,
  18. Col,
  19. Collapse,
  20. CollapseProps,
  21. Divider,
  22. Flex,
  23. List,
  24. Row,
  25. Space,
  26. Spin,
  27. Tag,
  28. Tooltip,
  29. Typography,
  30. } from 'antd';
  31. import { useCallback } from 'react';
  32. import SettingTitle from '../components/setting-title';
  33. import { isLocalLlmFactory } from '../utils';
  34. import ApiKeyModal from './api-key-modal';
  35. import {
  36. useHandleDeleteLlm,
  37. useSelectModelProvidersLoading,
  38. useSubmitApiKey,
  39. useSubmitOllama,
  40. useSubmitSystemModelSetting,
  41. } from './hooks';
  42. import styles from './index.less';
  43. import OllamaModal from './ollama-modal';
  44. import SystemModelSettingModal from './system-model-setting-modal';
  45. const IconMap = {
  46. 'Tongyi-Qianwen': 'tongyi',
  47. Moonshot: 'moonshot',
  48. OpenAI: 'openai',
  49. 'ZHIPU-AI': 'zhipu',
  50. 文心一言: 'wenxin',
  51. Ollama: 'ollama',
  52. Xinference: 'xinference',
  53. DeepSeek: 'deepseek',
  54. };
  55. const LlmIcon = ({ name }: { name: string }) => {
  56. const icon = IconMap[name as keyof typeof IconMap];
  57. return icon ? (
  58. <SvgIcon name={`llm/${icon}`} width={48} height={48}></SvgIcon>
  59. ) : (
  60. <Avatar shape="square" size="large" icon={<UserOutlined />} />
  61. );
  62. };
  63. const { Text } = Typography;
  64. interface IModelCardProps {
  65. item: LlmItem;
  66. clickApiKey: (llmFactory: string) => void;
  67. }
  68. const ModelCard = ({ item, clickApiKey }: IModelCardProps) => {
  69. const { visible, switchVisible } = useSetModalState();
  70. const { t } = useTranslate('setting');
  71. const { handleDeleteLlm } = useHandleDeleteLlm(item.name);
  72. const handleApiKeyClick = () => {
  73. clickApiKey(item.name);
  74. };
  75. const handleShowMoreClick = () => {
  76. switchVisible();
  77. };
  78. return (
  79. <List.Item>
  80. <Card className={styles.addedCard}>
  81. <Row align={'middle'}>
  82. <Col span={12}>
  83. <Flex gap={'middle'} align="center">
  84. <LlmIcon name={item.name} />
  85. <Flex vertical gap={'small'}>
  86. <b>{item.name}</b>
  87. <Text>{item.tags}</Text>
  88. </Flex>
  89. </Flex>
  90. </Col>
  91. <Col span={12} className={styles.factoryOperationWrapper}>
  92. <Space size={'middle'}>
  93. <Button onClick={handleApiKeyClick}>
  94. {isLocalLlmFactory(item.name) ? t('addTheModel') : 'API-Key'}
  95. <SettingOutlined />
  96. </Button>
  97. <Button onClick={handleShowMoreClick}>
  98. <Flex gap={'small'}>
  99. {t('showMoreModels')}
  100. <MoreModelIcon />
  101. </Flex>
  102. </Button>
  103. </Space>
  104. </Col>
  105. </Row>
  106. {visible && (
  107. <List
  108. size="small"
  109. dataSource={item.llm}
  110. className={styles.llmList}
  111. renderItem={(item) => (
  112. <List.Item>
  113. <Space>
  114. {item.name} <Tag color="#b8b8b8">{item.type}</Tag>
  115. <Tooltip title={t('delete', { keyPrefix: 'common' })}>
  116. <Button type={'text'} onClick={handleDeleteLlm(item.name)}>
  117. <CloseCircleOutlined style={{ color: '#D92D20' }} />
  118. </Button>
  119. </Tooltip>
  120. </Space>
  121. </List.Item>
  122. )}
  123. />
  124. )}
  125. </Card>
  126. </List.Item>
  127. );
  128. };
  129. const UserSettingModel = () => {
  130. const factoryList = useFetchLlmFactoryListOnMount();
  131. const llmList = useFetchMyLlmListOnMount();
  132. const loading = useSelectModelProvidersLoading();
  133. const {
  134. saveApiKeyLoading,
  135. initialApiKey,
  136. llmFactory,
  137. onApiKeySavingOk,
  138. apiKeyVisible,
  139. hideApiKeyModal,
  140. showApiKeyModal,
  141. } = useSubmitApiKey();
  142. const {
  143. saveSystemModelSettingLoading,
  144. onSystemSettingSavingOk,
  145. systemSettingVisible,
  146. hideSystemSettingModal,
  147. showSystemSettingModal,
  148. } = useSubmitSystemModelSetting();
  149. const { t } = useTranslate('setting');
  150. const {
  151. llmAddingVisible,
  152. hideLlmAddingModal,
  153. showLlmAddingModal,
  154. onLlmAddingOk,
  155. llmAddingLoading,
  156. selectedLlmFactory,
  157. } = useSubmitOllama();
  158. const handleApiKeyClick = useCallback(
  159. (llmFactory: string) => {
  160. if (isLocalLlmFactory(llmFactory)) {
  161. showLlmAddingModal(llmFactory);
  162. } else {
  163. showApiKeyModal({ llm_factory: llmFactory });
  164. }
  165. },
  166. [showApiKeyModal, showLlmAddingModal],
  167. );
  168. const handleAddModel = (llmFactory: string) => () => {
  169. if (isLocalLlmFactory(llmFactory)) {
  170. showLlmAddingModal(llmFactory);
  171. } else {
  172. handleApiKeyClick(llmFactory);
  173. }
  174. };
  175. const items: CollapseProps['items'] = [
  176. {
  177. key: '1',
  178. label: t('addedModels'),
  179. children: (
  180. <List
  181. grid={{ gutter: 16, column: 1 }}
  182. dataSource={llmList}
  183. renderItem={(item) => (
  184. <ModelCard item={item} clickApiKey={handleApiKeyClick}></ModelCard>
  185. )}
  186. />
  187. ),
  188. },
  189. {
  190. key: '2',
  191. label: t('modelsToBeAdded'),
  192. children: (
  193. <List
  194. grid={{
  195. gutter: 24,
  196. xs: 1,
  197. sm: 2,
  198. md: 3,
  199. lg: 4,
  200. xl: 4,
  201. xxl: 8,
  202. }}
  203. dataSource={factoryList}
  204. renderItem={(item) => (
  205. <List.Item>
  206. <Card className={styles.toBeAddedCard}>
  207. <Flex vertical gap={'large'}>
  208. <LlmIcon name={item.name} />
  209. <Flex vertical gap={'middle'}>
  210. <b>{item.name}</b>
  211. <Text>{item.tags}</Text>
  212. </Flex>
  213. </Flex>
  214. <Divider></Divider>
  215. <Button type="link" onClick={handleAddModel(item.name)}>
  216. {t('addTheModel')}
  217. </Button>
  218. </Card>
  219. </List.Item>
  220. )}
  221. />
  222. ),
  223. },
  224. ];
  225. return (
  226. <section id="xx" className={styles.modelWrapper}>
  227. <Spin spinning={loading}>
  228. <section className={styles.modelContainer}>
  229. <SettingTitle
  230. title={t('model')}
  231. description={t('modelDescription')}
  232. showRightButton
  233. clickButton={showSystemSettingModal}
  234. ></SettingTitle>
  235. <Divider></Divider>
  236. <Collapse defaultActiveKey={['1', '2']} ghost items={items} />
  237. </section>
  238. </Spin>
  239. <ApiKeyModal
  240. visible={apiKeyVisible}
  241. hideModal={hideApiKeyModal}
  242. loading={saveApiKeyLoading}
  243. initialValue={initialApiKey}
  244. onOk={onApiKeySavingOk}
  245. llmFactory={llmFactory}
  246. ></ApiKeyModal>
  247. <SystemModelSettingModal
  248. visible={systemSettingVisible}
  249. onOk={onSystemSettingSavingOk}
  250. hideModal={hideSystemSettingModal}
  251. loading={saveSystemModelSettingLoading}
  252. ></SystemModelSettingModal>
  253. <OllamaModal
  254. visible={llmAddingVisible}
  255. hideModal={hideLlmAddingModal}
  256. onOk={onLlmAddingOk}
  257. loading={llmAddingLoading}
  258. llmFactory={selectedLlmFactory}
  259. ></OllamaModal>
  260. </section>
  261. );
  262. };
  263. export default UserSettingModel;