panel.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 Field from '@/app/components/workflow/nodes/_base/components/field'
  11. import Split from '@/app/components/workflow/nodes/_base/components/split'
  12. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  13. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  14. import { Resolution } from '@/types/app'
  15. import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
  16. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  17. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  18. import ResultPanel from '@/app/components/workflow/run/result-panel'
  19. import TooltipPlus from '@/app/components/base/tooltip-plus'
  20. import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
  21. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  22. import Switch from '@/app/components/base/switch'
  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. isShowVisionConfig,
  37. handleModelChanged,
  38. hasSetBlockStatus,
  39. handleCompletionParamsChange,
  40. handleContextVarChange,
  41. filterInputVar,
  42. filterVar,
  43. availableVars,
  44. availableNodes,
  45. handlePromptChange,
  46. handleSyeQueryChange,
  47. handleMemoryChange,
  48. handleVisionResolutionEnabledChange,
  49. handleVisionResolutionChange,
  50. isShowSingleRun,
  51. hideSingleRun,
  52. inputVarValues,
  53. setInputVarValues,
  54. visionFiles,
  55. setVisionFiles,
  56. contexts,
  57. setContexts,
  58. runningStatus,
  59. handleRun,
  60. handleStop,
  61. varInputs,
  62. runResult,
  63. } = useConfig(id, data)
  64. const model = inputs.model
  65. const singleRunForms = (() => {
  66. const forms: FormProps[] = []
  67. if (varInputs.length > 0) {
  68. forms.push(
  69. {
  70. label: t(`${i18nPrefix}.singleRun.variable`)!,
  71. inputs: varInputs,
  72. values: inputVarValues,
  73. onChange: setInputVarValues,
  74. },
  75. )
  76. }
  77. if (inputs.context?.variable_selector && inputs.context?.variable_selector.length > 0) {
  78. forms.push(
  79. {
  80. label: t(`${i18nPrefix}.context`)!,
  81. inputs: [{
  82. label: '',
  83. variable: '#context#',
  84. type: InputVarType.contexts,
  85. required: false,
  86. }],
  87. values: { '#context#': contexts },
  88. onChange: keyValue => setContexts((keyValue as any)['#context#']),
  89. },
  90. )
  91. }
  92. if (isShowVisionConfig) {
  93. forms.push(
  94. {
  95. label: t(`${i18nPrefix}.vision`)!,
  96. inputs: [{
  97. label: t(`${i18nPrefix}.files`)!,
  98. variable: '#files#',
  99. type: InputVarType.files,
  100. required: false,
  101. }],
  102. values: { '#files#': visionFiles },
  103. onChange: keyValue => setVisionFiles((keyValue as any)['#files#']),
  104. },
  105. )
  106. }
  107. return forms
  108. })()
  109. return (
  110. <div className='mt-2'>
  111. <div className='px-4 pb-4 space-y-4'>
  112. <Field
  113. title={t(`${i18nPrefix}.model`)}
  114. >
  115. <ModelParameterModal
  116. popupClassName='!w-[387px]'
  117. isInWorkflow
  118. isAdvancedMode={true}
  119. mode={model?.mode}
  120. provider={model?.provider}
  121. completionParams={model?.completion_params}
  122. modelId={model?.name}
  123. setModel={handleModelChanged}
  124. onCompletionParamsChange={handleCompletionParamsChange}
  125. hideDebugWithMultipleModel
  126. debugWithMultipleModel={false}
  127. readonly={readOnly}
  128. />
  129. </Field>
  130. {/* knowledge */}
  131. <Field
  132. title={t(`${i18nPrefix}.context`)}
  133. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  134. >
  135. <>
  136. <VarReferencePicker
  137. readonly={readOnly}
  138. nodeId={id}
  139. isShowNodeName
  140. value={inputs.context?.variable_selector || []}
  141. onChange={handleContextVarChange}
  142. filterVar={filterVar}
  143. />
  144. {shouldShowContextTip && (
  145. <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  146. )}
  147. </>
  148. </Field>
  149. {/* Prompt */}
  150. {model.name && (
  151. <ConfigPrompt
  152. readOnly={readOnly}
  153. nodeId={id}
  154. filterVar={filterInputVar}
  155. isChatModel={isChatModel}
  156. isChatApp={isChatMode}
  157. isShowContext
  158. payload={inputs.prompt_template}
  159. onChange={handlePromptChange}
  160. hasSetBlockStatus={hasSetBlockStatus}
  161. />
  162. )}
  163. {/* Memory put place examples. */}
  164. {isChatMode && isChatModel && !!inputs.memory && (
  165. <div className='mt-4'>
  166. <div className='flex justify-between items-center h-8 pl-3 pr-2 rounded-lg bg-gray-100'>
  167. <div className='flex items-center space-x-1'>
  168. <div className='text-xs font-semibold text-gray-700 uppercase'>{t('workflow.nodes.common.memories.title')}</div>
  169. <TooltipPlus
  170. popupContent={t('workflow.nodes.common.memories.tip')}
  171. >
  172. <HelpCircle className='w-3.5 h-3.5 text-gray-400' />
  173. </TooltipPlus>
  174. </div>
  175. <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>
  176. </div>
  177. {/* Readonly User Query */}
  178. <div className='mt-4'>
  179. <Editor
  180. title={<div className='flex items-center space-x-1'>
  181. <div className='text-xs font-semibold text-gray-700 uppercase'>user</div>
  182. <TooltipPlus
  183. popupContent={
  184. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  185. }
  186. >
  187. <HelpCircle className='w-3.5 h-3.5 text-gray-400' />
  188. </TooltipPlus>
  189. </div>}
  190. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  191. onChange={handleSyeQueryChange}
  192. readOnly={readOnly}
  193. isShowContext={false}
  194. isChatApp
  195. isChatModel
  196. hasSetBlockStatus={hasSetBlockStatus}
  197. nodesOutputVars={availableVars}
  198. availableNodes={availableNodes}
  199. />
  200. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  201. <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  202. )}
  203. </div>
  204. </div>
  205. )}
  206. {/* Memory */}
  207. {isChatMode && (
  208. <>
  209. <Split />
  210. <MemoryConfig
  211. readonly={readOnly}
  212. config={{ data: inputs.memory }}
  213. onChange={handleMemoryChange}
  214. canSetRoleName={isCompletionModel}
  215. />
  216. </>
  217. )}
  218. {/* Vision: GPT4-vision and so on */}
  219. {isShowVisionConfig && (
  220. <>
  221. <Split />
  222. <Field
  223. title={t(`${i18nPrefix}.vision`)}
  224. tooltip={t('appDebug.vision.description')!}
  225. operations={
  226. <Switch size='md' defaultValue={inputs.vision.enabled} onChange={handleVisionResolutionEnabledChange} />
  227. }
  228. >
  229. {inputs.vision.enabled
  230. ? (
  231. <ResolutionPicker
  232. value={inputs.vision.configs?.detail || Resolution.high}
  233. onChange={handleVisionResolutionChange}
  234. />
  235. )
  236. : null}
  237. </Field>
  238. </>
  239. )}
  240. </div>
  241. <Split />
  242. <div className='px-4 pt-4 pb-2'>
  243. <OutputVars>
  244. <>
  245. <VarItem
  246. name='text'
  247. type='string'
  248. description={t(`${i18nPrefix}.outputVars.output`)}
  249. />
  250. </>
  251. </OutputVars>
  252. </div>
  253. {isShowSingleRun && (
  254. <BeforeRunForm
  255. nodeName={inputs.title}
  256. onHide={hideSingleRun}
  257. forms={singleRunForms}
  258. runningStatus={runningStatus}
  259. onRun={handleRun}
  260. onStop={handleStop}
  261. result={<ResultPanel {...runResult} showSteps={false} />}
  262. />
  263. )}
  264. </div>
  265. )
  266. }
  267. export default React.memo(Panel)