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 = ({ 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([]) 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 ( setOpen(v => !v)}>
{t('common.modelProvider.systemModelSettings')}
{t('common.modelProvider.systemReasoningModel.key')} {t('common.modelProvider.systemReasoningModel.tip')}
} triggerClassName='ml-0.5 w-4 h-4 shrink-0' />
handleChangeDefaultModel(ModelTypeEnum.textGeneration, model)} />
{t('common.modelProvider.embeddingModel.key')} {t('common.modelProvider.embeddingModel.tip')}
} triggerClassName='ml-0.5 w-4 h-4 shrink-0' />
handleChangeDefaultModel(ModelTypeEnum.textEmbedding, model)} />
{t('common.modelProvider.rerankModel.key')} {t('common.modelProvider.rerankModel.tip')}
} triggerClassName='ml-0.5 w-4 h-4 shrink-0' />
handleChangeDefaultModel(ModelTypeEnum.rerank, model)} />
{t('common.modelProvider.speechToTextModel.key')} {t('common.modelProvider.speechToTextModel.tip')}
} triggerClassName='ml-0.5 w-4 h-4 shrink-0' />
handleChangeDefaultModel(ModelTypeEnum.speech2text, model)} />
{t('common.modelProvider.ttsModel.key')} {t('common.modelProvider.ttsModel.tip')}
} triggerClassName='ml-0.5 w-4 h-4 shrink-0' />
handleChangeDefaultModel(ModelTypeEnum.tts, model)} />
) } export default SystemModel