2024-01-02 23:42:00 +08:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2024-10-11 12:26:53 +08:00
|
|
|
import { RiAlertFill } from '@remixicon/react'
|
2024-01-02 23:42:00 +08:00
|
|
|
import SystemModelSelector from './system-model-selector'
|
|
|
|
import ProviderAddedCard, { UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST } from './provider-added-card'
|
|
|
|
import ProviderCard from './provider-card'
|
|
|
|
import type {
|
2024-06-05 00:13:29 +08:00
|
|
|
CustomConfigurationModelFixedFields,
|
2024-01-02 23:42:00 +08:00
|
|
|
ModelProvider,
|
|
|
|
} from './declarations'
|
2024-01-03 00:05:08 +08:00
|
|
|
import {
|
2024-06-05 00:13:29 +08:00
|
|
|
ConfigurationMethodEnum,
|
2024-01-03 00:05:08 +08:00
|
|
|
CustomConfigurationStatusEnum,
|
2024-04-04 15:54:59 +08:00
|
|
|
ModelTypeEnum,
|
2024-01-03 00:05:08 +08:00
|
|
|
} from './declarations'
|
2024-01-02 23:42:00 +08:00
|
|
|
import {
|
|
|
|
useDefaultModel,
|
2024-01-08 18:54:39 +08:00
|
|
|
useUpdateModelList,
|
|
|
|
useUpdateModelProviders,
|
2024-01-02 23:42:00 +08:00
|
|
|
} from './hooks'
|
|
|
|
import { useProviderContext } from '@/context/provider-context'
|
2024-06-05 00:13:29 +08:00
|
|
|
import { useModalContextSelector } from '@/context/modal-context'
|
2024-01-02 23:42:00 +08:00
|
|
|
import { useEventEmitterContextContext } from '@/context/event-emitter'
|
2024-10-11 12:26:53 +08:00
|
|
|
import cn from '@/utils/classnames'
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
const ModelProviderPage = () => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { eventEmitter } = useEventEmitterContextContext()
|
2024-01-08 18:54:39 +08:00
|
|
|
const updateModelProviders = useUpdateModelProviders()
|
|
|
|
const updateModelList = useUpdateModelList()
|
2024-04-04 15:54:59 +08:00
|
|
|
const { data: textGenerationDefaultModel } = useDefaultModel(ModelTypeEnum.textGeneration)
|
|
|
|
const { data: embeddingsDefaultModel } = useDefaultModel(ModelTypeEnum.textEmbedding)
|
|
|
|
const { data: rerankDefaultModel } = useDefaultModel(ModelTypeEnum.rerank)
|
|
|
|
const { data: speech2textDefaultModel } = useDefaultModel(ModelTypeEnum.speech2text)
|
|
|
|
const { data: ttsDefaultModel } = useDefaultModel(ModelTypeEnum.tts)
|
2024-01-02 23:42:00 +08:00
|
|
|
const { modelProviders: providers } = useProviderContext()
|
2024-06-05 00:13:29 +08:00
|
|
|
const setShowModelModal = useModalContextSelector(state => state.setShowModelModal)
|
2024-01-24 01:05:37 +08:00
|
|
|
const defaultModelNotConfigured = !textGenerationDefaultModel && !embeddingsDefaultModel && !speech2textDefaultModel && !rerankDefaultModel && !ttsDefaultModel
|
2024-09-08 13:14:11 +08:00
|
|
|
const [configuredProviders, notConfiguredProviders] = useMemo(() => {
|
|
|
|
const configuredProviders: ModelProvider[] = []
|
|
|
|
const notConfiguredProviders: ModelProvider[] = []
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
providers.forEach((provider) => {
|
2024-01-17 15:02:27 +08:00
|
|
|
if (
|
|
|
|
provider.custom_configuration.status === CustomConfigurationStatusEnum.active
|
|
|
|
|| (
|
|
|
|
provider.system_configuration.enabled === true
|
|
|
|
&& provider.system_configuration.quota_configurations.find(item => item.quota_type === provider.system_configuration.current_quota_type)
|
|
|
|
)
|
|
|
|
)
|
2024-09-08 13:14:11 +08:00
|
|
|
configuredProviders.push(provider)
|
2024-01-02 23:42:00 +08:00
|
|
|
else
|
2024-09-08 13:14:11 +08:00
|
|
|
notConfiguredProviders.push(provider)
|
2024-01-02 23:42:00 +08:00
|
|
|
})
|
|
|
|
|
2024-09-08 13:14:11 +08:00
|
|
|
return [configuredProviders, notConfiguredProviders]
|
2024-01-02 23:42:00 +08:00
|
|
|
}, [providers])
|
|
|
|
|
|
|
|
const handleOpenModal = (
|
|
|
|
provider: ModelProvider,
|
2024-06-05 00:13:29 +08:00
|
|
|
configurateMethod: ConfigurationMethodEnum,
|
|
|
|
CustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields,
|
2024-01-02 23:42:00 +08:00
|
|
|
) => {
|
|
|
|
setShowModelModal({
|
|
|
|
payload: {
|
|
|
|
currentProvider: provider,
|
2024-06-05 00:13:29 +08:00
|
|
|
currentConfigurationMethod: configurateMethod,
|
|
|
|
currentCustomConfigurationModelFixedFields: CustomConfigurationModelFixedFields,
|
2024-01-02 23:42:00 +08:00
|
|
|
},
|
|
|
|
onSaveCallback: () => {
|
2024-01-08 18:54:39 +08:00
|
|
|
updateModelProviders()
|
|
|
|
|
2024-06-05 00:13:29 +08:00
|
|
|
if (configurateMethod === ConfigurationMethodEnum.predefinedModel) {
|
2024-01-08 18:54:39 +08:00
|
|
|
provider.supported_model_types.forEach((type) => {
|
|
|
|
updateModelList(type)
|
|
|
|
})
|
|
|
|
}
|
2024-01-02 23:42:00 +08:00
|
|
|
|
2024-06-05 00:13:29 +08:00
|
|
|
if (configurateMethod === ConfigurationMethodEnum.customizableModel && provider.custom_configuration.status === CustomConfigurationStatusEnum.active) {
|
2024-01-02 23:42:00 +08:00
|
|
|
eventEmitter?.emit({
|
|
|
|
type: UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST,
|
|
|
|
payload: provider.provider,
|
|
|
|
} as any)
|
2024-01-08 18:54:39 +08:00
|
|
|
|
2024-06-05 00:13:29 +08:00
|
|
|
if (CustomConfigurationModelFixedFields?.__model_type)
|
|
|
|
updateModelList(CustomConfigurationModelFixedFields?.__model_type)
|
2024-01-02 23:42:00 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='relative pt-1 -mt-2'>
|
2024-10-11 12:26:53 +08:00
|
|
|
<div className={cn('flex items-center mb-2')}>
|
|
|
|
<div className='grow text-text-primary system-md-semibold'>{t('common.modelProvider.models')}</div>
|
|
|
|
<div className={cn(
|
|
|
|
'shrink-0 relative flex items-center justify-end gap-2 p-0.5 rounded-lg border border-transparent',
|
|
|
|
defaultModelNotConfigured && 'pl-2 bg-components-panel-bg-blur border-components-panel-border shadow-xs',
|
|
|
|
)}>
|
|
|
|
{defaultModelNotConfigured && <div className='absolute top-0 bottom-0 right-0 left-0 opacity-40' style={{ background: 'linear-gradient(92deg, rgba(247, 144, 9, 0.25) 0%, rgba(255, 255, 255, 0.00) 100%)' }}/>}
|
|
|
|
{defaultModelNotConfigured && (
|
|
|
|
<div className='flex items-center gap-1 text-text-primary system-xs-medium'>
|
|
|
|
<RiAlertFill className='w-4 h-4 text-text-warning-secondary' />
|
|
|
|
{t('common.modelProvider.notConfigured')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<SystemModelSelector
|
|
|
|
notConfigured={defaultModelNotConfigured}
|
|
|
|
textGenerationDefaultModel={textGenerationDefaultModel}
|
|
|
|
embeddingsDefaultModel={embeddingsDefaultModel}
|
|
|
|
rerankDefaultModel={rerankDefaultModel}
|
|
|
|
speech2textDefaultModel={speech2textDefaultModel}
|
|
|
|
ttsDefaultModel={ttsDefaultModel}
|
|
|
|
/>
|
|
|
|
</div>
|
2024-01-02 23:42:00 +08:00
|
|
|
</div>
|
|
|
|
{
|
2024-09-08 13:14:11 +08:00
|
|
|
!!configuredProviders?.length && (
|
2024-01-02 23:42:00 +08:00
|
|
|
<div className='pb-3'>
|
|
|
|
{
|
2024-09-08 13:14:11 +08:00
|
|
|
configuredProviders?.map(provider => (
|
2024-01-02 23:42:00 +08:00
|
|
|
<ProviderAddedCard
|
|
|
|
key={provider.provider}
|
|
|
|
provider={provider}
|
2024-06-05 00:13:29 +08:00
|
|
|
onOpenModal={(configurateMethod: ConfigurationMethodEnum, currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields) => handleOpenModal(provider, configurateMethod, currentCustomConfigurationModelFixedFields)}
|
2024-01-02 23:42:00 +08:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
2024-09-08 13:14:11 +08:00
|
|
|
!!notConfiguredProviders?.length && (
|
2024-01-02 23:42:00 +08:00
|
|
|
<>
|
|
|
|
<div className='flex items-center mb-2 text-xs font-semibold text-gray-500'>
|
|
|
|
+ {t('common.modelProvider.addMoreModelProvider')}
|
|
|
|
<span className='grow ml-3 h-[1px] bg-gradient-to-r from-[#f3f4f6]' />
|
|
|
|
</div>
|
|
|
|
<div className='grid grid-cols-3 gap-2'>
|
|
|
|
{
|
2024-09-08 13:14:11 +08:00
|
|
|
notConfiguredProviders?.map(provider => (
|
2024-01-02 23:42:00 +08:00
|
|
|
<ProviderCard
|
|
|
|
key={provider.provider}
|
|
|
|
provider={provider}
|
2024-06-05 00:13:29 +08:00
|
|
|
onOpenModal={(configurateMethod: ConfigurationMethodEnum) => handleOpenModal(provider, configurateMethod)}
|
2024-01-02 23:42:00 +08:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ModelProviderPage
|