index.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 = 5
  113. export const DEFAULT_AGENT_SETTING = {
  114. enabled: false,
  115. max_iteration: 5,
  116. strategy: AgentStrategy.functionCall,
  117. tools: [],
  118. }
  119. export const supportFunctionCallModels = ['glm-3-turbo', 'glm-4']
  120. export const DEFAULT_AGENT_PROMPT = {
  121. chat: `Respond to the human as helpfully and accurately as possible.
  122. {{instruction}}
  123. You have access to the following tools:
  124. {{tools}}
  125. 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).
  126. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  127. Provide only ONE action per $JSON_BLOB, as shown:
  128. \`\`\`
  129. {
  130. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  131. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  132. }
  133. \`\`\`
  134. Follow this format:
  135. Question: input question to answer
  136. Thought: consider previous and subsequent steps
  137. Action:
  138. \`\`\`
  139. $JSON_BLOB
  140. \`\`\`
  141. Observation: action result
  142. ... (repeat Thought/Action/Observation N times)
  143. Thought: I know what to respond
  144. Action:
  145. \`\`\`
  146. {
  147. "{{TOOL_NAME_KEY}}": "Final Answer",
  148. "{{ACTION_INPUT_KEY}}": "Final response to human"
  149. }
  150. \`\`\`
  151. 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:.`,
  152. completion: `
  153. Respond to the human as helpfully and accurately as possible.
  154. {{instruction}}
  155. You have access to the following tools:
  156. {{tools}}
  157. 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).
  158. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  159. Provide only ONE action per $JSON_BLOB, as shown:
  160. \`\`\`
  161. {{{{
  162. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  163. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  164. }}}}
  165. \`\`\`
  166. Follow this format:
  167. Question: input question to answer
  168. Thought: consider previous and subsequent steps
  169. Action:
  170. \`\`\`
  171. $JSON_BLOB
  172. \`\`\`
  173. Observation: action result
  174. ... (repeat Thought/Action/Observation N times)
  175. Thought: I know what to respond
  176. Action:
  177. \`\`\`
  178. {{{{
  179. "{{TOOL_NAME_KEY}}": "Final Answer",
  180. "{{ACTION_INPUT_KEY}}": "Final response to human"
  181. }}}}
  182. \`\`\`
  183. 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:.
  184. Question: {{query}}
  185. Thought: {{agent_scratchpad}}
  186. `,
  187. }