use-config.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import { useCallback, useEffect, useRef, useState } from 'react'
  2. import produce from 'immer'
  3. import { EditionType, VarType } from '../../types'
  4. import type { Memory, PromptItem, ValueSelector, Var, Variable } from '../../types'
  5. import { useStore } from '../../store'
  6. import {
  7. useIsChatMode,
  8. useNodesReadOnly,
  9. } from '../../hooks'
  10. import useAvailableVarList from '../_base/hooks/use-available-var-list'
  11. import useConfigVision from '../../hooks/use-config-vision'
  12. import type { LLMNodeType } from './types'
  13. import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  14. import {
  15. ModelTypeEnum,
  16. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  18. import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
  19. import { RETRIEVAL_OUTPUT_STRUCT } from '@/app/components/workflow/constants'
  20. import { checkHasContextBlock, checkHasHistoryBlock, checkHasQueryBlock } from '@/app/components/base/prompt-editor/constants'
  21. const useConfig = (id: string, payload: LLMNodeType) => {
  22. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  23. const isChatMode = useIsChatMode()
  24. const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
  25. const [defaultRolePrefix, setDefaultRolePrefix] = useState<{ user: string; assistant: string }>({ user: '', assistant: '' })
  26. const { inputs, setInputs: doSetInputs } = useNodeCrud<LLMNodeType>(id, payload)
  27. const inputRef = useRef(inputs)
  28. const setInputs = useCallback((newInputs: LLMNodeType) => {
  29. if (newInputs.memory && !newInputs.memory.role_prefix) {
  30. const newPayload = produce(newInputs, (draft) => {
  31. draft.memory!.role_prefix = defaultRolePrefix
  32. })
  33. doSetInputs(newPayload)
  34. inputRef.current = newPayload
  35. return
  36. }
  37. doSetInputs(newInputs)
  38. inputRef.current = newInputs
  39. }, [doSetInputs, defaultRolePrefix])
  40. // model
  41. const model = inputs.model
  42. const modelMode = inputs.model?.mode
  43. const isChatModel = modelMode === 'chat'
  44. const isCompletionModel = !isChatModel
  45. const hasSetBlockStatus = (() => {
  46. const promptTemplate = inputs.prompt_template
  47. const hasSetContext = isChatModel ? (promptTemplate as PromptItem[]).some(item => checkHasContextBlock(item.text)) : checkHasContextBlock((promptTemplate as PromptItem).text)
  48. if (!isChatMode) {
  49. return {
  50. history: false,
  51. query: false,
  52. context: hasSetContext,
  53. }
  54. }
  55. if (isChatModel) {
  56. return {
  57. history: false,
  58. query: (promptTemplate as PromptItem[]).some(item => checkHasQueryBlock(item.text)),
  59. context: hasSetContext,
  60. }
  61. }
  62. else {
  63. return {
  64. history: checkHasHistoryBlock((promptTemplate as PromptItem).text),
  65. query: checkHasQueryBlock((promptTemplate as PromptItem).text),
  66. context: hasSetContext,
  67. }
  68. }
  69. })()
  70. const shouldShowContextTip = !hasSetBlockStatus.context && inputs.context.enabled
  71. const appendDefaultPromptConfig = useCallback((draft: LLMNodeType, defaultConfig: any, passInIsChatMode?: boolean) => {
  72. const promptTemplates = defaultConfig.prompt_templates
  73. if (passInIsChatMode === undefined ? isChatModel : passInIsChatMode) {
  74. draft.prompt_template = promptTemplates.chat_model.prompts
  75. }
  76. else {
  77. draft.prompt_template = promptTemplates.completion_model.prompt
  78. setDefaultRolePrefix({
  79. user: promptTemplates.completion_model.conversation_histories_role.user_prefix,
  80. assistant: promptTemplates.completion_model.conversation_histories_role.assistant_prefix,
  81. })
  82. }
  83. }, [isChatModel])
  84. useEffect(() => {
  85. const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
  86. if (isReady && !inputs.prompt_template) {
  87. const newInputs = produce(inputs, (draft) => {
  88. appendDefaultPromptConfig(draft, defaultConfig)
  89. })
  90. setInputs(newInputs)
  91. }
  92. // eslint-disable-next-line react-hooks/exhaustive-deps
  93. }, [defaultConfig, isChatModel])
  94. const [modelChanged, setModelChanged] = useState(false)
  95. const {
  96. currentProvider,
  97. currentModel,
  98. } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration)
  99. const {
  100. isVisionModel,
  101. handleVisionResolutionEnabledChange,
  102. handleVisionResolutionChange,
  103. handleModelChanged: handleVisionConfigAfterModelChanged,
  104. } = useConfigVision(model, {
  105. payload: inputs.vision,
  106. onChange: (newPayload) => {
  107. const newInputs = produce(inputs, (draft) => {
  108. draft.vision = newPayload
  109. })
  110. setInputs(newInputs)
  111. },
  112. })
  113. const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
  114. const newInputs = produce(inputRef.current, (draft) => {
  115. draft.model.provider = model.provider
  116. draft.model.name = model.modelId
  117. draft.model.mode = model.mode!
  118. const isModeChange = model.mode !== inputRef.current.model.mode
  119. if (isModeChange && defaultConfig && Object.keys(defaultConfig).length > 0)
  120. appendDefaultPromptConfig(draft, defaultConfig, model.mode === 'chat')
  121. })
  122. setInputs(newInputs)
  123. setModelChanged(true)
  124. }, [setInputs, defaultConfig, appendDefaultPromptConfig])
  125. useEffect(() => {
  126. if (currentProvider?.provider && currentModel?.model && !model.provider) {
  127. handleModelChanged({
  128. provider: currentProvider?.provider,
  129. modelId: currentModel?.model,
  130. mode: currentModel?.model_properties?.mode as string,
  131. })
  132. }
  133. }, [model.provider, currentProvider, currentModel, handleModelChanged])
  134. const handleCompletionParamsChange = useCallback((newParams: Record<string, any>) => {
  135. const newInputs = produce(inputs, (draft) => {
  136. draft.model.completion_params = newParams
  137. })
  138. setInputs(newInputs)
  139. }, [inputs, setInputs])
  140. // change to vision model to set vision enabled, else disabled
  141. useEffect(() => {
  142. if (!modelChanged)
  143. return
  144. setModelChanged(false)
  145. handleVisionConfigAfterModelChanged()
  146. // eslint-disable-next-line react-hooks/exhaustive-deps
  147. }, [isVisionModel, modelChanged])
  148. // variables
  149. const isShowVars = (() => {
  150. if (isChatModel)
  151. return (inputs.prompt_template as PromptItem[]).some(item => item.edition_type === EditionType.jinja2)
  152. return (inputs.prompt_template as PromptItem).edition_type === EditionType.jinja2
  153. })()
  154. const handleAddEmptyVariable = useCallback(() => {
  155. const newInputs = produce(inputRef.current, (draft) => {
  156. if (!draft.prompt_config) {
  157. draft.prompt_config = {
  158. jinja2_variables: [],
  159. }
  160. }
  161. if (!draft.prompt_config.jinja2_variables)
  162. draft.prompt_config.jinja2_variables = []
  163. draft.prompt_config.jinja2_variables.push({
  164. variable: '',
  165. value_selector: [],
  166. })
  167. })
  168. setInputs(newInputs)
  169. }, [setInputs])
  170. const handleAddVariable = useCallback((payload: Variable) => {
  171. const newInputs = produce(inputRef.current, (draft) => {
  172. if (!draft.prompt_config) {
  173. draft.prompt_config = {
  174. jinja2_variables: [],
  175. }
  176. }
  177. if (!draft.prompt_config.jinja2_variables)
  178. draft.prompt_config.jinja2_variables = []
  179. draft.prompt_config.jinja2_variables.push(payload)
  180. })
  181. setInputs(newInputs)
  182. }, [setInputs])
  183. const handleVarListChange = useCallback((newList: Variable[]) => {
  184. const newInputs = produce(inputRef.current, (draft) => {
  185. if (!draft.prompt_config) {
  186. draft.prompt_config = {
  187. jinja2_variables: [],
  188. }
  189. }
  190. if (!draft.prompt_config.jinja2_variables)
  191. draft.prompt_config.jinja2_variables = []
  192. draft.prompt_config.jinja2_variables = newList
  193. })
  194. setInputs(newInputs)
  195. }, [setInputs])
  196. const handleVarNameChange = useCallback((oldName: string, newName: string) => {
  197. const newInputs = produce(inputRef.current, (draft) => {
  198. if (isChatModel) {
  199. const promptTemplate = draft.prompt_template as PromptItem[]
  200. promptTemplate.filter(item => item.edition_type === EditionType.jinja2).forEach((item) => {
  201. item.jinja2_text = (item.jinja2_text || '').replaceAll(`{{ ${oldName} }}`, `{{ ${newName} }}`)
  202. })
  203. }
  204. else {
  205. if ((draft.prompt_template as PromptItem).edition_type !== EditionType.jinja2)
  206. return
  207. const promptTemplate = draft.prompt_template as PromptItem
  208. promptTemplate.jinja2_text = (promptTemplate.jinja2_text || '').replaceAll(`{{ ${oldName} }}`, `{{ ${newName} }}`)
  209. }
  210. })
  211. setInputs(newInputs)
  212. }, [isChatModel, setInputs])
  213. // context
  214. const handleContextVarChange = useCallback((newVar: ValueSelector | string) => {
  215. const newInputs = produce(inputs, (draft) => {
  216. draft.context.variable_selector = newVar as ValueSelector || []
  217. draft.context.enabled = !!(newVar && newVar.length > 0)
  218. })
  219. setInputs(newInputs)
  220. }, [inputs, setInputs])
  221. const handlePromptChange = useCallback((newPrompt: PromptItem[] | PromptItem) => {
  222. const newInputs = produce(inputRef.current, (draft) => {
  223. draft.prompt_template = newPrompt
  224. })
  225. setInputs(newInputs)
  226. }, [setInputs])
  227. const handleMemoryChange = useCallback((newMemory?: Memory) => {
  228. const newInputs = produce(inputs, (draft) => {
  229. draft.memory = newMemory
  230. })
  231. setInputs(newInputs)
  232. }, [inputs, setInputs])
  233. const handleSyeQueryChange = useCallback((newQuery: string) => {
  234. const newInputs = produce(inputs, (draft) => {
  235. if (!draft.memory) {
  236. draft.memory = {
  237. window: {
  238. enabled: false,
  239. size: 10,
  240. },
  241. query_prompt_template: newQuery,
  242. }
  243. }
  244. else {
  245. draft.memory.query_prompt_template = newQuery
  246. }
  247. })
  248. setInputs(newInputs)
  249. }, [inputs, setInputs])
  250. const filterInputVar = useCallback((varPayload: Var) => {
  251. return [VarType.number, VarType.string, VarType.secret, VarType.arrayString, VarType.arrayNumber, VarType.file, VarType.arrayFile].includes(varPayload.type)
  252. }, [])
  253. const filterJinjia2InputVar = useCallback((varPayload: Var) => {
  254. return [VarType.number, VarType.string, VarType.secret, VarType.arrayString, VarType.arrayNumber].includes(varPayload.type)
  255. }, [])
  256. const filterMemoryPromptVar = useCallback((varPayload: Var) => {
  257. return [VarType.arrayObject, VarType.array, VarType.number, VarType.string, VarType.secret, VarType.arrayString, VarType.arrayNumber, VarType.file, VarType.arrayFile].includes(varPayload.type)
  258. }, [])
  259. const {
  260. availableVars,
  261. availableNodesWithParent,
  262. } = useAvailableVarList(id, {
  263. onlyLeafNodeVar: false,
  264. filterVar: filterMemoryPromptVar,
  265. })
  266. // single run
  267. const {
  268. isShowSingleRun,
  269. hideSingleRun,
  270. getInputVars,
  271. runningStatus,
  272. handleRun,
  273. handleStop,
  274. runInputData,
  275. setRunInputData,
  276. runResult,
  277. toVarInputs,
  278. } = useOneStepRun<LLMNodeType>({
  279. id,
  280. data: inputs,
  281. defaultRunInputData: {
  282. '#context#': [RETRIEVAL_OUTPUT_STRUCT],
  283. '#files#': [],
  284. },
  285. })
  286. const inputVarValues = (() => {
  287. const vars: Record<string, any> = {}
  288. Object.keys(runInputData)
  289. .filter(key => !['#context#', '#files#'].includes(key))
  290. .forEach((key) => {
  291. vars[key] = runInputData[key]
  292. })
  293. return vars
  294. })()
  295. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  296. const newVars = {
  297. ...newPayload,
  298. '#context#': runInputData['#context#'],
  299. '#files#': runInputData['#files#'],
  300. }
  301. setRunInputData(newVars)
  302. }, [runInputData, setRunInputData])
  303. const contexts = runInputData['#context#']
  304. const setContexts = useCallback((newContexts: string[]) => {
  305. setRunInputData({
  306. ...runInputData,
  307. '#context#': newContexts,
  308. })
  309. }, [runInputData, setRunInputData])
  310. const visionFiles = runInputData['#files#']
  311. const setVisionFiles = useCallback((newFiles: any[]) => {
  312. setRunInputData({
  313. ...runInputData,
  314. '#files#': newFiles,
  315. })
  316. }, [runInputData, setRunInputData])
  317. const allVarStrArr = (() => {
  318. const arr = isChatModel ? (inputs.prompt_template as PromptItem[]).filter(item => item.edition_type !== EditionType.jinja2).map(item => item.text) : [(inputs.prompt_template as PromptItem).text]
  319. if (isChatMode && isChatModel && !!inputs.memory) {
  320. arr.push('{{#sys.query#}}')
  321. arr.push(inputs.memory.query_prompt_template)
  322. }
  323. return arr
  324. })()
  325. const varInputs = (() => {
  326. const vars = getInputVars(allVarStrArr)
  327. if (isShowVars)
  328. return [...vars, ...toVarInputs(inputs.prompt_config?.jinja2_variables || [])]
  329. return vars
  330. })()
  331. return {
  332. readOnly,
  333. isChatMode,
  334. inputs,
  335. isChatModel,
  336. isCompletionModel,
  337. hasSetBlockStatus,
  338. shouldShowContextTip,
  339. isVisionModel,
  340. handleModelChanged,
  341. handleCompletionParamsChange,
  342. isShowVars,
  343. handleVarListChange,
  344. handleVarNameChange,
  345. handleAddVariable,
  346. handleAddEmptyVariable,
  347. handleContextVarChange,
  348. filterInputVar,
  349. filterVar: filterMemoryPromptVar,
  350. availableVars,
  351. availableNodesWithParent,
  352. handlePromptChange,
  353. handleMemoryChange,
  354. handleSyeQueryChange,
  355. handleVisionResolutionEnabledChange,
  356. handleVisionResolutionChange,
  357. isShowSingleRun,
  358. hideSingleRun,
  359. inputVarValues,
  360. setInputVarValues,
  361. visionFiles,
  362. setVisionFiles,
  363. contexts,
  364. setContexts,
  365. varInputs,
  366. runningStatus,
  367. handleRun,
  368. handleStop,
  369. runResult,
  370. filterJinjia2InputVar,
  371. }
  372. }
  373. export default useConfig