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.

panel.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 { findVariableWhenOnLLMVision } from '../utils'
  9. import type { LLMNodeType } from './types'
  10. import ConfigPrompt from './components/config-prompt'
  11. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  12. import AddButton2 from '@/app/components/base/button/add-button'
  13. import Field from '@/app/components/workflow/nodes/_base/components/field'
  14. import Split from '@/app/components/workflow/nodes/_base/components/split'
  15. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  16. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  17. import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
  18. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  19. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  20. import ResultPanel from '@/app/components/workflow/run/result-panel'
  21. import Tooltip from '@/app/components/base/tooltip'
  22. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  23. import StructureOutput from './components/structure-output'
  24. import Switch from '@/app/components/base/switch'
  25. import { RiAlertFill, RiQuestionLine } from '@remixicon/react'
  26. const i18nPrefix = 'workflow.nodes.llm'
  27. const Panel: FC<NodePanelProps<LLMNodeType>> = ({
  28. id,
  29. data,
  30. }) => {
  31. const { t } = useTranslation()
  32. const {
  33. readOnly,
  34. inputs,
  35. isChatModel,
  36. isChatMode,
  37. isCompletionModel,
  38. shouldShowContextTip,
  39. isVisionModel,
  40. handleModelChanged,
  41. hasSetBlockStatus,
  42. handleCompletionParamsChange,
  43. handleContextVarChange,
  44. filterInputVar,
  45. filterVar,
  46. availableVars,
  47. availableNodesWithParent,
  48. isShowVars,
  49. handlePromptChange,
  50. handleAddEmptyVariable,
  51. handleAddVariable,
  52. handleVarListChange,
  53. handleVarNameChange,
  54. handleSyeQueryChange,
  55. handleMemoryChange,
  56. handleVisionResolutionEnabledChange,
  57. handleVisionResolutionChange,
  58. isShowSingleRun,
  59. hideSingleRun,
  60. inputVarValues,
  61. setInputVarValues,
  62. visionFiles,
  63. setVisionFiles,
  64. contexts,
  65. setContexts,
  66. runningStatus,
  67. isModelSupportStructuredOutput,
  68. structuredOutputCollapsed,
  69. setStructuredOutputCollapsed,
  70. handleStructureOutputEnableChange,
  71. handleStructureOutputChange,
  72. handleRun,
  73. handleStop,
  74. varInputs,
  75. runResult,
  76. filterJinjia2InputVar,
  77. } = useConfig(id, data)
  78. const model = inputs.model
  79. const singleRunForms = (() => {
  80. const forms: FormProps[] = []
  81. if (varInputs.length > 0) {
  82. forms.push(
  83. {
  84. label: t(`${i18nPrefix}.singleRun.variable`)!,
  85. inputs: varInputs,
  86. values: inputVarValues,
  87. onChange: setInputVarValues,
  88. },
  89. )
  90. }
  91. if (inputs.context?.variable_selector && inputs.context?.variable_selector.length > 0) {
  92. forms.push(
  93. {
  94. label: t(`${i18nPrefix}.context`)!,
  95. inputs: [{
  96. label: '',
  97. variable: '#context#',
  98. type: InputVarType.contexts,
  99. required: false,
  100. }],
  101. values: { '#context#': contexts },
  102. onChange: keyValue => setContexts(keyValue['#context#']),
  103. },
  104. )
  105. }
  106. if (isVisionModel && data.vision?.enabled && data.vision?.configs?.variable_selector) {
  107. const currentVariable = findVariableWhenOnLLMVision(data.vision.configs.variable_selector, availableVars)
  108. forms.push(
  109. {
  110. label: t(`${i18nPrefix}.vision`)!,
  111. inputs: [{
  112. label: currentVariable?.variable as any,
  113. variable: '#files#',
  114. type: currentVariable?.formType as any,
  115. required: false,
  116. }],
  117. values: { '#files#': visionFiles },
  118. onChange: keyValue => setVisionFiles((keyValue as any)['#files#']),
  119. },
  120. )
  121. }
  122. return forms
  123. })()
  124. const handleModelChange = useCallback((model: {
  125. provider: string
  126. modelId: string
  127. mode?: string
  128. }) => {
  129. handleCompletionParamsChange({})
  130. handleModelChanged(model)
  131. // eslint-disable-next-line react-hooks/exhaustive-deps
  132. }, [])
  133. return (
  134. <div className='mt-2'>
  135. <div className='space-y-4 px-4 pb-4'>
  136. <Field
  137. title={t(`${i18nPrefix}.model`)}
  138. required
  139. >
  140. <ModelParameterModal
  141. popupClassName='!w-[387px]'
  142. isInWorkflow
  143. isAdvancedMode={true}
  144. mode={model?.mode}
  145. provider={model?.provider}
  146. completionParams={model?.completion_params}
  147. modelId={model?.name}
  148. setModel={handleModelChange}
  149. onCompletionParamsChange={handleCompletionParamsChange}
  150. hideDebugWithMultipleModel
  151. debugWithMultipleModel={false}
  152. readonly={readOnly}
  153. />
  154. </Field>
  155. {/* knowledge */}
  156. <Field
  157. title={t(`${i18nPrefix}.context`)}
  158. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  159. >
  160. <>
  161. <VarReferencePicker
  162. readonly={readOnly}
  163. nodeId={id}
  164. isShowNodeName
  165. value={inputs.context?.variable_selector || []}
  166. onChange={handleContextVarChange}
  167. filterVar={filterVar}
  168. />
  169. {shouldShowContextTip && (
  170. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  171. )}
  172. </>
  173. </Field>
  174. {/* Prompt */}
  175. {model.name && (
  176. <ConfigPrompt
  177. readOnly={readOnly}
  178. nodeId={id}
  179. filterVar={filterInputVar}
  180. isChatModel={isChatModel}
  181. isChatApp={isChatMode}
  182. isShowContext
  183. payload={inputs.prompt_template}
  184. onChange={handlePromptChange}
  185. hasSetBlockStatus={hasSetBlockStatus}
  186. varList={inputs.prompt_config?.jinja2_variables || []}
  187. handleAddVariable={handleAddVariable}
  188. modelConfig={model}
  189. />
  190. )}
  191. {isShowVars && (
  192. <Field
  193. title={t('workflow.nodes.templateTransform.inputVars')}
  194. operations={
  195. !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined
  196. }
  197. >
  198. <VarList
  199. nodeId={id}
  200. readonly={readOnly}
  201. list={inputs.prompt_config?.jinja2_variables || []}
  202. onChange={handleVarListChange}
  203. onVarNameChange={handleVarNameChange}
  204. filterVar={filterJinjia2InputVar}
  205. isSupportFileVar={false}
  206. />
  207. </Field>
  208. )}
  209. {/* Memory put place examples. */}
  210. {isChatMode && isChatModel && !!inputs.memory && (
  211. <div className='mt-4'>
  212. <div className='flex h-8 items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2'>
  213. <div className='flex items-center space-x-1'>
  214. <div className='text-xs font-semibold uppercase text-text-secondary'>{t('workflow.nodes.common.memories.title')}</div>
  215. <Tooltip
  216. popupContent={t('workflow.nodes.common.memories.tip')}
  217. triggerClassName='w-4 h-4'
  218. />
  219. </div>
  220. <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>
  221. </div>
  222. {/* Readonly User Query */}
  223. <div className='mt-4'>
  224. <Editor
  225. title={<div className='flex items-center space-x-1'>
  226. <div className='text-xs font-semibold uppercase text-text-secondary'>user</div>
  227. <Tooltip
  228. popupContent={
  229. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  230. }
  231. triggerClassName='w-4 h-4'
  232. />
  233. </div>}
  234. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  235. onChange={handleSyeQueryChange}
  236. readOnly={readOnly}
  237. isShowContext={false}
  238. isChatApp
  239. isChatModel
  240. hasSetBlockStatus={hasSetBlockStatus}
  241. nodesOutputVars={availableVars}
  242. availableNodes={availableNodesWithParent}
  243. isSupportFileVar
  244. />
  245. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  246. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  247. )}
  248. </div>
  249. </div>
  250. )}
  251. {/* Memory */}
  252. {isChatMode && (
  253. <>
  254. <Split />
  255. <MemoryConfig
  256. readonly={readOnly}
  257. config={{ data: inputs.memory }}
  258. onChange={handleMemoryChange}
  259. canSetRoleName={isCompletionModel}
  260. />
  261. </>
  262. )}
  263. {/* Vision: GPT4-vision and so on */}
  264. <ConfigVision
  265. nodeId={id}
  266. readOnly={readOnly}
  267. isVisionModel={isVisionModel}
  268. enabled={inputs.vision?.enabled}
  269. onEnabledChange={handleVisionResolutionEnabledChange}
  270. config={inputs.vision?.configs}
  271. onConfigChange={handleVisionResolutionChange}
  272. />
  273. </div>
  274. <Split />
  275. <OutputVars
  276. collapsed={structuredOutputCollapsed}
  277. onCollapse={setStructuredOutputCollapsed}
  278. operations={
  279. <div className='mr-4 flex shrink-0 items-center'>
  280. {(!isModelSupportStructuredOutput && !!inputs.structured_output_enabled) && (
  281. <Tooltip noDecoration popupContent={
  282. <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]'>
  283. <div className='title-xs-semi-bold text-text-primary'>{t('app.structOutput.modelNotSupported')}</div>
  284. <div className='body-xs-regular mt-1 text-text-secondary'>{t('app.structOutput.modelNotSupportedTip')}</div>
  285. </div>
  286. }>
  287. <div>
  288. <RiAlertFill className='mr-1 size-4 text-text-warning-secondary' />
  289. </div>
  290. </Tooltip>
  291. )}
  292. <div className='system-xs-medium-uppercase mr-0.5 text-text-tertiary'>{t('app.structOutput.structured')}</div>
  293. <Tooltip popupContent={
  294. <div className='max-w-[150px]'>{t('app.structOutput.structuredTip')}</div>
  295. }>
  296. <div>
  297. <RiQuestionLine className='size-3.5 text-text-quaternary' />
  298. </div>
  299. </Tooltip>
  300. <Switch
  301. className='ml-2'
  302. defaultValue={!!inputs.structured_output_enabled}
  303. onChange={handleStructureOutputEnableChange}
  304. size='md'
  305. disabled={readOnly}
  306. />
  307. </div>
  308. }
  309. >
  310. <>
  311. <VarItem
  312. name='text'
  313. type='string'
  314. description={t(`${i18nPrefix}.outputVars.output`)}
  315. />
  316. {inputs.structured_output_enabled && (
  317. <>
  318. <Split className='mt-3' />
  319. <StructureOutput
  320. className='mt-4'
  321. value={inputs.structured_output}
  322. onChange={handleStructureOutputChange}
  323. />
  324. </>
  325. )}
  326. </>
  327. </OutputVars>
  328. {isShowSingleRun && (
  329. <BeforeRunForm
  330. nodeName={inputs.title}
  331. nodeType={inputs.type}
  332. onHide={hideSingleRun}
  333. forms={singleRunForms}
  334. runningStatus={runningStatus}
  335. onRun={handleRun}
  336. onStop={handleStop}
  337. result={<ResultPanel {...runResult} showSteps={false} />}
  338. />
  339. )}
  340. </div>
  341. )
  342. }
  343. export default React.memo(Panel)