Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.tsx 8.1KB

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