index.ts 7.2 KB

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