panel.tsx 10.0 KB

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