index.ts 5.9 KB

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