index.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import type { FC } from 'react'
  2. import { Fragment, useState } from 'react'
  3. import { Popover, Transition } from '@headlessui/react'
  4. import { useTranslation } from 'react-i18next'
  5. import _ from 'lodash-es'
  6. import cn from 'classnames'
  7. import type { BackendModel, ProviderEnum } from '@/app/components/header/account-setting/model-page/declarations'
  8. import { ModelType } from '@/app/components/header/account-setting/model-page/declarations'
  9. import { ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows'
  10. import { Check, SearchLg } from '@/app/components/base/icons/src/vender/line/general'
  11. import { XCircle } from '@/app/components/base/icons/src/vender/solid/general'
  12. import { AlertCircle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback'
  13. import Tooltip from '@/app/components/base/tooltip'
  14. import ModelIcon from '@/app/components/app/configuration/config-model/model-icon'
  15. import ModelName, { supportI18nModelName } from '@/app/components/app/configuration/config-model/model-name'
  16. import ProviderName from '@/app/components/app/configuration/config-model/provider-name'
  17. import { useProviderContext } from '@/context/provider-context'
  18. type Props = {
  19. value: {
  20. providerName: ProviderEnum
  21. modelName: string
  22. } | undefined
  23. modelType: ModelType
  24. supportAgentThought?: boolean
  25. onChange: (value: BackendModel) => void
  26. popClassName?: string
  27. readonly?: boolean
  28. triggerIconSmall?: boolean
  29. }
  30. const ModelSelector: FC<Props> = ({
  31. value,
  32. modelType,
  33. supportAgentThought,
  34. onChange,
  35. popClassName,
  36. readonly,
  37. triggerIconSmall,
  38. }) => {
  39. const { t } = useTranslation()
  40. const { textGenerationModelList, embeddingsModelList, speech2textModelList, agentThoughtModelList } = useProviderContext()
  41. const [search, setSearch] = useState('')
  42. const modelList = supportAgentThought
  43. ? agentThoughtModelList
  44. : ({
  45. [ModelType.textGeneration]: textGenerationModelList,
  46. [ModelType.embeddings]: embeddingsModelList,
  47. [ModelType.speech2text]: speech2textModelList,
  48. })[modelType]
  49. const currModel = modelList.find(item => item.model_name === value?.modelName && item.model_provider.provider_name === value.providerName)
  50. const allModelNames = (() => {
  51. if (!search)
  52. return {}
  53. const res: Record<string, string> = {}
  54. modelList.forEach(({ model_name }) => {
  55. res[model_name] = supportI18nModelName.includes(model_name) ? t(`common.modelName.${model_name}`) : model_name
  56. })
  57. return res
  58. })()
  59. const filteredModelList = search
  60. ? modelList.filter(({ model_name }) => {
  61. if (allModelNames[model_name].includes(search))
  62. return true
  63. return false
  64. })
  65. : modelList
  66. const hasRemoved = value && !modelList.find(({ model_name }) => model_name === value.modelName)
  67. const modelOptions: any[] = (() => {
  68. const providers = _.uniq(filteredModelList.map(item => item.model_provider.provider_name))
  69. const res: any[] = []
  70. providers.forEach((providerName) => {
  71. res.push({
  72. type: 'provider',
  73. value: providerName,
  74. })
  75. const models = filteredModelList.filter(m => m.model_provider.provider_name === providerName)
  76. models.forEach(({ model_name, model_display_name }) => {
  77. res.push({
  78. type: 'model',
  79. providerName,
  80. value: model_name,
  81. modelDisplayName: model_display_name,
  82. })
  83. })
  84. })
  85. return res
  86. })()
  87. return (
  88. <div className=''>
  89. <Popover className='relative'>
  90. <Popover.Button className={cn('flex items-center px-2.5 w-full h-9 rounded-lg', readonly ? '!cursor-auto' : 'bg-gray-100', hasRemoved && '!bg-[#FEF3F2]')}>
  91. {
  92. ({ open }) => (
  93. <>
  94. {
  95. value
  96. ? (
  97. <>
  98. <ModelIcon
  99. className={cn('mr-1.5', !triggerIconSmall && 'w-5 h-5')}
  100. modelId={value.modelName}
  101. providerName={value.providerName}
  102. />
  103. <div className='mr-1.5 grow text-left text-sm text-gray-900 truncate'><ModelName modelId={value.modelName} modelDisplayName={currModel?.model_display_name} /></div>
  104. </>
  105. )
  106. : (
  107. <div className='grow text-left text-sm text-gray-800 opacity-60'>{t('common.modelProvider.selectModel')}</div>
  108. )
  109. }
  110. {
  111. hasRemoved && (
  112. <Tooltip
  113. selector='model-selector-remove-tip'
  114. htmlContent={
  115. <div className='w-[261px] text-gray-500'>{t('common.modelProvider.selector.tip')}</div>
  116. }
  117. >
  118. <AlertCircle className='mr-1 w-4 h-4 text-[#F04438]' />
  119. </Tooltip>
  120. )
  121. }
  122. {!readonly && <ChevronDown className={`w-4 h-4 text-gray-700 ${open ? 'opacity-100' : 'opacity-60'}`} />}
  123. </>
  124. )
  125. }
  126. </Popover.Button>
  127. {!readonly && (
  128. <Transition
  129. as={Fragment}
  130. leave='transition ease-in duration-100'
  131. leaveFrom='opacity-100'
  132. leaveTo='opacity-0'
  133. >
  134. <Popover.Panel className={cn(popClassName, 'absolute top-10 p-1 min-w-[232px] max-w-[260px] max-h-[366px] bg-white border-[0.5px] border-gray-200 rounded-lg shadow-lg overflow-auto z-10')}>
  135. <div className='px-2 pt-2 pb-1'>
  136. <div className='flex items-center px-2 h-8 bg-gray-100 rounded-lg'>
  137. <div className='mr-1.5 p-[1px]'><SearchLg className='w-[14px] h-[14px] text-gray-400' /></div>
  138. <div className='grow px-0.5'>
  139. <input
  140. value={search}
  141. onChange={e => setSearch(e.target.value)}
  142. className={`
  143. block w-full h-8 bg-transparent text-[13px] text-gray-700
  144. outline-none appearance-none border-none
  145. `}
  146. placeholder={t('common.modelProvider.searchModel') || ''}
  147. />
  148. </div>
  149. {
  150. search && (
  151. <div className='ml-1 p-0.5 cursor-pointer' onClick={() => setSearch('')}>
  152. <XCircle className='w-3 h-3 text-gray-400' />
  153. </div>
  154. )
  155. }
  156. </div>
  157. </div>
  158. {
  159. modelOptions.map((model: any) => {
  160. if (model.type === 'provider') {
  161. return (
  162. <div
  163. className='px-3 pt-2 pb-1 text-xs font-medium text-gray-500'
  164. key={`${model.type}-${model.value}`}
  165. >
  166. <ProviderName provideName={model.value} />
  167. </div>
  168. )
  169. }
  170. if (model.type === 'model') {
  171. return (
  172. <Popover.Button
  173. key={`${model.providerName}-${model.value}`}
  174. className={`
  175. flex items-center px-3 w-full h-8 rounded-lg hover:bg-gray-50
  176. ${!readonly ? 'cursor-pointer' : 'cursor-auto'}
  177. ${(value?.providerName === model.providerName && value?.modelName === model.value) && 'bg-gray-50'}
  178. `}
  179. onClick={() => {
  180. const selectedModel = modelList.find((item) => {
  181. return item.model_name === model.value && item.model_provider.provider_name === model.providerName
  182. })
  183. onChange(selectedModel as BackendModel)
  184. }}
  185. >
  186. <ModelIcon
  187. className='mr-2 shrink-0'
  188. modelId={model.value}
  189. providerName={model.providerName}
  190. />
  191. <div className='grow text-left text-sm text-gray-900 truncate'><ModelName modelId={model.value} modelDisplayName={model.modelDisplayName} /></div>
  192. { (value?.providerName === model.providerName && value?.modelName === model.value) && <Check className='shrink-0 w-4 h-4 text-primary-600' /> }
  193. </Popover.Button>
  194. )
  195. }
  196. return null
  197. })
  198. }
  199. {(search && filteredModelList.length === 0) && (
  200. <div className='px-3 pt-1.5 h-[30px] text-center text-xs text-gray-500'>{t('common.modelProvider.noModelFound', { model: search })}</div>
  201. )}
  202. </Popover.Panel>
  203. </Transition>
  204. )}
  205. </Popover>
  206. </div>
  207. )
  208. }
  209. export default ModelSelector