panel.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import type { FC } from 'react'
  2. import React 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. return (
  117. <div className='mt-2'>
  118. <div className='px-4 pb-4 space-y-4'>
  119. <Field
  120. title={t(`${i18nPrefix}.model`)}
  121. >
  122. <ModelParameterModal
  123. popupClassName='!w-[387px]'
  124. isInWorkflow
  125. isAdvancedMode={true}
  126. mode={model?.mode}
  127. provider={model?.provider}
  128. completionParams={model?.completion_params}
  129. modelId={model?.name}
  130. setModel={handleModelChanged}
  131. onCompletionParamsChange={handleCompletionParamsChange}
  132. hideDebugWithMultipleModel
  133. debugWithMultipleModel={false}
  134. readonly={readOnly}
  135. />
  136. </Field>
  137. {/* knowledge */}
  138. <Field
  139. title={t(`${i18nPrefix}.context`)}
  140. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  141. >
  142. <>
  143. <VarReferencePicker
  144. readonly={readOnly}
  145. nodeId={id}
  146. isShowNodeName
  147. value={inputs.context?.variable_selector || []}
  148. onChange={handleContextVarChange}
  149. filterVar={filterVar}
  150. />
  151. {shouldShowContextTip && (
  152. <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  153. )}
  154. </>
  155. </Field>
  156. {/* Prompt */}
  157. {model.name && (
  158. <ConfigPrompt
  159. readOnly={readOnly}
  160. nodeId={id}
  161. filterVar={filterInputVar}
  162. isChatModel={isChatModel}
  163. isChatApp={isChatMode}
  164. isShowContext
  165. payload={inputs.prompt_template}
  166. onChange={handlePromptChange}
  167. hasSetBlockStatus={hasSetBlockStatus}
  168. varList={inputs.prompt_config?.jinja2_variables || []}
  169. handleAddVariable={handleAddVariable}
  170. modelConfig={model}
  171. />
  172. )}
  173. {isShowVars && (
  174. <Field
  175. title={t('workflow.nodes.templateTransform.inputVars')}
  176. operations={
  177. !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined
  178. }
  179. >
  180. <VarList
  181. nodeId={id}
  182. readonly={readOnly}
  183. list={inputs.prompt_config?.jinja2_variables || []}
  184. onChange={handleVarListChange}
  185. onVarNameChange={handleVarNameChange}
  186. filterVar={filterJinjia2InputVar}
  187. isSupportFileVar={false}
  188. />
  189. </Field>
  190. )}
  191. {/* Memory put place examples. */}
  192. {isChatMode && isChatModel && !!inputs.memory && (
  193. <div className='mt-4'>
  194. <div className='flex justify-between items-center h-8 pl-3 pr-2 rounded-lg bg-gray-100'>
  195. <div className='flex items-center space-x-1'>
  196. <div className='text-xs font-semibold text-gray-700 uppercase'>{t('workflow.nodes.common.memories.title')}</div>
  197. <Tooltip
  198. popupContent={t('workflow.nodes.common.memories.tip')}
  199. triggerClassName='w-4 h-4'
  200. />
  201. </div>
  202. <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>
  203. </div>
  204. {/* Readonly User Query */}
  205. <div className='mt-4'>
  206. <Editor
  207. title={<div className='flex items-center space-x-1'>
  208. <div className='text-xs font-semibold text-gray-700 uppercase'>user</div>
  209. <Tooltip
  210. popupContent={
  211. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  212. }
  213. triggerClassName='w-4 h-4'
  214. />
  215. </div>}
  216. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  217. onChange={handleSyeQueryChange}
  218. readOnly={readOnly}
  219. isShowContext={false}
  220. isChatApp
  221. isChatModel
  222. hasSetBlockStatus={hasSetBlockStatus}
  223. nodesOutputVars={availableVars}
  224. availableNodes={availableNodesWithParent}
  225. isSupportFileVar
  226. />
  227. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  228. <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  229. )}
  230. </div>
  231. </div>
  232. )}
  233. {/* Memory */}
  234. {isChatMode && (
  235. <>
  236. <Split />
  237. <MemoryConfig
  238. readonly={readOnly}
  239. config={{ data: inputs.memory }}
  240. onChange={handleMemoryChange}
  241. canSetRoleName={isCompletionModel}
  242. />
  243. </>
  244. )}
  245. {/* Vision: GPT4-vision and so on */}
  246. <ConfigVision
  247. nodeId={id}
  248. readOnly={readOnly}
  249. isVisionModel={isVisionModel}
  250. enabled={inputs.vision?.enabled}
  251. onEnabledChange={handleVisionResolutionEnabledChange}
  252. config={inputs.vision?.configs}
  253. onConfigChange={handleVisionResolutionChange}
  254. />
  255. </div>
  256. <Split />
  257. <OutputVars>
  258. <>
  259. <VarItem
  260. name='text'
  261. type='string'
  262. description={t(`${i18nPrefix}.outputVars.output`)}
  263. />
  264. </>
  265. </OutputVars>
  266. {isShowSingleRun && (
  267. <BeforeRunForm
  268. nodeName={inputs.title}
  269. nodeType={inputs.type}
  270. onHide={hideSingleRun}
  271. forms={singleRunForms}
  272. runningStatus={runningStatus}
  273. onRun={handleRun}
  274. onStop={handleStop}
  275. result={<ResultPanel {...runResult} showSteps={false} />}
  276. />
  277. )}
  278. </div>
  279. )
  280. }
  281. export default React.memo(Panel)