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

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