index.ts 7.0 KB

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