index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { useMemo } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import SystemModelSelector from './system-model-selector'
  4. import ProviderAddedCard, { UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST } from './provider-added-card'
  5. import ProviderCard from './provider-card'
  6. import type {
  7. CustomConfigurationModelFixedFields,
  8. ModelProvider,
  9. } from './declarations'
  10. import {
  11. ConfigurationMethodEnum,
  12. CustomConfigurationStatusEnum,
  13. ModelTypeEnum,
  14. } from './declarations'
  15. import {
  16. useDefaultModel,
  17. useUpdateModelList,
  18. useUpdateModelProviders,
  19. } from './hooks'
  20. import { AlertTriangle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback'
  21. import { useProviderContext } from '@/context/provider-context'
  22. import { useModalContextSelector } from '@/context/modal-context'
  23. import { useEventEmitterContextContext } from '@/context/event-emitter'
  24. const ModelProviderPage = () => {
  25. const { t } = useTranslation()
  26. const { eventEmitter } = useEventEmitterContextContext()
  27. const updateModelProviders = useUpdateModelProviders()
  28. const updateModelList = useUpdateModelList()
  29. const { data: textGenerationDefaultModel } = useDefaultModel(ModelTypeEnum.textGeneration)
  30. const { data: embeddingsDefaultModel } = useDefaultModel(ModelTypeEnum.textEmbedding)
  31. const { data: rerankDefaultModel } = useDefaultModel(ModelTypeEnum.rerank)
  32. const { data: speech2textDefaultModel } = useDefaultModel(ModelTypeEnum.speech2text)
  33. const { data: ttsDefaultModel } = useDefaultModel(ModelTypeEnum.tts)
  34. const { modelProviders: providers } = useProviderContext()
  35. const setShowModelModal = useModalContextSelector(state => state.setShowModelModal)
  36. const defaultModelNotConfigured = !textGenerationDefaultModel && !embeddingsDefaultModel && !speech2textDefaultModel && !rerankDefaultModel && !ttsDefaultModel
  37. const [configuredProviders, notConfiguredProviders] = useMemo(() => {
  38. const configuredProviders: ModelProvider[] = []
  39. const notConfiguredProviders: ModelProvider[] = []
  40. providers.forEach((provider) => {
  41. if (
  42. provider.custom_configuration.status === CustomConfigurationStatusEnum.active
  43. || (
  44. provider.system_configuration.enabled === true
  45. && provider.system_configuration.quota_configurations.find(item => item.quota_type === provider.system_configuration.current_quota_type)
  46. )
  47. )
  48. configuredProviders.push(provider)
  49. else
  50. notConfiguredProviders.push(provider)
  51. })
  52. return [configuredProviders, notConfiguredProviders]
  53. }, [providers])
  54. const handleOpenModal = (
  55. provider: ModelProvider,
  56. configurateMethod: ConfigurationMethodEnum,
  57. CustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields,
  58. ) => {
  59. setShowModelModal({
  60. payload: {
  61. currentProvider: provider,
  62. currentConfigurationMethod: configurateMethod,
  63. currentCustomConfigurationModelFixedFields: CustomConfigurationModelFixedFields,
  64. },
  65. onSaveCallback: () => {
  66. updateModelProviders()
  67. if (configurateMethod === ConfigurationMethodEnum.predefinedModel) {
  68. provider.supported_model_types.forEach((type) => {
  69. updateModelList(type)
  70. })
  71. }
  72. if (configurateMethod === ConfigurationMethodEnum.customizableModel && provider.custom_configuration.status === CustomConfigurationStatusEnum.active) {
  73. eventEmitter?.emit({
  74. type: UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST,
  75. payload: provider.provider,
  76. } as any)
  77. if (CustomConfigurationModelFixedFields?.__model_type)
  78. updateModelList(CustomConfigurationModelFixedFields?.__model_type)
  79. }
  80. },
  81. })
  82. }
  83. return (
  84. <div className='relative pt-1 -mt-2'>
  85. <div className={`flex items-center justify-between mb-2 h-8 ${defaultModelNotConfigured && 'px-3 bg-[#FFFAEB] rounded-lg border border-[#FEF0C7]'}`}>
  86. {
  87. defaultModelNotConfigured
  88. ? (
  89. <div className='flex items-center text-xs font-medium text-gray-700'>
  90. <AlertTriangle className='mr-1 w-3 h-3 text-[#F79009]' />
  91. {t('common.modelProvider.notConfigured')}
  92. </div>
  93. )
  94. : <div className='text-sm font-medium text-gray-800'>{t('common.modelProvider.models')}</div>
  95. }
  96. <SystemModelSelector
  97. textGenerationDefaultModel={textGenerationDefaultModel}
  98. embeddingsDefaultModel={embeddingsDefaultModel}
  99. rerankDefaultModel={rerankDefaultModel}
  100. speech2textDefaultModel={speech2textDefaultModel}
  101. ttsDefaultModel={ttsDefaultModel}
  102. />
  103. </div>
  104. {
  105. !!configuredProviders?.length && (
  106. <div className='pb-3'>
  107. {
  108. configuredProviders?.map(provider => (
  109. <ProviderAddedCard
  110. key={provider.provider}
  111. provider={provider}
  112. onOpenModal={(configurateMethod: ConfigurationMethodEnum, currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields) => handleOpenModal(provider, configurateMethod, currentCustomConfigurationModelFixedFields)}
  113. />
  114. ))
  115. }
  116. </div>
  117. )
  118. }
  119. {
  120. !!notConfiguredProviders?.length && (
  121. <>
  122. <div className='flex items-center mb-2 text-xs font-semibold text-gray-500'>
  123. + {t('common.modelProvider.addMoreModelProvider')}
  124. <span className='grow ml-3 h-[1px] bg-gradient-to-r from-[#f3f4f6]' />
  125. </div>
  126. <div className='grid grid-cols-3 gap-2'>
  127. {
  128. notConfiguredProviders?.map(provider => (
  129. <ProviderCard
  130. key={provider.provider}
  131. provider={provider}
  132. onOpenModal={(configurateMethod: ConfigurationMethodEnum) => handleOpenModal(provider, configurateMethod)}
  133. />
  134. ))
  135. }
  136. </div>
  137. </>
  138. )
  139. }
  140. </div>
  141. )
  142. }
  143. export default ModelProviderPage