Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

default.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // import { RETRIEVAL_OUTPUT_STRUCT } from '../../constants'
  2. import { BlockEnum, EditionType } from '../../types'
  3. import { type NodeDefault, type PromptItem, PromptRole } from '../../types'
  4. import type { LLMNodeType } from './types'
  5. import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/blocks'
  6. const RETRIEVAL_OUTPUT_STRUCT = `{
  7. "content": "",
  8. "title": "",
  9. "url": "",
  10. "icon": "",
  11. "metadata": {
  12. "dataset_id": "",
  13. "dataset_name": "",
  14. "document_id": [],
  15. "document_name": "",
  16. "document_data_source_type": "",
  17. "segment_id": "",
  18. "segment_position": "",
  19. "segment_word_count": "",
  20. "segment_hit_count": "",
  21. "segment_index_node_hash": "",
  22. "score": ""
  23. }
  24. }`
  25. const i18nPrefix = 'workflow.errorMsg'
  26. const nodeDefault: NodeDefault<LLMNodeType> = {
  27. defaultValue: {
  28. model: {
  29. provider: '',
  30. name: '',
  31. mode: 'chat',
  32. completion_params: {
  33. temperature: 0.7,
  34. },
  35. },
  36. prompt_template: [{
  37. role: PromptRole.system,
  38. text: '',
  39. }],
  40. context: {
  41. enabled: false,
  42. variable_selector: [],
  43. },
  44. vision: {
  45. enabled: false,
  46. },
  47. },
  48. defaultRunInputData: {
  49. '#context#': [RETRIEVAL_OUTPUT_STRUCT],
  50. '#files#': [],
  51. },
  52. getAvailablePrevNodes(isChatMode: boolean) {
  53. const nodes = isChatMode
  54. ? ALL_CHAT_AVAILABLE_BLOCKS
  55. : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
  56. return nodes
  57. },
  58. getAvailableNextNodes(isChatMode: boolean) {
  59. const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
  60. return nodes
  61. },
  62. checkValid(payload: LLMNodeType, t: any) {
  63. let errorMessages = ''
  64. if (!errorMessages && !payload.model.provider)
  65. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.model`) })
  66. if (!errorMessages && !payload.memory) {
  67. const isChatModel = payload.model.mode === 'chat'
  68. const isPromptEmpty = isChatModel
  69. ? !(payload.prompt_template as PromptItem[]).some((t) => {
  70. if (t.edition_type === EditionType.jinja2)
  71. return t.jinja2_text !== ''
  72. return t.text !== ''
  73. })
  74. : ((payload.prompt_template as PromptItem).edition_type === EditionType.jinja2 ? (payload.prompt_template as PromptItem).jinja2_text === '' : (payload.prompt_template as PromptItem).text === '')
  75. if (isPromptEmpty)
  76. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.llm.prompt') })
  77. }
  78. if (!errorMessages && !!payload.memory) {
  79. const isChatModel = payload.model.mode === 'chat'
  80. // payload.memory.query_prompt_template not pass is default: {{#sys.query#}}
  81. if (isChatModel && !!payload.memory.query_prompt_template && !payload.memory.query_prompt_template.includes('{{#sys.query#}}'))
  82. errorMessages = t('workflow.nodes.llm.sysQueryInUser')
  83. }
  84. if (!errorMessages) {
  85. const isChatModel = payload.model.mode === 'chat'
  86. const isShowVars = (() => {
  87. if (isChatModel)
  88. return (payload.prompt_template as PromptItem[]).some(item => item.edition_type === EditionType.jinja2)
  89. return (payload.prompt_template as PromptItem).edition_type === EditionType.jinja2
  90. })()
  91. if (isShowVars && payload.prompt_config?.jinja2_variables) {
  92. payload.prompt_config?.jinja2_variables.forEach((i) => {
  93. if (!errorMessages && !i.variable)
  94. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variable`) })
  95. if (!errorMessages && !i.value_selector.length)
  96. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.variableValue`) })
  97. })
  98. }
  99. }
  100. if (!errorMessages && payload.vision?.enabled && !payload.vision.configs?.variable_selector?.length)
  101. errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t(`${i18nPrefix}.fields.visionVariable`) })
  102. return {
  103. isValid: !errorMessages,
  104. errorMessage: errorMessages,
  105. }
  106. },
  107. }
  108. export default nodeDefault