Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.ts 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { InputVarType } from '@/app/components/workflow/types'
  2. import { AgentStrategy } from '@/types/app'
  3. import { PromptRole } from '@/models/debug'
  4. import { DatasetAttr } from '@/types/feature'
  5. const getBooleanConfig = (envVar: string | undefined, dataAttrKey: DatasetAttr, defaultValue: boolean = true) => {
  6. if (envVar !== undefined && envVar !== '')
  7. return envVar === 'true'
  8. const attrValue = globalThis.document?.body?.getAttribute(dataAttrKey)
  9. if (attrValue !== undefined && attrValue !== '')
  10. return attrValue === 'true'
  11. return defaultValue
  12. }
  13. const getNumberConfig = (envVar: string | undefined, dataAttrKey: DatasetAttr, defaultValue: number) => {
  14. if (envVar)
  15. return Number.parseInt(envVar)
  16. const attrValue = globalThis.document?.body?.getAttribute(dataAttrKey)
  17. if (attrValue)
  18. return Number.parseInt(attrValue)
  19. return defaultValue
  20. }
  21. const getStringConfig = (envVar: string | undefined, dataAttrKey: DatasetAttr, defaultValue: string) => {
  22. if (envVar)
  23. return envVar
  24. const attrValue = globalThis.document?.body?.getAttribute(dataAttrKey)
  25. if (attrValue)
  26. return attrValue
  27. return defaultValue
  28. }
  29. export const API_PREFIX = getStringConfig(process.env.NEXT_PUBLIC_API_PREFIX, DatasetAttr.DATA_API_PREFIX, 'http://localhost:5001/console/api')
  30. export const PUBLIC_API_PREFIX = getStringConfig(process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX, DatasetAttr.DATA_PUBLIC_API_PREFIX, 'http://localhost:5001/api')
  31. export const MARKETPLACE_API_PREFIX = getStringConfig(process.env.NEXT_PUBLIC_MARKETPLACE_API_PREFIX, DatasetAttr.DATA_MARKETPLACE_API_PREFIX, 'http://localhost:5002/api')
  32. export const MARKETPLACE_URL_PREFIX = getStringConfig(process.env.NEXT_PUBLIC_MARKETPLACE_URL_PREFIX, DatasetAttr.DATA_MARKETPLACE_URL_PREFIX, '')
  33. const EDITION = getStringConfig(process.env.NEXT_PUBLIC_EDITION, DatasetAttr.DATA_PUBLIC_EDITION, 'SELF_HOSTED')
  34. export const IS_CE_EDITION = EDITION === 'SELF_HOSTED'
  35. export const IS_CLOUD_EDITION = EDITION === 'CLOUD'
  36. export const SUPPORT_MAIL_LOGIN = !!(process.env.NEXT_PUBLIC_SUPPORT_MAIL_LOGIN || globalThis.document?.body?.getAttribute('data-public-support-mail-login'))
  37. export const TONE_LIST = [
  38. {
  39. id: 1,
  40. name: 'Creative',
  41. config: {
  42. temperature: 0.8,
  43. top_p: 0.9,
  44. presence_penalty: 0.1,
  45. frequency_penalty: 0.1,
  46. },
  47. },
  48. {
  49. id: 2,
  50. name: 'Balanced',
  51. config: {
  52. temperature: 0.5,
  53. top_p: 0.85,
  54. presence_penalty: 0.2,
  55. frequency_penalty: 0.3,
  56. },
  57. },
  58. {
  59. id: 3,
  60. name: 'Precise',
  61. config: {
  62. temperature: 0.2,
  63. top_p: 0.75,
  64. presence_penalty: 0.5,
  65. frequency_penalty: 0.5,
  66. },
  67. },
  68. {
  69. id: 4,
  70. name: 'Custom',
  71. },
  72. ]
  73. export const DEFAULT_CHAT_PROMPT_CONFIG = {
  74. prompt: [
  75. {
  76. role: PromptRole.system,
  77. text: '',
  78. },
  79. ],
  80. }
  81. export const DEFAULT_COMPLETION_PROMPT_CONFIG = {
  82. prompt: {
  83. text: '',
  84. },
  85. conversation_histories_role: {
  86. user_prefix: '',
  87. assistant_prefix: '',
  88. },
  89. }
  90. export const getMaxToken = (modelId: string) => {
  91. return (modelId === 'gpt-4' || modelId === 'gpt-3.5-turbo-16k') ? 8000 : 4000
  92. }
  93. export const LOCALE_COOKIE_NAME = 'locale'
  94. export const DEFAULT_VALUE_MAX_LEN = 48
  95. export const DEFAULT_PARAGRAPH_VALUE_MAX_LEN = 1000
  96. export const zhRegex = /^[\u4E00-\u9FA5]$/m
  97. export const emojiRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/m
  98. export const emailRegex = /^[\w.!#$%&'*+\-/=?^{|}~]+@([\w-]+\.)+[\w-]{2,}$/m
  99. const MAX_ZN_VAR_NAME_LENGTH = 8
  100. const MAX_EN_VAR_VALUE_LENGTH = 30
  101. export const getMaxVarNameLength = (value: string) => {
  102. if (zhRegex.test(value))
  103. return MAX_ZN_VAR_NAME_LENGTH
  104. return MAX_EN_VAR_VALUE_LENGTH
  105. }
  106. export const MAX_VAR_KEY_LENGTH = 30
  107. export const MAX_PROMPT_MESSAGE_LENGTH = 10
  108. export const VAR_ITEM_TEMPLATE = {
  109. key: '',
  110. name: '',
  111. type: 'string',
  112. max_length: DEFAULT_VALUE_MAX_LEN,
  113. required: true,
  114. }
  115. export const VAR_ITEM_TEMPLATE_IN_WORKFLOW = {
  116. variable: '',
  117. label: '',
  118. type: InputVarType.textInput,
  119. max_length: DEFAULT_VALUE_MAX_LEN,
  120. required: true,
  121. options: [],
  122. }
  123. export const appDefaultIconBackground = '#D5F5F6'
  124. export const NEED_REFRESH_APP_LIST_KEY = 'needRefreshAppList'
  125. export const DATASET_DEFAULT = {
  126. top_k: 4,
  127. score_threshold: 0.8,
  128. }
  129. export const APP_PAGE_LIMIT = 10
  130. export const ANNOTATION_DEFAULT = {
  131. score_threshold: 0.9,
  132. }
  133. export const DEFAULT_AGENT_SETTING = {
  134. enabled: false,
  135. max_iteration: 10,
  136. strategy: AgentStrategy.functionCall,
  137. tools: [],
  138. }
  139. export const DEFAULT_AGENT_PROMPT = {
  140. chat: `Respond to the human as helpfully and accurately as possible.
  141. {{instruction}}
  142. You have access to the following tools:
  143. {{tools}}
  144. Use a json blob to specify a tool by providing an {{TOOL_NAME_KEY}} key (tool name) and an {{ACTION_INPUT_KEY}} key (tool input).
  145. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  146. Provide only ONE action per $JSON_BLOB, as shown:
  147. \`\`\`
  148. {
  149. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  150. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  151. }
  152. \`\`\`
  153. Follow this format:
  154. Question: input question to answer
  155. Thought: consider previous and subsequent steps
  156. Action:
  157. \`\`\`
  158. $JSON_BLOB
  159. \`\`\`
  160. Observation: action result
  161. ... (repeat Thought/Action/Observation N times)
  162. Thought: I know what to respond
  163. Action:
  164. \`\`\`
  165. {
  166. "{{TOOL_NAME_KEY}}": "Final Answer",
  167. "{{ACTION_INPUT_KEY}}": "Final response to human"
  168. }
  169. \`\`\`
  170. Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:\`\`\`$JSON_BLOB\`\`\`then Observation:.`,
  171. completion: `
  172. Respond to the human as helpfully and accurately as possible.
  173. {{instruction}}
  174. You have access to the following tools:
  175. {{tools}}
  176. Use a json blob to specify a tool by providing an {{TOOL_NAME_KEY}} key (tool name) and an {{ACTION_INPUT_KEY}} key (tool input).
  177. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  178. Provide only ONE action per $JSON_BLOB, as shown:
  179. \`\`\`
  180. {{{{
  181. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  182. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  183. }}}}
  184. \`\`\`
  185. Follow this format:
  186. Question: input question to answer
  187. Thought: consider previous and subsequent steps
  188. Action:
  189. \`\`\`
  190. $JSON_BLOB
  191. \`\`\`
  192. Observation: action result
  193. ... (repeat Thought/Action/Observation N times)
  194. Thought: I know what to respond
  195. Action:
  196. \`\`\`
  197. {{{{
  198. "{{TOOL_NAME_KEY}}": "Final Answer",
  199. "{{ACTION_INPUT_KEY}}": "Final response to human"
  200. }}}}
  201. \`\`\`
  202. Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:\`\`\`$JSON_BLOB\`\`\`then Observation:.
  203. Question: {{query}}
  204. Thought: {{agent_scratchpad}}
  205. `,
  206. }
  207. export const VAR_REGEX = /\{\{(#[a-zA-Z0-9_-]{1,50}(\.[a-zA-Z_]\w{0,29}){1,10}#)\}\}/gi
  208. export const resetReg = () => VAR_REGEX.lastIndex = 0
  209. export const DISABLE_UPLOAD_IMAGE_AS_ICON = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true'
  210. export const GITHUB_ACCESS_TOKEN = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || ''
  211. export const SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS = '.difypkg,.difybndl'
  212. export const FULL_DOC_PREVIEW_LENGTH = 50
  213. export const JSON_SCHEMA_MAX_DEPTH = 10
  214. export const MAX_TOOLS_NUM = getNumberConfig(process.env.NEXT_PUBLIC_MAX_TOOLS_NUM, DatasetAttr.DATA_PUBLIC_MAX_TOOLS_NUM, 10)
  215. export const TEXT_GENERATION_TIMEOUT_MS = getNumberConfig(process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS, DatasetAttr.DATA_PUBLIC_TEXT_GENERATION_TIMEOUT_MS, 60000)
  216. export const LOOP_NODE_MAX_COUNT = getNumberConfig(process.env.NEXT_PUBLIC_LOOP_NODE_MAX_COUNT, DatasetAttr.DATA_PUBLIC_LOOP_NODE_MAX_COUNT, 100)
  217. export const MAX_ITERATIONS_NUM = getNumberConfig(process.env.NEXT_PUBLIC_MAX_ITERATIONS_NUM, DatasetAttr.DATA_PUBLIC_MAX_ITERATIONS_NUM, 99)
  218. export const MAX_TREE_DEPTH = getNumberConfig(process.env.NEXT_PUBLIC_MAX_TREE_DEPTH, DatasetAttr.DATA_PUBLIC_MAX_TREE_DEPTH, 50)
  219. export const ALLOW_UNSAFE_DATA_SCHEME = getBooleanConfig(process.env.NEXT_PUBLIC_ALLOW_UNSAFE_DATA_SCHEME, DatasetAttr.DATA_PUBLIC_ALLOW_UNSAFE_DATA_SCHEME, false)
  220. export const ENABLE_WEBSITE_JINAREADER = getBooleanConfig(process.env.NEXT_PUBLIC_ENABLE_WEBSITE_JINAREADER, DatasetAttr.DATA_PUBLIC_ENABLE_WEBSITE_JINAREADER, true)
  221. export const ENABLE_WEBSITE_FIRECRAWL = getBooleanConfig(process.env.NEXT_PUBLIC_ENABLE_WEBSITE_FIRECRAWL, DatasetAttr.DATA_PUBLIC_ENABLE_WEBSITE_FIRECRAWL, true)
  222. export const ENABLE_WEBSITE_WATERCRAWL = getBooleanConfig(process.env.NEXT_PUBLIC_ENABLE_WEBSITE_WATERCRAWL, DatasetAttr.DATA_PUBLIC_ENABLE_WEBSITE_WATERCRAWL, false)
  223. export const VALUE_SELECTOR_DELIMITER = '@@@'