index.ts 9.1 KB

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