index.ts 6.2 KB

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