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.

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