index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import ModelSelector from '../model-selector'
  5. import {
  6. useModelList,
  7. useSystemDefaultModelAndModelList,
  8. useUpdateModelList,
  9. } from '../hooks'
  10. import type {
  11. DefaultModel,
  12. DefaultModelResponse,
  13. } from '../declarations'
  14. import { ModelTypeEnum } from '../declarations'
  15. import Tooltip from '@/app/components/base/tooltip'
  16. import { Settings01 } from '@/app/components/base/icons/src/vender/line/general'
  17. import {
  18. PortalToFollowElem,
  19. PortalToFollowElemContent,
  20. PortalToFollowElemTrigger,
  21. } from '@/app/components/base/portal-to-follow-elem'
  22. import Button from '@/app/components/base/button'
  23. import { useProviderContext } from '@/context/provider-context'
  24. import { updateDefaultModel } from '@/service/common'
  25. import { useToastContext } from '@/app/components/base/toast'
  26. import { useAppContext } from '@/context/app-context'
  27. type SystemModelSelectorProps = {
  28. textGenerationDefaultModel: DefaultModelResponse | undefined
  29. embeddingsDefaultModel: DefaultModelResponse | undefined
  30. rerankDefaultModel: DefaultModelResponse | undefined
  31. speech2textDefaultModel: DefaultModelResponse | undefined
  32. ttsDefaultModel: DefaultModelResponse | undefined
  33. }
  34. const SystemModel: FC<SystemModelSelectorProps> = ({
  35. textGenerationDefaultModel,
  36. embeddingsDefaultModel,
  37. rerankDefaultModel,
  38. speech2textDefaultModel,
  39. ttsDefaultModel,
  40. }) => {
  41. const { t } = useTranslation()
  42. const { notify } = useToastContext()
  43. const { isCurrentWorkspaceManager } = useAppContext()
  44. const { textGenerationModelList } = useProviderContext()
  45. const updateModelList = useUpdateModelList()
  46. const { data: embeddingModelList } = useModelList(ModelTypeEnum.textEmbedding)
  47. const { data: rerankModelList } = useModelList(ModelTypeEnum.rerank)
  48. const { data: speech2textModelList } = useModelList(ModelTypeEnum.speech2text)
  49. const { data: ttsModelList } = useModelList(ModelTypeEnum.tts)
  50. const [changedModelTypes, setChangedModelTypes] = useState<ModelTypeEnum[]>([])
  51. const [currentTextGenerationDefaultModel, changeCurrentTextGenerationDefaultModel] = useSystemDefaultModelAndModelList(textGenerationDefaultModel, textGenerationModelList)
  52. const [currentEmbeddingsDefaultModel, changeCurrentEmbeddingsDefaultModel] = useSystemDefaultModelAndModelList(embeddingsDefaultModel, embeddingModelList)
  53. const [currentRerankDefaultModel, changeCurrentRerankDefaultModel] = useSystemDefaultModelAndModelList(rerankDefaultModel, rerankModelList)
  54. const [currentSpeech2textDefaultModel, changeCurrentSpeech2textDefaultModel] = useSystemDefaultModelAndModelList(speech2textDefaultModel, speech2textModelList)
  55. const [currentTTSDefaultModel, changeCurrentTTSDefaultModel] = useSystemDefaultModelAndModelList(ttsDefaultModel, ttsModelList)
  56. const [open, setOpen] = useState(false)
  57. const getCurrentDefaultModelByModelType = (modelType: ModelTypeEnum) => {
  58. if (modelType === ModelTypeEnum.textGeneration)
  59. return currentTextGenerationDefaultModel
  60. else if (modelType === ModelTypeEnum.textEmbedding)
  61. return currentEmbeddingsDefaultModel
  62. else if (modelType === ModelTypeEnum.rerank)
  63. return currentRerankDefaultModel
  64. else if (modelType === ModelTypeEnum.speech2text)
  65. return currentSpeech2textDefaultModel
  66. else if (modelType === ModelTypeEnum.tts)
  67. return currentTTSDefaultModel
  68. return undefined
  69. }
  70. const handleChangeDefaultModel = (modelType: ModelTypeEnum, model: DefaultModel) => {
  71. if (modelType === ModelTypeEnum.textGeneration)
  72. changeCurrentTextGenerationDefaultModel(model)
  73. else if (modelType === ModelTypeEnum.textEmbedding)
  74. changeCurrentEmbeddingsDefaultModel(model)
  75. else if (modelType === ModelTypeEnum.rerank)
  76. changeCurrentRerankDefaultModel(model)
  77. else if (modelType === ModelTypeEnum.speech2text)
  78. changeCurrentSpeech2textDefaultModel(model)
  79. else if (modelType === ModelTypeEnum.tts)
  80. changeCurrentTTSDefaultModel(model)
  81. if (!changedModelTypes.includes(modelType))
  82. setChangedModelTypes([...changedModelTypes, modelType])
  83. }
  84. const handleSave = async () => {
  85. const res = await updateDefaultModel({
  86. url: '/workspaces/current/default-model',
  87. body: {
  88. model_settings: [ModelTypeEnum.textGeneration, ModelTypeEnum.textEmbedding, ModelTypeEnum.rerank, ModelTypeEnum.speech2text, ModelTypeEnum.tts].map((modelType) => {
  89. return {
  90. model_type: modelType,
  91. provider: getCurrentDefaultModelByModelType(modelType)?.provider,
  92. model: getCurrentDefaultModelByModelType(modelType)?.model,
  93. }
  94. }),
  95. },
  96. })
  97. if (res.result === 'success') {
  98. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  99. setOpen(false)
  100. changedModelTypes.forEach((modelType) => {
  101. if (modelType === ModelTypeEnum.textGeneration)
  102. updateModelList(modelType)
  103. else if (modelType === ModelTypeEnum.textEmbedding)
  104. updateModelList(modelType)
  105. else if (modelType === ModelTypeEnum.rerank)
  106. updateModelList(modelType)
  107. else if (modelType === ModelTypeEnum.speech2text)
  108. updateModelList(modelType)
  109. else if (modelType === ModelTypeEnum.tts)
  110. updateModelList(modelType)
  111. })
  112. }
  113. }
  114. return (
  115. <PortalToFollowElem
  116. open={open}
  117. onOpenChange={setOpen}
  118. placement='bottom-end'
  119. offset={{
  120. mainAxis: 4,
  121. crossAxis: 8,
  122. }}
  123. >
  124. <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)}>
  125. <div className={`
  126. flex items-center px-2 h-6 text-xs text-gray-700 cursor-pointer bg-white rounded-md border-[0.5px] border-gray-200 shadow-xs
  127. hover:bg-gray-100 hover:shadow-none
  128. ${open && 'bg-gray-100 shadow-none'}
  129. `}>
  130. <Settings01 className='mr-1 w-3 h-3 text-gray-500' />
  131. {t('common.modelProvider.systemModelSettings')}
  132. </div>
  133. </PortalToFollowElemTrigger>
  134. <PortalToFollowElemContent className='z-50'>
  135. <div className='pt-4 w-[360px] rounded-xl border-[0.5px] border-black/5 bg-white shadow-xl'>
  136. <div className='px-6 py-1'>
  137. <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
  138. {t('common.modelProvider.systemReasoningModel.key')}
  139. <Tooltip
  140. popupContent={
  141. <div className='w-[261px] text-gray-500'>
  142. {t('common.modelProvider.systemReasoningModel.tip')}
  143. </div>
  144. }
  145. triggerClassName='ml-0.5 w-4 h-4 shrink-0'
  146. />
  147. </div>
  148. <div>
  149. <ModelSelector
  150. defaultModel={currentTextGenerationDefaultModel}
  151. modelList={textGenerationModelList}
  152. onSelect={model => handleChangeDefaultModel(ModelTypeEnum.textGeneration, model)}
  153. />
  154. </div>
  155. </div>
  156. <div className='px-6 py-1'>
  157. <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
  158. {t('common.modelProvider.embeddingModel.key')}
  159. <Tooltip
  160. popupContent={
  161. <div className='w-[261px] text-gray-500'>
  162. {t('common.modelProvider.embeddingModel.tip')}
  163. </div>
  164. }
  165. triggerClassName='ml-0.5 w-4 h-4 shrink-0'
  166. />
  167. </div>
  168. <div>
  169. <ModelSelector
  170. defaultModel={currentEmbeddingsDefaultModel}
  171. modelList={embeddingModelList}
  172. onSelect={model => handleChangeDefaultModel(ModelTypeEnum.textEmbedding, model)}
  173. />
  174. </div>
  175. </div>
  176. <div className='px-6 py-1'>
  177. <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
  178. {t('common.modelProvider.rerankModel.key')}
  179. <Tooltip
  180. popupContent={
  181. <div className='w-[261px] text-gray-500'>
  182. {t('common.modelProvider.rerankModel.tip')}
  183. </div>
  184. }
  185. triggerClassName='ml-0.5 w-4 h-4 shrink-0'
  186. />
  187. </div>
  188. <div>
  189. <ModelSelector
  190. defaultModel={currentRerankDefaultModel}
  191. modelList={rerankModelList}
  192. onSelect={model => handleChangeDefaultModel(ModelTypeEnum.rerank, model)}
  193. />
  194. </div>
  195. </div>
  196. <div className='px-6 py-1'>
  197. <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
  198. {t('common.modelProvider.speechToTextModel.key')}
  199. <Tooltip
  200. popupContent={
  201. <div className='w-[261px] text-gray-500'>
  202. {t('common.modelProvider.speechToTextModel.tip')}
  203. </div>
  204. }
  205. triggerClassName='ml-0.5 w-4 h-4 shrink-0'
  206. />
  207. </div>
  208. <div>
  209. <ModelSelector
  210. defaultModel={currentSpeech2textDefaultModel}
  211. modelList={speech2textModelList}
  212. onSelect={model => handleChangeDefaultModel(ModelTypeEnum.speech2text, model)}
  213. />
  214. </div>
  215. </div>
  216. <div className='px-6 py-1'>
  217. <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
  218. {t('common.modelProvider.ttsModel.key')}
  219. <Tooltip
  220. popupContent={
  221. <div className='w-[261px] text-gray-500'>
  222. {t('common.modelProvider.ttsModel.tip')}
  223. </div>
  224. }
  225. triggerClassName='ml-0.5 w-4 h-4 shrink-0'
  226. />
  227. </div>
  228. <div>
  229. <ModelSelector
  230. defaultModel={currentTTSDefaultModel}
  231. modelList={ttsModelList}
  232. onSelect={model => handleChangeDefaultModel(ModelTypeEnum.tts, model)}
  233. />
  234. </div>
  235. </div>
  236. <div className='flex items-center justify-end px-6 py-4'>
  237. <Button
  238. onClick={() => setOpen(false)}
  239. >
  240. {t('common.operation.cancel')}
  241. </Button>
  242. <Button
  243. className='ml-2'
  244. variant='primary'
  245. onClick={handleSave}
  246. disabled={!isCurrentWorkspaceManager}
  247. >
  248. {t('common.operation.save')}
  249. </Button>
  250. </div>
  251. </div>
  252. </PortalToFollowElemContent>
  253. </PortalToFollowElem>
  254. )
  255. }
  256. export default SystemModel