panel.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiQuestionLine } from '@remixicon/react'
  5. import MemoryConfig from '../_base/components/memory-config'
  6. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  7. import useConfig from './use-config'
  8. import ResolutionPicker from './components/resolution-picker'
  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 { Resolution } from '@/types/app'
  18. import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
  19. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  20. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  21. import ResultPanel from '@/app/components/workflow/run/result-panel'
  22. import TooltipPlus from '@/app/components/base/tooltip-plus'
  23. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  24. import Switch from '@/app/components/base/switch'
  25. const i18nPrefix = 'workflow.nodes.llm'
  26. const Panel: FC<NodePanelProps<LLMNodeType>> = ({
  27. id,
  28. data,
  29. }) => {
  30. const { t } = useTranslation()
  31. const {
  32. readOnly,
  33. inputs,
  34. isChatModel,
  35. isChatMode,
  36. isCompletionModel,
  37. shouldShowContextTip,
  38. isShowVisionConfig,
  39. handleModelChanged,
  40. hasSetBlockStatus,
  41. handleCompletionParamsChange,
  42. handleContextVarChange,
  43. filterInputVar,
  44. filterVar,
  45. availableVars,
  46. availableNodesWithParent,
  47. isShowVars,
  48. handlePromptChange,
  49. handleAddEmptyVariable,
  50. handleAddVariable,
  51. handleVarListChange,
  52. handleVarNameChange,
  53. handleSyeQueryChange,
  54. handleMemoryChange,
  55. handleVisionResolutionEnabledChange,
  56. handleVisionResolutionChange,
  57. isShowSingleRun,
  58. hideSingleRun,
  59. inputVarValues,
  60. setInputVarValues,
  61. visionFiles,
  62. setVisionFiles,
  63. contexts,
  64. setContexts,
  65. runningStatus,
  66. handleRun,
  67. handleStop,
  68. varInputs,
  69. runResult,
  70. } = useConfig(id, data)
  71. const model = inputs.model
  72. const singleRunForms = (() => {
  73. const forms: FormProps[] = []
  74. if (varInputs.length > 0) {
  75. forms.push(
  76. {
  77. label: t(`${i18nPrefix}.singleRun.variable`)!,
  78. inputs: varInputs,
  79. values: inputVarValues,
  80. onChange: setInputVarValues,
  81. },
  82. )
  83. }
  84. if (inputs.context?.variable_selector && inputs.context?.variable_selector.length > 0) {
  85. forms.push(
  86. {
  87. label: t(`${i18nPrefix}.context`)!,
  88. inputs: [{
  89. label: '',
  90. variable: '#context#',
  91. type: InputVarType.contexts,
  92. required: false,
  93. }],
  94. values: { '#context#': contexts },
  95. onChange: keyValue => setContexts((keyValue as any)['#context#']),
  96. },
  97. )
  98. }
  99. if (isShowVisionConfig) {
  100. forms.push(
  101. {
  102. label: t(`${i18nPrefix}.vision`)!,
  103. inputs: [{
  104. label: t(`${i18nPrefix}.files`)!,
  105. variable: '#files#',
  106. type: InputVarType.files,
  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={filterVar}
  187. />
  188. </Field>
  189. )}
  190. {/* Memory put place examples. */}
  191. {isChatMode && isChatModel && !!inputs.memory && (
  192. <div className='mt-4'>
  193. <div className='flex justify-between items-center h-8 pl-3 pr-2 rounded-lg bg-gray-100'>
  194. <div className='flex items-center space-x-1'>
  195. <div className='text-xs font-semibold text-gray-700 uppercase'>{t('workflow.nodes.common.memories.title')}</div>
  196. <TooltipPlus
  197. popupContent={t('workflow.nodes.common.memories.tip')}
  198. >
  199. <RiQuestionLine className='w-3.5 h-3.5 text-gray-400' />
  200. </TooltipPlus>
  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. <TooltipPlus
  210. popupContent={
  211. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  212. }
  213. >
  214. <RiQuestionLine className='w-3.5 h-3.5 text-gray-400' />
  215. </TooltipPlus>
  216. </div>}
  217. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  218. onChange={handleSyeQueryChange}
  219. readOnly={readOnly}
  220. isShowContext={false}
  221. isChatApp
  222. isChatModel
  223. hasSetBlockStatus={hasSetBlockStatus}
  224. nodesOutputVars={availableVars}
  225. availableNodes={availableNodesWithParent}
  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. {isShowVisionConfig && (
  247. <>
  248. <Split />
  249. <Field
  250. title={t(`${i18nPrefix}.vision`)}
  251. tooltip={t('appDebug.vision.description')!}
  252. operations={
  253. <Switch size='md' defaultValue={inputs.vision.enabled} onChange={handleVisionResolutionEnabledChange} />
  254. }
  255. >
  256. {inputs.vision.enabled
  257. ? (
  258. <ResolutionPicker
  259. value={inputs.vision.configs?.detail || Resolution.high}
  260. onChange={handleVisionResolutionChange}
  261. />
  262. )
  263. : null}
  264. </Field>
  265. </>
  266. )}
  267. </div>
  268. <Split />
  269. <div className='px-4 pt-4 pb-2'>
  270. <OutputVars>
  271. <>
  272. <VarItem
  273. name='text'
  274. type='string'
  275. description={t(`${i18nPrefix}.outputVars.output`)}
  276. />
  277. </>
  278. </OutputVars>
  279. </div>
  280. {isShowSingleRun && (
  281. <BeforeRunForm
  282. nodeName={inputs.title}
  283. onHide={hideSingleRun}
  284. forms={singleRunForms}
  285. runningStatus={runningStatus}
  286. onRun={handleRun}
  287. onStop={handleStop}
  288. result={<ResultPanel {...runResult} showSteps={false} />}
  289. />
  290. )}
  291. </div>
  292. )
  293. }
  294. export default React.memo(Panel)