You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import type { FC } from 'react'
  2. import React, { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import MemoryConfig from '../_base/components/memory-config'
  5. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  6. import ConfigVision from '../_base/components/config-vision'
  7. import useConfig from './use-config'
  8. import type { LLMNodeType } from './types'
  9. import ConfigPrompt from './components/config-prompt'
  10. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  11. import AddButton2 from '@/app/components/base/button/add-button'
  12. import Field from '@/app/components/workflow/nodes/_base/components/field'
  13. import Split from '@/app/components/workflow/nodes/_base/components/split'
  14. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  15. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  16. import type { NodePanelProps } from '@/app/components/workflow/types'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  19. import StructureOutput from './components/structure-output'
  20. import Switch from '@/app/components/base/switch'
  21. import { RiAlertFill, RiQuestionLine } from '@remixicon/react'
  22. import { fetchAndMergeValidCompletionParams } from '@/utils/completion-params'
  23. import Toast from '@/app/components/base/toast'
  24. const i18nPrefix = 'workflow.nodes.llm'
  25. const Panel: FC<NodePanelProps<LLMNodeType>> = ({
  26. id,
  27. data,
  28. }) => {
  29. const { t } = useTranslation()
  30. const {
  31. readOnly,
  32. inputs,
  33. isChatModel,
  34. isChatMode,
  35. isCompletionModel,
  36. shouldShowContextTip,
  37. isVisionModel,
  38. handleModelChanged,
  39. hasSetBlockStatus,
  40. handleCompletionParamsChange,
  41. handleContextVarChange,
  42. filterInputVar,
  43. filterVar,
  44. availableVars,
  45. availableNodesWithParent,
  46. isShowVars,
  47. handlePromptChange,
  48. handleAddEmptyVariable,
  49. handleAddVariable,
  50. handleVarListChange,
  51. handleVarNameChange,
  52. handleSyeQueryChange,
  53. handleMemoryChange,
  54. handleVisionResolutionEnabledChange,
  55. handleVisionResolutionChange,
  56. isModelSupportStructuredOutput,
  57. structuredOutputCollapsed,
  58. setStructuredOutputCollapsed,
  59. handleStructureOutputEnableChange,
  60. handleStructureOutputChange,
  61. filterJinjia2InputVar,
  62. } = useConfig(id, data)
  63. const model = inputs.model
  64. const handleModelChange = useCallback((model: {
  65. provider: string
  66. modelId: string
  67. mode?: string
  68. }) => {
  69. (async () => {
  70. try {
  71. const { params: filtered, removedDetails } = await fetchAndMergeValidCompletionParams(
  72. model.provider,
  73. model.modelId,
  74. inputs.model.completion_params,
  75. )
  76. const keys = Object.keys(removedDetails)
  77. if (keys.length)
  78. Toast.notify({ type: 'warning', message: `${t('common.modelProvider.parametersInvalidRemoved')}: ${keys.map(k => `${k} (${removedDetails[k]})`).join(', ')}` })
  79. handleCompletionParamsChange(filtered)
  80. }
  81. catch (e) {
  82. Toast.notify({ type: 'error', message: t('common.error') })
  83. handleCompletionParamsChange({})
  84. }
  85. finally {
  86. handleModelChanged(model)
  87. }
  88. })()
  89. }, [inputs.model.completion_params])
  90. return (
  91. <div className='mt-2'>
  92. <div className='space-y-4 px-4 pb-4'>
  93. <Field
  94. title={t(`${i18nPrefix}.model`)}
  95. required
  96. >
  97. <ModelParameterModal
  98. popupClassName='!w-[387px]'
  99. isInWorkflow
  100. isAdvancedMode={true}
  101. mode={model?.mode}
  102. provider={model?.provider}
  103. completionParams={model?.completion_params}
  104. modelId={model?.name}
  105. setModel={handleModelChange}
  106. onCompletionParamsChange={handleCompletionParamsChange}
  107. hideDebugWithMultipleModel
  108. debugWithMultipleModel={false}
  109. readonly={readOnly}
  110. />
  111. </Field>
  112. {/* knowledge */}
  113. <Field
  114. title={t(`${i18nPrefix}.context`)}
  115. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  116. >
  117. <>
  118. <VarReferencePicker
  119. readonly={readOnly}
  120. nodeId={id}
  121. isShowNodeName
  122. value={inputs.context?.variable_selector || []}
  123. onChange={handleContextVarChange}
  124. filterVar={filterVar}
  125. />
  126. {shouldShowContextTip && (
  127. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  128. )}
  129. </>
  130. </Field>
  131. {/* Prompt */}
  132. {model.name && (
  133. <ConfigPrompt
  134. readOnly={readOnly}
  135. nodeId={id}
  136. filterVar={filterInputVar}
  137. isChatModel={isChatModel}
  138. isChatApp={isChatMode}
  139. isShowContext
  140. payload={inputs.prompt_template}
  141. onChange={handlePromptChange}
  142. hasSetBlockStatus={hasSetBlockStatus}
  143. varList={inputs.prompt_config?.jinja2_variables || []}
  144. handleAddVariable={handleAddVariable}
  145. modelConfig={model}
  146. />
  147. )}
  148. {isShowVars && (
  149. <Field
  150. title={t('workflow.nodes.templateTransform.inputVars')}
  151. operations={
  152. !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined
  153. }
  154. >
  155. <VarList
  156. nodeId={id}
  157. readonly={readOnly}
  158. list={inputs.prompt_config?.jinja2_variables || []}
  159. onChange={handleVarListChange}
  160. onVarNameChange={handleVarNameChange}
  161. filterVar={filterJinjia2InputVar}
  162. isSupportFileVar={false}
  163. />
  164. </Field>
  165. )}
  166. {/* Memory put place examples. */}
  167. {isChatMode && isChatModel && !!inputs.memory && (
  168. <div className='mt-4'>
  169. <div className='flex h-8 items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2'>
  170. <div className='flex items-center space-x-1'>
  171. <div className='text-xs font-semibold uppercase text-text-secondary'>{t('workflow.nodes.common.memories.title')}</div>
  172. <Tooltip
  173. popupContent={t('workflow.nodes.common.memories.tip')}
  174. triggerClassName='w-4 h-4'
  175. />
  176. </div>
  177. <div className='flex h-[18px] items-center rounded-[5px] border border-divider-deep bg-components-badge-bg-dimm px-1 text-xs font-semibold uppercase text-text-tertiary'>{t('workflow.nodes.common.memories.builtIn')}</div>
  178. </div>
  179. {/* Readonly User Query */}
  180. <div className='mt-4'>
  181. <Editor
  182. title={<div className='flex items-center space-x-1'>
  183. <div className='text-xs font-semibold uppercase text-text-secondary'>user</div>
  184. <Tooltip
  185. popupContent={
  186. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  187. }
  188. triggerClassName='w-4 h-4'
  189. />
  190. </div>}
  191. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  192. onChange={handleSyeQueryChange}
  193. readOnly={readOnly}
  194. isShowContext={false}
  195. isChatApp
  196. isChatModel
  197. hasSetBlockStatus={hasSetBlockStatus}
  198. nodesOutputVars={availableVars}
  199. availableNodes={availableNodesWithParent}
  200. isSupportFileVar
  201. />
  202. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  203. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  204. )}
  205. </div>
  206. </div>
  207. )}
  208. {/* Memory */}
  209. {isChatMode && (
  210. <>
  211. <Split />
  212. <MemoryConfig
  213. readonly={readOnly}
  214. config={{ data: inputs.memory }}
  215. onChange={handleMemoryChange}
  216. canSetRoleName={isCompletionModel}
  217. />
  218. </>
  219. )}
  220. {/* Vision: GPT4-vision and so on */}
  221. <ConfigVision
  222. nodeId={id}
  223. readOnly={readOnly}
  224. isVisionModel={isVisionModel}
  225. enabled={inputs.vision?.enabled}
  226. onEnabledChange={handleVisionResolutionEnabledChange}
  227. config={inputs.vision?.configs}
  228. onConfigChange={handleVisionResolutionChange}
  229. />
  230. </div>
  231. <Split />
  232. <OutputVars
  233. collapsed={structuredOutputCollapsed}
  234. onCollapse={setStructuredOutputCollapsed}
  235. operations={
  236. <div className='mr-4 flex shrink-0 items-center'>
  237. {(!isModelSupportStructuredOutput && !!inputs.structured_output_enabled) && (
  238. <Tooltip noDecoration popupContent={
  239. <div className='w-[232px] rounded-xl border-[0.5px] border-components-panel-border bg-components-tooltip-bg px-4 py-3.5 shadow-lg backdrop-blur-[5px]'>
  240. <div className='title-xs-semi-bold text-text-primary'>{t('app.structOutput.modelNotSupported')}</div>
  241. <div className='body-xs-regular mt-1 text-text-secondary'>{t('app.structOutput.modelNotSupportedTip')}</div>
  242. </div>
  243. }>
  244. <div>
  245. <RiAlertFill className='mr-1 size-4 text-text-warning-secondary' />
  246. </div>
  247. </Tooltip>
  248. )}
  249. <div className='system-xs-medium-uppercase mr-0.5 text-text-tertiary'>{t('app.structOutput.structured')}</div>
  250. <Tooltip popupContent={
  251. <div className='max-w-[150px]'>{t('app.structOutput.structuredTip')}</div>
  252. }>
  253. <div>
  254. <RiQuestionLine className='size-3.5 text-text-quaternary' />
  255. </div>
  256. </Tooltip>
  257. <Switch
  258. className='ml-2'
  259. defaultValue={!!inputs.structured_output_enabled}
  260. onChange={handleStructureOutputEnableChange}
  261. size='md'
  262. disabled={readOnly}
  263. />
  264. </div>
  265. }
  266. >
  267. <>
  268. <VarItem
  269. name='text'
  270. type='string'
  271. description={t(`${i18nPrefix}.outputVars.output`)}
  272. />
  273. {inputs.structured_output_enabled && (
  274. <>
  275. <Split className='mt-3' />
  276. <StructureOutput
  277. className='mt-4'
  278. value={inputs.structured_output}
  279. onChange={handleStructureOutputChange}
  280. />
  281. </>
  282. )}
  283. </>
  284. </OutputVars>
  285. </div>
  286. )
  287. }
  288. export default React.memo(Panel)