You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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