index.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import type {
  2. FC,
  3. ReactNode,
  4. } from 'react'
  5. import { useMemo, useState } from 'react'
  6. import useSWR from 'swr'
  7. import { useTranslation } from 'react-i18next'
  8. import type {
  9. DefaultModel,
  10. FormValue,
  11. ModelParameterRule,
  12. } from '../declarations'
  13. import { ModelStatusEnum } from '../declarations'
  14. import ModelSelector from '../model-selector'
  15. import {
  16. useTextGenerationCurrentProviderAndModelAndModelList,
  17. } from '../hooks'
  18. import ParameterItem from './parameter-item'
  19. import type { ParameterValue } from './parameter-item'
  20. import Trigger from './trigger'
  21. import type { TriggerProps } from './trigger'
  22. import PresetsParameter from './presets-parameter'
  23. import cn from '@/utils/classnames'
  24. import {
  25. PortalToFollowElem,
  26. PortalToFollowElemContent,
  27. PortalToFollowElemTrigger,
  28. } from '@/app/components/base/portal-to-follow-elem'
  29. import { fetchModelParameterRules } from '@/service/common'
  30. import Loading from '@/app/components/base/loading'
  31. import { useProviderContext } from '@/context/provider-context'
  32. import { TONE_LIST } from '@/config'
  33. import { ArrowNarrowLeft } from '@/app/components/base/icons/src/vender/line/arrows'
  34. export type ModelParameterModalProps = {
  35. popupClassName?: string
  36. portalToFollowElemContentClassName?: string
  37. isAdvancedMode: boolean
  38. mode: string
  39. modelId: string
  40. provider: string
  41. setModel: (model: { modelId: string; provider: string; mode?: string; features?: string[] }) => void
  42. completionParams: FormValue
  43. onCompletionParamsChange: (newParams: FormValue) => void
  44. hideDebugWithMultipleModel?: boolean
  45. debugWithMultipleModel?: boolean
  46. onDebugWithMultipleModelChange?: () => void
  47. renderTrigger?: (v: TriggerProps) => ReactNode
  48. readonly?: boolean
  49. isInWorkflow?: boolean
  50. }
  51. const stopParameterRule: ModelParameterRule = {
  52. default: [],
  53. help: {
  54. en_US: 'Up to four sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.',
  55. zh_Hans: '最多四个序列,API 将停止生成更多的 token。返回的文本将不包含停止序列。',
  56. },
  57. label: {
  58. en_US: 'Stop sequences',
  59. zh_Hans: '停止序列',
  60. },
  61. name: 'stop',
  62. required: false,
  63. type: 'tag',
  64. tagPlaceholder: {
  65. en_US: 'Enter sequence and press Tab',
  66. zh_Hans: '输入序列并按 Tab 键',
  67. },
  68. }
  69. const PROVIDER_WITH_PRESET_TONE = ['openai', 'azure_openai']
  70. const ModelParameterModal: FC<ModelParameterModalProps> = ({
  71. popupClassName,
  72. portalToFollowElemContentClassName,
  73. isAdvancedMode,
  74. modelId,
  75. provider,
  76. setModel,
  77. completionParams,
  78. onCompletionParamsChange,
  79. hideDebugWithMultipleModel,
  80. debugWithMultipleModel,
  81. onDebugWithMultipleModelChange,
  82. renderTrigger,
  83. readonly,
  84. isInWorkflow,
  85. }) => {
  86. const { t } = useTranslation()
  87. const { isAPIKeySet } = useProviderContext()
  88. const [open, setOpen] = useState(false)
  89. const { data: parameterRulesData, isLoading } = useSWR((provider && modelId) ? `/workspaces/current/model-providers/${provider}/models/parameter-rules?model=${modelId}` : null, fetchModelParameterRules)
  90. const {
  91. currentProvider,
  92. currentModel,
  93. activeTextGenerationModelList,
  94. } = useTextGenerationCurrentProviderAndModelAndModelList(
  95. { provider, model: modelId },
  96. )
  97. const hasDeprecated = !currentProvider || !currentModel
  98. const modelDisabled = currentModel?.status !== ModelStatusEnum.active
  99. const disabled = !isAPIKeySet || hasDeprecated || modelDisabled
  100. const parameterRules: ModelParameterRule[] = useMemo(() => {
  101. return parameterRulesData?.data || []
  102. }, [parameterRulesData])
  103. const handleParamChange = (key: string, value: ParameterValue) => {
  104. onCompletionParamsChange({
  105. ...completionParams,
  106. [key]: value,
  107. })
  108. }
  109. const handleChangeModel = ({ provider, model }: DefaultModel) => {
  110. const targetProvider = activeTextGenerationModelList.find(modelItem => modelItem.provider === provider)
  111. const targetModelItem = targetProvider?.models.find(modelItem => modelItem.model === model)
  112. setModel({
  113. modelId: model,
  114. provider,
  115. mode: targetModelItem?.model_properties.mode as string,
  116. features: targetModelItem?.features || [],
  117. })
  118. }
  119. const handleSwitch = (key: string, value: boolean, assignValue: ParameterValue) => {
  120. if (!value) {
  121. const newCompletionParams = { ...completionParams }
  122. delete newCompletionParams[key]
  123. onCompletionParamsChange(newCompletionParams)
  124. }
  125. if (value) {
  126. onCompletionParamsChange({
  127. ...completionParams,
  128. [key]: assignValue,
  129. })
  130. }
  131. }
  132. const handleSelectPresetParameter = (toneId: number) => {
  133. const tone = TONE_LIST.find(tone => tone.id === toneId)
  134. if (tone) {
  135. onCompletionParamsChange({
  136. ...completionParams,
  137. ...tone.config,
  138. })
  139. }
  140. }
  141. return (
  142. <PortalToFollowElem
  143. open={open}
  144. onOpenChange={setOpen}
  145. placement={isInWorkflow ? 'left' : 'bottom-end'}
  146. offset={4}
  147. >
  148. <div className='relative'>
  149. <PortalToFollowElemTrigger
  150. onClick={() => {
  151. if (readonly)
  152. return
  153. setOpen(v => !v)
  154. }}
  155. className='block'
  156. >
  157. {
  158. renderTrigger
  159. ? renderTrigger({
  160. open,
  161. disabled,
  162. modelDisabled,
  163. hasDeprecated,
  164. currentProvider,
  165. currentModel,
  166. providerName: provider,
  167. modelId,
  168. })
  169. : (
  170. <Trigger
  171. disabled={disabled}
  172. isInWorkflow={isInWorkflow}
  173. modelDisabled={modelDisabled}
  174. hasDeprecated={hasDeprecated}
  175. currentProvider={currentProvider}
  176. currentModel={currentModel}
  177. providerName={provider}
  178. modelId={modelId}
  179. />
  180. )
  181. }
  182. </PortalToFollowElemTrigger>
  183. <PortalToFollowElemContent className={cn(portalToFollowElemContentClassName, 'z-[60]')}>
  184. <div className={cn(popupClassName, 'w-[496px] rounded-xl border border-gray-100 bg-white shadow-xl')}>
  185. <div className={cn(
  186. 'max-h-[480px] overflow-y-auto',
  187. !isInWorkflow && 'px-10 pt-6 pb-8',
  188. isInWorkflow && 'p-4')}>
  189. <div className='flex items-center justify-between h-8'>
  190. <div className={cn('font-semibold text-gray-900 shrink-0', isInWorkflow && 'text-[13px]')}>
  191. {t('common.modelProvider.model').toLocaleUpperCase()}
  192. </div>
  193. <ModelSelector
  194. defaultModel={(provider || modelId) ? { provider, model: modelId } : undefined}
  195. modelList={activeTextGenerationModelList}
  196. onSelect={handleChangeModel}
  197. triggerClassName='max-w-[295px]'
  198. />
  199. </div>
  200. {
  201. !!parameterRules.length && (
  202. <div className='my-5 h-[1px] bg-gray-100' />
  203. )
  204. }
  205. {
  206. isLoading && (
  207. <div className='mt-5'><Loading /></div>
  208. )
  209. }
  210. {
  211. !isLoading && !!parameterRules.length && (
  212. <div className='flex items-center justify-between mb-4'>
  213. <div className={cn('font-semibold text-gray-900', isInWorkflow && 'text-[13px]')}>{t('common.modelProvider.parameters')}</div>
  214. {
  215. PROVIDER_WITH_PRESET_TONE.includes(provider) && (
  216. <PresetsParameter onSelect={handleSelectPresetParameter} />
  217. )
  218. }
  219. </div>
  220. )
  221. }
  222. {
  223. !isLoading && !!parameterRules.length && (
  224. [
  225. ...parameterRules,
  226. ...(isAdvancedMode ? [stopParameterRule] : []),
  227. ].map(parameter => (
  228. <ParameterItem
  229. key={`${modelId}-${parameter.name}`}
  230. className='mb-4'
  231. parameterRule={parameter}
  232. value={completionParams?.[parameter.name]}
  233. onChange={v => handleParamChange(parameter.name, v)}
  234. onSwitch={(checked, assignValue) => handleSwitch(parameter.name, checked, assignValue)}
  235. isInWorkflow={isInWorkflow}
  236. />
  237. ))
  238. )
  239. }
  240. </div>
  241. {!hideDebugWithMultipleModel && (
  242. <div
  243. className='flex items-center justify-between px-6 h-[50px] bg-gray-50 border-t border-t-gray-100 text-xs font-medium text-primary-600 cursor-pointer rounded-b-xl'
  244. onClick={() => onDebugWithMultipleModelChange?.()}
  245. >
  246. {
  247. debugWithMultipleModel
  248. ? t('appDebug.debugAsSingleModel')
  249. : t('appDebug.debugAsMultipleModel')
  250. }
  251. <ArrowNarrowLeft className='w-3 h-3 rotate-180' />
  252. </div>
  253. )}
  254. </div>
  255. </PortalToFollowElemContent>
  256. </div>
  257. </PortalToFollowElem>
  258. )
  259. }
  260. export default ModelParameterModal