You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

panel.tsx 12KB

Introduce Plugins (#13836) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: xhe <xw897002528@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: takatost <takatost@gmail.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: Yi <yxiaoisme@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: AkaraChen <85140972+AkaraChen@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: Hiroki Nagai <82458324+nagaihiroki-git@users.noreply.github.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in> Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com> Co-authored-by: Ademílson Tonato <ademilsonft@outlook.com> Co-authored-by: Ademílson Tonato <ademilson.tonato@refurbed.com> Co-authored-by: IWAI, Masaharu <iwaim.sub@gmail.com> Co-authored-by: Yueh-Po Peng (Yabi) <94939112+y10ab1@users.noreply.github.com> Co-authored-by: Jason <ggbbddjm@gmail.com> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: yjc980121 <3898524+yjc980121@users.noreply.github.com> Co-authored-by: heyszt <36215648+hieheihei@users.noreply.github.com> Co-authored-by: Abdullah AlOsaimi <osaimiacc@gmail.com> Co-authored-by: Abdullah AlOsaimi <189027247+osaimi@users.noreply.github.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: zuodongxu <192560071+zuodongxu@users.noreply.github.com> Co-authored-by: Masashi Tomooka <tmokmss@users.noreply.github.com> Co-authored-by: aplio <ryo.091219@gmail.com> Co-authored-by: Obada Khalili <54270856+obadakhalili@users.noreply.github.com> Co-authored-by: Nam Vu <zuzoovn@gmail.com> Co-authored-by: Kei YAMAZAKI <1715090+kei-yamazaki@users.noreply.github.com> Co-authored-by: TechnoHouse <13776377+deephbz@users.noreply.github.com> Co-authored-by: Riddhimaan-Senapati <114703025+Riddhimaan-Senapati@users.noreply.github.com> Co-authored-by: MaFee921 <31881301+2284730142@users.noreply.github.com> Co-authored-by: te-chan <t-nakanome@sakura-is.co.jp> Co-authored-by: HQidea <HQidea@users.noreply.github.com> Co-authored-by: Joshbly <36315710+Joshbly@users.noreply.github.com> Co-authored-by: xhe <xw897002528@gmail.com> Co-authored-by: weiwenyan-dev <154779315+weiwenyan-dev@users.noreply.github.com> Co-authored-by: ex_wenyan.wei <ex_wenyan.wei@tcl.com> Co-authored-by: engchina <12236799+engchina@users.noreply.github.com> Co-authored-by: engchina <atjapan2015@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <253605712@qq.com> Co-authored-by: Kemal <kemalmeler@outlook.com> Co-authored-by: Lazy_Frog <4590648+lazyFrogLOL@users.noreply.github.com> Co-authored-by: Yi Xiao <54782454+YIXIAO0@users.noreply.github.com> Co-authored-by: Steven sun <98230804+Tuyohai@users.noreply.github.com> Co-authored-by: steven <sunzwj@digitalchina.com> Co-authored-by: Kalo Chin <91766386+fdb02983rhy@users.noreply.github.com> Co-authored-by: Katy Tao <34019945+KatyTao@users.noreply.github.com> Co-authored-by: depy <42985524+h4ckdepy@users.noreply.github.com> Co-authored-by: 胡春东 <gycm520@gmail.com> Co-authored-by: Junjie.M <118170653@qq.com> Co-authored-by: MuYu <mr.muzea@gmail.com> Co-authored-by: Naoki Takashima <39912547+takatea@users.noreply.github.com> Co-authored-by: Summer-Gu <37869445+gubinjie@users.noreply.github.com> Co-authored-by: Fei He <droxer.he@gmail.com> Co-authored-by: ybalbert001 <120714773+ybalbert001@users.noreply.github.com> Co-authored-by: Yuanbo Li <ybalbert@amazon.com> Co-authored-by: douxc <7553076+douxc@users.noreply.github.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: Wu Jiayang <62842862+Wu-Jiayang@users.noreply.github.com> Co-authored-by: Your Name <you@example.com> Co-authored-by: kimjion <45935338+kimjion@users.noreply.github.com> Co-authored-by: AugNSo <song.tiankai@icloud.com> Co-authored-by: llinvokerl <38915183+llinvokerl@users.noreply.github.com> Co-authored-by: liusurong.lsr <liusurong.lsr@alibaba-inc.com> Co-authored-by: Vasu Negi <vasu-negi@users.noreply.github.com> Co-authored-by: Hundredwz <1808096180@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import type { FC } from 'react'
  2. import React, { useCallback } 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 { findVariableWhenOnLLMVision } from '../utils'
  9. import type { LLMNodeType } from './types'
  10. import ConfigPrompt from './components/config-prompt'
  11. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  12. import AddButton2 from '@/app/components/base/button/add-button'
  13. import Field from '@/app/components/workflow/nodes/_base/components/field'
  14. import Split from '@/app/components/workflow/nodes/_base/components/split'
  15. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  16. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  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 Tooltip from '@/app/components/base/tooltip'
  22. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  23. import StructureOutput from './components/structure-output'
  24. import Switch from '@/app/components/base/switch'
  25. import { RiAlertFill, RiQuestionLine } from '@remixicon/react'
  26. const i18nPrefix = 'workflow.nodes.llm'
  27. const Panel: FC<NodePanelProps<LLMNodeType>> = ({
  28. id,
  29. data,
  30. }) => {
  31. const { t } = useTranslation()
  32. const {
  33. readOnly,
  34. inputs,
  35. isChatModel,
  36. isChatMode,
  37. isCompletionModel,
  38. shouldShowContextTip,
  39. isVisionModel,
  40. handleModelChanged,
  41. hasSetBlockStatus,
  42. handleCompletionParamsChange,
  43. handleContextVarChange,
  44. filterInputVar,
  45. filterVar,
  46. availableVars,
  47. availableNodesWithParent,
  48. isShowVars,
  49. handlePromptChange,
  50. handleAddEmptyVariable,
  51. handleAddVariable,
  52. handleVarListChange,
  53. handleVarNameChange,
  54. handleSyeQueryChange,
  55. handleMemoryChange,
  56. handleVisionResolutionEnabledChange,
  57. handleVisionResolutionChange,
  58. isShowSingleRun,
  59. hideSingleRun,
  60. inputVarValues,
  61. setInputVarValues,
  62. visionFiles,
  63. setVisionFiles,
  64. contexts,
  65. setContexts,
  66. runningStatus,
  67. isModelSupportStructuredOutput,
  68. structuredOutputCollapsed,
  69. setStructuredOutputCollapsed,
  70. handleStructureOutputEnableChange,
  71. handleStructureOutputChange,
  72. handleRun,
  73. handleStop,
  74. varInputs,
  75. runResult,
  76. filterJinjia2InputVar,
  77. } = useConfig(id, data)
  78. const model = inputs.model
  79. const singleRunForms = (() => {
  80. const forms: FormProps[] = []
  81. if (varInputs.length > 0) {
  82. forms.push(
  83. {
  84. label: t(`${i18nPrefix}.singleRun.variable`)!,
  85. inputs: varInputs,
  86. values: inputVarValues,
  87. onChange: setInputVarValues,
  88. },
  89. )
  90. }
  91. if (inputs.context?.variable_selector && inputs.context?.variable_selector.length > 0) {
  92. forms.push(
  93. {
  94. label: t(`${i18nPrefix}.context`)!,
  95. inputs: [{
  96. label: '',
  97. variable: '#context#',
  98. type: InputVarType.contexts,
  99. required: false,
  100. }],
  101. values: { '#context#': contexts },
  102. onChange: keyValue => setContexts(keyValue['#context#']),
  103. },
  104. )
  105. }
  106. if (isVisionModel && data.vision?.enabled && data.vision?.configs?.variable_selector) {
  107. const currentVariable = findVariableWhenOnLLMVision(data.vision.configs.variable_selector, availableVars)
  108. forms.push(
  109. {
  110. label: t(`${i18nPrefix}.vision`)!,
  111. inputs: [{
  112. label: currentVariable?.variable as any,
  113. variable: '#files#',
  114. type: currentVariable?.formType as any,
  115. required: false,
  116. }],
  117. values: { '#files#': visionFiles },
  118. onChange: keyValue => setVisionFiles((keyValue as any)['#files#']),
  119. },
  120. )
  121. }
  122. return forms
  123. })()
  124. const handleModelChange = useCallback((model: {
  125. provider: string
  126. modelId: string
  127. mode?: string
  128. }) => {
  129. handleCompletionParamsChange({})
  130. handleModelChanged(model)
  131. // eslint-disable-next-line react-hooks/exhaustive-deps
  132. }, [])
  133. return (
  134. <div className='mt-2'>
  135. <div className='space-y-4 px-4 pb-4'>
  136. <Field
  137. title={t(`${i18nPrefix}.model`)}
  138. required
  139. >
  140. <ModelParameterModal
  141. popupClassName='!w-[387px]'
  142. isInWorkflow
  143. isAdvancedMode={true}
  144. mode={model?.mode}
  145. provider={model?.provider}
  146. completionParams={model?.completion_params}
  147. modelId={model?.name}
  148. setModel={handleModelChange}
  149. onCompletionParamsChange={handleCompletionParamsChange}
  150. hideDebugWithMultipleModel
  151. debugWithMultipleModel={false}
  152. readonly={readOnly}
  153. />
  154. </Field>
  155. {/* knowledge */}
  156. <Field
  157. title={t(`${i18nPrefix}.context`)}
  158. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  159. >
  160. <>
  161. <VarReferencePicker
  162. readonly={readOnly}
  163. nodeId={id}
  164. isShowNodeName
  165. value={inputs.context?.variable_selector || []}
  166. onChange={handleContextVarChange}
  167. filterVar={filterVar}
  168. />
  169. {shouldShowContextTip && (
  170. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  171. )}
  172. </>
  173. </Field>
  174. {/* Prompt */}
  175. {model.name && (
  176. <ConfigPrompt
  177. readOnly={readOnly}
  178. nodeId={id}
  179. filterVar={filterInputVar}
  180. isChatModel={isChatModel}
  181. isChatApp={isChatMode}
  182. isShowContext
  183. payload={inputs.prompt_template}
  184. onChange={handlePromptChange}
  185. hasSetBlockStatus={hasSetBlockStatus}
  186. varList={inputs.prompt_config?.jinja2_variables || []}
  187. handleAddVariable={handleAddVariable}
  188. modelConfig={model}
  189. />
  190. )}
  191. {isShowVars && (
  192. <Field
  193. title={t('workflow.nodes.templateTransform.inputVars')}
  194. operations={
  195. !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined
  196. }
  197. >
  198. <VarList
  199. nodeId={id}
  200. readonly={readOnly}
  201. list={inputs.prompt_config?.jinja2_variables || []}
  202. onChange={handleVarListChange}
  203. onVarNameChange={handleVarNameChange}
  204. filterVar={filterJinjia2InputVar}
  205. isSupportFileVar={false}
  206. />
  207. </Field>
  208. )}
  209. {/* Memory put place examples. */}
  210. {isChatMode && isChatModel && !!inputs.memory && (
  211. <div className='mt-4'>
  212. <div className='flex h-8 items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2'>
  213. <div className='flex items-center space-x-1'>
  214. <div className='text-xs font-semibold uppercase text-text-secondary'>{t('workflow.nodes.common.memories.title')}</div>
  215. <Tooltip
  216. popupContent={t('workflow.nodes.common.memories.tip')}
  217. triggerClassName='w-4 h-4'
  218. />
  219. </div>
  220. <div className='flex h-[18px] items-center rounded-[5px] border border-divider-deep bg-components-badge-bg-dimm px-1 text-xs font-semibold uppercase text-text-tertiary'>{t('workflow.nodes.common.memories.builtIn')}</div>
  221. </div>
  222. {/* Readonly User Query */}
  223. <div className='mt-4'>
  224. <Editor
  225. title={<div className='flex items-center space-x-1'>
  226. <div className='text-xs font-semibold uppercase text-text-secondary'>user</div>
  227. <Tooltip
  228. popupContent={
  229. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  230. }
  231. triggerClassName='w-4 h-4'
  232. />
  233. </div>}
  234. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  235. onChange={handleSyeQueryChange}
  236. readOnly={readOnly}
  237. isShowContext={false}
  238. isChatApp
  239. isChatModel
  240. hasSetBlockStatus={hasSetBlockStatus}
  241. nodesOutputVars={availableVars}
  242. availableNodes={availableNodesWithParent}
  243. isSupportFileVar
  244. />
  245. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  246. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  247. )}
  248. </div>
  249. </div>
  250. )}
  251. {/* Memory */}
  252. {isChatMode && (
  253. <>
  254. <Split />
  255. <MemoryConfig
  256. readonly={readOnly}
  257. config={{ data: inputs.memory }}
  258. onChange={handleMemoryChange}
  259. canSetRoleName={isCompletionModel}
  260. />
  261. </>
  262. )}
  263. {/* Vision: GPT4-vision and so on */}
  264. <ConfigVision
  265. nodeId={id}
  266. readOnly={readOnly}
  267. isVisionModel={isVisionModel}
  268. enabled={inputs.vision?.enabled}
  269. onEnabledChange={handleVisionResolutionEnabledChange}
  270. config={inputs.vision?.configs}
  271. onConfigChange={handleVisionResolutionChange}
  272. />
  273. </div>
  274. <Split />
  275. <OutputVars
  276. collapsed={structuredOutputCollapsed}
  277. onCollapse={setStructuredOutputCollapsed}
  278. operations={
  279. <div className='mr-4 flex shrink-0 items-center'>
  280. {(!isModelSupportStructuredOutput && !!inputs.structured_output_enabled) && (
  281. <Tooltip noDecoration popupContent={
  282. <div className='w-[232px] rounded-xl border-[0.5px] border-components-panel-border bg-components-tooltip-bg px-4 py-3.5 shadow-lg backdrop-blur-[5px]'>
  283. <div className='title-xs-semi-bold text-text-primary'>{t('app.structOutput.modelNotSupported')}</div>
  284. <div className='body-xs-regular mt-1 text-text-secondary'>{t('app.structOutput.modelNotSupportedTip')}</div>
  285. </div>
  286. }>
  287. <div>
  288. <RiAlertFill className='mr-1 size-4 text-text-warning-secondary' />
  289. </div>
  290. </Tooltip>
  291. )}
  292. <div className='system-xs-medium-uppercase mr-0.5 text-text-tertiary'>{t('app.structOutput.structured')}</div>
  293. <Tooltip popupContent={
  294. <div className='max-w-[150px]'>{t('app.structOutput.structuredTip')}</div>
  295. }>
  296. <div>
  297. <RiQuestionLine className='size-3.5 text-text-quaternary' />
  298. </div>
  299. </Tooltip>
  300. <Switch
  301. className='ml-2'
  302. defaultValue={!!inputs.structured_output_enabled}
  303. onChange={handleStructureOutputEnableChange}
  304. size='md'
  305. disabled={readOnly}
  306. />
  307. </div>
  308. }
  309. >
  310. <>
  311. <VarItem
  312. name='text'
  313. type='string'
  314. description={t(`${i18nPrefix}.outputVars.output`)}
  315. />
  316. {inputs.structured_output_enabled && (
  317. <>
  318. <Split className='mt-3' />
  319. <StructureOutput
  320. className='mt-4'
  321. value={inputs.structured_output}
  322. onChange={handleStructureOutputChange}
  323. />
  324. </>
  325. )}
  326. </>
  327. </OutputVars>
  328. {isShowSingleRun && (
  329. <BeforeRunForm
  330. nodeName={inputs.title}
  331. nodeType={inputs.type}
  332. onHide={hideSingleRun}
  333. forms={singleRunForms}
  334. runningStatus={runningStatus}
  335. onRun={handleRun}
  336. onStop={handleStop}
  337. result={<ResultPanel {...runResult} showSteps={false} />}
  338. />
  339. )}
  340. </div>
  341. )
  342. }
  343. export default React.memo(Panel)