您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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 个月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. >
  139. <ModelParameterModal
  140. popupClassName='!w-[387px]'
  141. isInWorkflow
  142. isAdvancedMode={true}
  143. mode={model?.mode}
  144. provider={model?.provider}
  145. completionParams={model?.completion_params}
  146. modelId={model?.name}
  147. setModel={handleModelChange}
  148. onCompletionParamsChange={handleCompletionParamsChange}
  149. hideDebugWithMultipleModel
  150. debugWithMultipleModel={false}
  151. readonly={readOnly}
  152. />
  153. </Field>
  154. {/* knowledge */}
  155. <Field
  156. title={t(`${i18nPrefix}.context`)}
  157. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  158. >
  159. <>
  160. <VarReferencePicker
  161. readonly={readOnly}
  162. nodeId={id}
  163. isShowNodeName
  164. value={inputs.context?.variable_selector || []}
  165. onChange={handleContextVarChange}
  166. filterVar={filterVar}
  167. />
  168. {shouldShowContextTip && (
  169. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  170. )}
  171. </>
  172. </Field>
  173. {/* Prompt */}
  174. {model.name && (
  175. <ConfigPrompt
  176. readOnly={readOnly}
  177. nodeId={id}
  178. filterVar={filterInputVar}
  179. isChatModel={isChatModel}
  180. isChatApp={isChatMode}
  181. isShowContext
  182. payload={inputs.prompt_template}
  183. onChange={handlePromptChange}
  184. hasSetBlockStatus={hasSetBlockStatus}
  185. varList={inputs.prompt_config?.jinja2_variables || []}
  186. handleAddVariable={handleAddVariable}
  187. modelConfig={model}
  188. />
  189. )}
  190. {isShowVars && (
  191. <Field
  192. title={t('workflow.nodes.templateTransform.inputVars')}
  193. operations={
  194. !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined
  195. }
  196. >
  197. <VarList
  198. nodeId={id}
  199. readonly={readOnly}
  200. list={inputs.prompt_config?.jinja2_variables || []}
  201. onChange={handleVarListChange}
  202. onVarNameChange={handleVarNameChange}
  203. filterVar={filterJinjia2InputVar}
  204. isSupportFileVar={false}
  205. />
  206. </Field>
  207. )}
  208. {/* Memory put place examples. */}
  209. {isChatMode && isChatModel && !!inputs.memory && (
  210. <div className='mt-4'>
  211. <div className='flex h-8 items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2'>
  212. <div className='flex items-center space-x-1'>
  213. <div className='text-xs font-semibold uppercase text-text-secondary'>{t('workflow.nodes.common.memories.title')}</div>
  214. <Tooltip
  215. popupContent={t('workflow.nodes.common.memories.tip')}
  216. triggerClassName='w-4 h-4'
  217. />
  218. </div>
  219. <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>
  220. </div>
  221. {/* Readonly User Query */}
  222. <div className='mt-4'>
  223. <Editor
  224. title={<div className='flex items-center space-x-1'>
  225. <div className='text-xs font-semibold uppercase text-text-secondary'>user</div>
  226. <Tooltip
  227. popupContent={
  228. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  229. }
  230. triggerClassName='w-4 h-4'
  231. />
  232. </div>}
  233. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  234. onChange={handleSyeQueryChange}
  235. readOnly={readOnly}
  236. isShowContext={false}
  237. isChatApp
  238. isChatModel
  239. hasSetBlockStatus={hasSetBlockStatus}
  240. nodesOutputVars={availableVars}
  241. availableNodes={availableNodesWithParent}
  242. isSupportFileVar
  243. />
  244. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  245. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  246. )}
  247. </div>
  248. </div>
  249. )}
  250. {/* Memory */}
  251. {isChatMode && (
  252. <>
  253. <Split />
  254. <MemoryConfig
  255. readonly={readOnly}
  256. config={{ data: inputs.memory }}
  257. onChange={handleMemoryChange}
  258. canSetRoleName={isCompletionModel}
  259. />
  260. </>
  261. )}
  262. {/* Vision: GPT4-vision and so on */}
  263. <ConfigVision
  264. nodeId={id}
  265. readOnly={readOnly}
  266. isVisionModel={isVisionModel}
  267. enabled={inputs.vision?.enabled}
  268. onEnabledChange={handleVisionResolutionEnabledChange}
  269. config={inputs.vision?.configs}
  270. onConfigChange={handleVisionResolutionChange}
  271. />
  272. </div>
  273. <Split />
  274. <OutputVars
  275. collapsed={structuredOutputCollapsed}
  276. onCollapse={setStructuredOutputCollapsed}
  277. operations={
  278. <div className='mr-4 flex shrink-0 items-center'>
  279. {!isModelSupportStructuredOutput && (
  280. <Tooltip noDecoration popupContent={
  281. <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]'>
  282. <div className='title-xs-semi-bold text-text-primary'>{t('app.structOutput.modelNotSupported')}</div>
  283. <div className='body-xs-regular mt-1 text-text-secondary'>{t('app.structOutput.modelNotSupportedTip')}</div>
  284. </div>
  285. }>
  286. <div>
  287. <RiAlertFill className='mr-1 size-4 text-text-warning-secondary' />
  288. </div>
  289. </Tooltip>
  290. )}
  291. <div className='system-xs-medium-uppercase mr-0.5 text-text-tertiary'>{t('app.structOutput.structured')}</div>
  292. <Tooltip popupContent={
  293. <div className='max-w-[150px]'>{t('app.structOutput.structuredTip')}</div>
  294. }>
  295. <div>
  296. <RiQuestionLine className='size-3.5 text-text-quaternary' />
  297. </div>
  298. </Tooltip>
  299. <Switch
  300. className='ml-2'
  301. defaultValue={!!inputs.structured_output_enabled}
  302. onChange={handleStructureOutputEnableChange}
  303. size='md'
  304. disabled={readOnly}
  305. />
  306. </div>
  307. }
  308. >
  309. <>
  310. <VarItem
  311. name='text'
  312. type='string'
  313. description={t(`${i18nPrefix}.outputVars.output`)}
  314. />
  315. {inputs.structured_output_enabled && (
  316. <>
  317. <Split className='mt-3' />
  318. <StructureOutput
  319. className='mt-4'
  320. value={inputs.structured_output}
  321. onChange={handleStructureOutputChange}
  322. />
  323. </>
  324. )}
  325. </>
  326. </OutputVars>
  327. {isShowSingleRun && (
  328. <BeforeRunForm
  329. nodeName={inputs.title}
  330. nodeType={inputs.type}
  331. onHide={hideSingleRun}
  332. forms={singleRunForms}
  333. runningStatus={runningStatus}
  334. onRun={handleRun}
  335. onStop={handleStop}
  336. result={<ResultPanel {...runResult} showSteps={false} />}
  337. />
  338. )}
  339. </div>
  340. )
  341. }
  342. export default React.memo(Panel)