index.ts 7.3 KB

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