panel.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import type { FC } from 'react'
  2. import React, { useMemo } from 'react'
  3. import { useStoreApi } from 'reactflow'
  4. import { useTranslation } from 'react-i18next'
  5. import { BlockEnum } from '../../types'
  6. import MemoryConfig from '../_base/components/memory-config'
  7. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  8. import useConfig from './use-config'
  9. import ResolutionPicker from './components/resolution-picker'
  10. import type { LLMNodeType } from './types'
  11. import ConfigPrompt from './components/config-prompt'
  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 TooltipPlus from '@/app/components/base/tooltip-plus'
  22. import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
  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 store = useStoreApi()
  32. const startNode = useMemo(() => {
  33. const nodes = store.getState().getNodes()
  34. return nodes.find(node => node.data.type === BlockEnum.Start)
  35. }, [store])
  36. const {
  37. readOnly,
  38. inputs,
  39. isChatModel,
  40. isChatMode,
  41. isCompletionModel,
  42. shouldShowContextTip,
  43. isShowVisionConfig,
  44. handleModelChanged,
  45. hasSetBlockStatus,
  46. handleCompletionParamsChange,
  47. handleContextVarChange,
  48. filterInputVar,
  49. filterVar,
  50. availableVars,
  51. availableNodes,
  52. handlePromptChange,
  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. />
  169. )}
  170. {/* Memory put place examples. */}
  171. {isChatMode && isChatModel && !!inputs.memory && (
  172. <div className='mt-4'>
  173. <div className='flex justify-between items-center h-8 pl-3 pr-2 rounded-lg bg-gray-100'>
  174. <div className='flex items-center space-x-1'>
  175. <div className='text-xs font-semibold text-gray-700 uppercase'>{t('workflow.nodes.common.memories.title')}</div>
  176. <TooltipPlus
  177. popupContent={t('workflow.nodes.common.memories.tip')}
  178. >
  179. <HelpCircle className='w-3.5 h-3.5 text-gray-400' />
  180. </TooltipPlus>
  181. </div>
  182. <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>
  183. </div>
  184. {/* Readonly User Query */}
  185. <div className='mt-4'>
  186. <Editor
  187. title={<div className='flex items-center space-x-1'>
  188. <div className='text-xs font-semibold text-gray-700 uppercase'>user</div>
  189. <TooltipPlus
  190. popupContent={
  191. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  192. }
  193. >
  194. <HelpCircle className='w-3.5 h-3.5 text-gray-400' />
  195. </TooltipPlus>
  196. </div>}
  197. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  198. onChange={handleSyeQueryChange}
  199. readOnly={readOnly}
  200. isShowContext={false}
  201. isChatApp
  202. isChatModel
  203. hasSetBlockStatus={hasSetBlockStatus}
  204. nodesOutputVars={availableVars}
  205. availableNodes={availableNodes}
  206. />
  207. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  208. <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  209. )}
  210. </div>
  211. </div>
  212. )}
  213. {/* Memory */}
  214. {isChatMode && (
  215. <>
  216. <Split />
  217. <MemoryConfig
  218. readonly={readOnly}
  219. config={{ data: inputs.memory }}
  220. onChange={handleMemoryChange}
  221. canSetRoleName={isCompletionModel}
  222. />
  223. </>
  224. )}
  225. {/* Vision: GPT4-vision and so on */}
  226. {isShowVisionConfig && (
  227. <>
  228. <Split />
  229. <Field
  230. title={t(`${i18nPrefix}.vision`)}
  231. tooltip={t('appDebug.vision.description')!}
  232. operations={
  233. <Switch size='md' defaultValue={inputs.vision.enabled} onChange={handleVisionResolutionEnabledChange} />
  234. }
  235. >
  236. {inputs.vision.enabled
  237. ? (
  238. <ResolutionPicker
  239. value={inputs.vision.configs?.detail || Resolution.high}
  240. onChange={handleVisionResolutionChange}
  241. />
  242. )
  243. : null}
  244. </Field>
  245. </>
  246. )}
  247. </div>
  248. <Split />
  249. <div className='px-4 pt-4 pb-2'>
  250. <OutputVars>
  251. <>
  252. <VarItem
  253. name='text'
  254. type='string'
  255. description={t(`${i18nPrefix}.outputVars.output`)}
  256. />
  257. </>
  258. </OutputVars>
  259. </div>
  260. {isShowSingleRun && (
  261. <BeforeRunForm
  262. nodeName={inputs.title}
  263. onHide={hideSingleRun}
  264. forms={singleRunForms}
  265. runningStatus={runningStatus}
  266. onRun={handleRun}
  267. onStop={handleStop}
  268. result={<ResultPanel {...runResult} showSteps={false} />}
  269. />
  270. )}
  271. </div>
  272. )
  273. }
  274. export default React.memo(Panel)