index.ts 6.3 KB

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