panel.tsx 10 KB

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