panel.tsx 10 KB

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