panel.tsx 8.8 KB

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