panel.tsx 9.2 KB

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