123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import type { FC } from 'react'
- import { useState } from 'react'
- import { useTranslation } from 'react-i18next'
- import ModelSelector from '../model-selector'
- import {
- useModelList,
- useSystemDefaultModelAndModelList,
- useUpdateModelList,
- } from '../hooks'
- import type {
- DefaultModel,
- DefaultModelResponse,
- } from '../declarations'
- import { ModelTypeEnum } from '../declarations'
- import Tooltip from '@/app/components/base/tooltip'
- import { Settings01 } from '@/app/components/base/icons/src/vender/line/general'
- import {
- PortalToFollowElem,
- PortalToFollowElemContent,
- PortalToFollowElemTrigger,
- } from '@/app/components/base/portal-to-follow-elem'
- import Button from '@/app/components/base/button'
- import { useProviderContext } from '@/context/provider-context'
- import { updateDefaultModel } from '@/service/common'
- import { useToastContext } from '@/app/components/base/toast'
- import { useAppContext } from '@/context/app-context'
- type SystemModelSelectorProps = {
- textGenerationDefaultModel: DefaultModelResponse | undefined
- embeddingsDefaultModel: DefaultModelResponse | undefined
- rerankDefaultModel: DefaultModelResponse | undefined
- speech2textDefaultModel: DefaultModelResponse | undefined
- ttsDefaultModel: DefaultModelResponse | undefined
- }
- const SystemModel: FC<SystemModelSelectorProps> = ({
- textGenerationDefaultModel,
- embeddingsDefaultModel,
- rerankDefaultModel,
- speech2textDefaultModel,
- ttsDefaultModel,
- }) => {
- const { t } = useTranslation()
- const { notify } = useToastContext()
- const { isCurrentWorkspaceManager } = useAppContext()
- const { textGenerationModelList } = useProviderContext()
- const updateModelList = useUpdateModelList()
- const { data: embeddingModelList } = useModelList(ModelTypeEnum.textEmbedding)
- const { data: rerankModelList } = useModelList(ModelTypeEnum.rerank)
- const { data: speech2textModelList } = useModelList(ModelTypeEnum.speech2text)
- const { data: ttsModelList } = useModelList(ModelTypeEnum.tts)
- const [changedModelTypes, setChangedModelTypes] = useState<ModelTypeEnum[]>([])
- const [currentTextGenerationDefaultModel, changeCurrentTextGenerationDefaultModel] = useSystemDefaultModelAndModelList(textGenerationDefaultModel, textGenerationModelList)
- const [currentEmbeddingsDefaultModel, changeCurrentEmbeddingsDefaultModel] = useSystemDefaultModelAndModelList(embeddingsDefaultModel, embeddingModelList)
- const [currentRerankDefaultModel, changeCurrentRerankDefaultModel] = useSystemDefaultModelAndModelList(rerankDefaultModel, rerankModelList)
- const [currentSpeech2textDefaultModel, changeCurrentSpeech2textDefaultModel] = useSystemDefaultModelAndModelList(speech2textDefaultModel, speech2textModelList)
- const [currentTTSDefaultModel, changeCurrentTTSDefaultModel] = useSystemDefaultModelAndModelList(ttsDefaultModel, ttsModelList)
- const [open, setOpen] = useState(false)
- const getCurrentDefaultModelByModelType = (modelType: ModelTypeEnum) => {
- if (modelType === ModelTypeEnum.textGeneration)
- return currentTextGenerationDefaultModel
- else if (modelType === ModelTypeEnum.textEmbedding)
- return currentEmbeddingsDefaultModel
- else if (modelType === ModelTypeEnum.rerank)
- return currentRerankDefaultModel
- else if (modelType === ModelTypeEnum.speech2text)
- return currentSpeech2textDefaultModel
- else if (modelType === ModelTypeEnum.tts)
- return currentTTSDefaultModel
- return undefined
- }
- const handleChangeDefaultModel = (modelType: ModelTypeEnum, model: DefaultModel) => {
- if (modelType === ModelTypeEnum.textGeneration)
- changeCurrentTextGenerationDefaultModel(model)
- else if (modelType === ModelTypeEnum.textEmbedding)
- changeCurrentEmbeddingsDefaultModel(model)
- else if (modelType === ModelTypeEnum.rerank)
- changeCurrentRerankDefaultModel(model)
- else if (modelType === ModelTypeEnum.speech2text)
- changeCurrentSpeech2textDefaultModel(model)
- else if (modelType === ModelTypeEnum.tts)
- changeCurrentTTSDefaultModel(model)
- if (!changedModelTypes.includes(modelType))
- setChangedModelTypes([...changedModelTypes, modelType])
- }
- const handleSave = async () => {
- const res = await updateDefaultModel({
- url: '/workspaces/current/default-model',
- body: {
- model_settings: [ModelTypeEnum.textGeneration, ModelTypeEnum.textEmbedding, ModelTypeEnum.rerank, ModelTypeEnum.speech2text, ModelTypeEnum.tts].map((modelType) => {
- return {
- model_type: modelType,
- provider: getCurrentDefaultModelByModelType(modelType)?.provider,
- model: getCurrentDefaultModelByModelType(modelType)?.model,
- }
- }),
- },
- })
- if (res.result === 'success') {
- notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
- setOpen(false)
- changedModelTypes.forEach((modelType) => {
- if (modelType === ModelTypeEnum.textGeneration)
- updateModelList(modelType)
- else if (modelType === ModelTypeEnum.textEmbedding)
- updateModelList(modelType)
- else if (modelType === ModelTypeEnum.rerank)
- updateModelList(modelType)
- else if (modelType === ModelTypeEnum.speech2text)
- updateModelList(modelType)
- else if (modelType === ModelTypeEnum.tts)
- updateModelList(modelType)
- })
- }
- }
- return (
- <PortalToFollowElem
- open={open}
- onOpenChange={setOpen}
- placement='bottom-end'
- offset={{
- mainAxis: 4,
- crossAxis: 8,
- }}
- >
- <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)}>
- <div className={`
- 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
- hover:bg-gray-100 hover:shadow-none
- ${open && 'bg-gray-100 shadow-none'}
- `}>
- <Settings01 className='mr-1 w-3 h-3 text-gray-500' />
- {t('common.modelProvider.systemModelSettings')}
- </div>
- </PortalToFollowElemTrigger>
- <PortalToFollowElemContent className='z-50'>
- <div className='pt-4 w-[360px] rounded-xl border-[0.5px] border-black/5 bg-white shadow-xl'>
- <div className='px-6 py-1'>
- <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
- {t('common.modelProvider.systemReasoningModel.key')}
- <Tooltip
- popupContent={
- <div className='w-[261px] text-gray-500'>
- {t('common.modelProvider.systemReasoningModel.tip')}
- </div>
- }
- triggerClassName='ml-0.5 w-4 h-4 shrink-0'
- />
- </div>
- <div>
- <ModelSelector
- defaultModel={currentTextGenerationDefaultModel}
- modelList={textGenerationModelList}
- onSelect={model => handleChangeDefaultModel(ModelTypeEnum.textGeneration, model)}
- />
- </div>
- </div>
- <div className='px-6 py-1'>
- <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
- {t('common.modelProvider.embeddingModel.key')}
- <Tooltip
- popupContent={
- <div className='w-[261px] text-gray-500'>
- {t('common.modelProvider.embeddingModel.tip')}
- </div>
- }
- triggerClassName='ml-0.5 w-4 h-4 shrink-0'
- />
- </div>
- <div>
- <ModelSelector
- defaultModel={currentEmbeddingsDefaultModel}
- modelList={embeddingModelList}
- onSelect={model => handleChangeDefaultModel(ModelTypeEnum.textEmbedding, model)}
- />
- </div>
- </div>
- <div className='px-6 py-1'>
- <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
- {t('common.modelProvider.rerankModel.key')}
- <Tooltip
- popupContent={
- <div className='w-[261px] text-gray-500'>
- {t('common.modelProvider.rerankModel.tip')}
- </div>
- }
- triggerClassName='ml-0.5 w-4 h-4 shrink-0'
- />
- </div>
- <div>
- <ModelSelector
- defaultModel={currentRerankDefaultModel}
- modelList={rerankModelList}
- onSelect={model => handleChangeDefaultModel(ModelTypeEnum.rerank, model)}
- />
- </div>
- </div>
- <div className='px-6 py-1'>
- <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
- {t('common.modelProvider.speechToTextModel.key')}
- <Tooltip
- popupContent={
- <div className='w-[261px] text-gray-500'>
- {t('common.modelProvider.speechToTextModel.tip')}
- </div>
- }
- triggerClassName='ml-0.5 w-4 h-4 shrink-0'
- />
- </div>
- <div>
- <ModelSelector
- defaultModel={currentSpeech2textDefaultModel}
- modelList={speech2textModelList}
- onSelect={model => handleChangeDefaultModel(ModelTypeEnum.speech2text, model)}
- />
- </div>
- </div>
- <div className='px-6 py-1'>
- <div className='flex items-center h-8 text-[13px] font-medium text-gray-900'>
- {t('common.modelProvider.ttsModel.key')}
- <Tooltip
- popupContent={
- <div className='w-[261px] text-gray-500'>
- {t('common.modelProvider.ttsModel.tip')}
- </div>
- }
- triggerClassName='ml-0.5 w-4 h-4 shrink-0'
- />
- </div>
- <div>
- <ModelSelector
- defaultModel={currentTTSDefaultModel}
- modelList={ttsModelList}
- onSelect={model => handleChangeDefaultModel(ModelTypeEnum.tts, model)}
- />
- </div>
- </div>
- <div className='flex items-center justify-end px-6 py-4'>
- <Button
- onClick={() => setOpen(false)}
- >
- {t('common.operation.cancel')}
- </Button>
- <Button
- className='ml-2'
- variant='primary'
- onClick={handleSave}
- disabled={!isCurrentWorkspaceManager}
- >
- {t('common.operation.save')}
- </Button>
- </div>
- </div>
- </PortalToFollowElemContent>
- </PortalToFollowElem>
- )
- }
- export default SystemModel
|