Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { InputVarType } from '@/app/components/workflow/types'
  2. import { AgentStrategy } from '@/types/app'
  3. import { PromptRole } from '@/models/debug'
  4. import { PipelineInputVarType } from '@/models/pipeline'
  5. import { DatasetAttr } from '@/types/feature'
  6. import pkg from '../package.json'
  7. import type { ModelParameterRule } from '@/app/components/header/account-setting/model-provider-page/declarations'
  8. const getBooleanConfig = (
  9. envVar: string | undefined,
  10. dataAttrKey: DatasetAttr,
  11. defaultValue: boolean = true,
  12. ) => {
  13. if (envVar !== undefined && envVar !== '') return envVar === 'true'
  14. const attrValue = globalThis.document?.body?.getAttribute(dataAttrKey)
  15. if (attrValue !== undefined && attrValue !== '') return attrValue === 'true'
  16. return defaultValue
  17. }
  18. const getNumberConfig = (
  19. envVar: string | undefined,
  20. dataAttrKey: DatasetAttr,
  21. defaultValue: number,
  22. ) => {
  23. if (envVar) {
  24. const parsed = Number.parseInt(envVar)
  25. if (!Number.isNaN(parsed) && parsed > 0) return parsed
  26. }
  27. const attrValue = globalThis.document?.body?.getAttribute(dataAttrKey)
  28. if (attrValue) {
  29. const parsed = Number.parseInt(attrValue)
  30. if (!Number.isNaN(parsed) && parsed > 0) return parsed
  31. }
  32. return defaultValue
  33. }
  34. const getStringConfig = (
  35. envVar: string | undefined,
  36. dataAttrKey: DatasetAttr,
  37. defaultValue: string,
  38. ) => {
  39. if (envVar) return envVar
  40. const attrValue = globalThis.document?.body?.getAttribute(dataAttrKey)
  41. if (attrValue) return attrValue
  42. return defaultValue
  43. }
  44. export const API_PREFIX = getStringConfig(
  45. process.env.NEXT_PUBLIC_API_PREFIX,
  46. DatasetAttr.DATA_API_PREFIX,
  47. 'http://localhost:5001/console/api',
  48. )
  49. export const PUBLIC_API_PREFIX = getStringConfig(
  50. process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX,
  51. DatasetAttr.DATA_PUBLIC_API_PREFIX,
  52. 'http://localhost:5001/api',
  53. )
  54. export const MARKETPLACE_API_PREFIX = getStringConfig(
  55. process.env.NEXT_PUBLIC_MARKETPLACE_API_PREFIX,
  56. DatasetAttr.DATA_MARKETPLACE_API_PREFIX,
  57. 'http://localhost:5002/api',
  58. )
  59. export const MARKETPLACE_URL_PREFIX = getStringConfig(
  60. process.env.NEXT_PUBLIC_MARKETPLACE_URL_PREFIX,
  61. DatasetAttr.DATA_MARKETPLACE_URL_PREFIX,
  62. '',
  63. )
  64. const EDITION = getStringConfig(
  65. process.env.NEXT_PUBLIC_EDITION,
  66. DatasetAttr.DATA_PUBLIC_EDITION,
  67. 'SELF_HOSTED',
  68. )
  69. export const IS_CE_EDITION = EDITION === 'SELF_HOSTED'
  70. export const IS_CLOUD_EDITION = EDITION === 'CLOUD'
  71. export const SUPPORT_MAIL_LOGIN = !!(
  72. process.env.NEXT_PUBLIC_SUPPORT_MAIL_LOGIN
  73. || globalThis.document?.body?.getAttribute('data-public-support-mail-login')
  74. )
  75. export const TONE_LIST = [
  76. {
  77. id: 1,
  78. name: 'Creative',
  79. config: {
  80. temperature: 0.8,
  81. top_p: 0.9,
  82. presence_penalty: 0.1,
  83. frequency_penalty: 0.1,
  84. },
  85. },
  86. {
  87. id: 2,
  88. name: 'Balanced',
  89. config: {
  90. temperature: 0.5,
  91. top_p: 0.85,
  92. presence_penalty: 0.2,
  93. frequency_penalty: 0.3,
  94. },
  95. },
  96. {
  97. id: 3,
  98. name: 'Precise',
  99. config: {
  100. temperature: 0.2,
  101. top_p: 0.75,
  102. presence_penalty: 0.5,
  103. frequency_penalty: 0.5,
  104. },
  105. },
  106. {
  107. id: 4,
  108. name: 'Custom',
  109. },
  110. ]
  111. export const DEFAULT_CHAT_PROMPT_CONFIG = {
  112. prompt: [
  113. {
  114. role: PromptRole.system,
  115. text: '',
  116. },
  117. ],
  118. }
  119. export const DEFAULT_COMPLETION_PROMPT_CONFIG = {
  120. prompt: {
  121. text: '',
  122. },
  123. conversation_histories_role: {
  124. user_prefix: '',
  125. assistant_prefix: '',
  126. },
  127. }
  128. export const getMaxToken = (modelId: string) => {
  129. return modelId === 'gpt-4' || modelId === 'gpt-3.5-turbo-16k' ? 8000 : 4000
  130. }
  131. export const LOCALE_COOKIE_NAME = 'locale'
  132. export const DEFAULT_VALUE_MAX_LEN = 48
  133. export const DEFAULT_PARAGRAPH_VALUE_MAX_LEN = 1000
  134. export const zhRegex = /^[\u4E00-\u9FA5]$/m
  135. export const emojiRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/m
  136. export const emailRegex = /^[\w.!#$%&'*+\-/=?^{|}~]+@([\w-]+\.)+[\w-]{2,}$/m
  137. const MAX_ZN_VAR_NAME_LENGTH = 8
  138. const MAX_EN_VAR_VALUE_LENGTH = 30
  139. export const getMaxVarNameLength = (value: string) => {
  140. if (zhRegex.test(value)) return MAX_ZN_VAR_NAME_LENGTH
  141. return MAX_EN_VAR_VALUE_LENGTH
  142. }
  143. export const MAX_VAR_KEY_LENGTH = 30
  144. export const MAX_PROMPT_MESSAGE_LENGTH = 10
  145. export const VAR_ITEM_TEMPLATE = {
  146. key: '',
  147. name: '',
  148. type: 'string',
  149. max_length: DEFAULT_VALUE_MAX_LEN,
  150. required: true,
  151. }
  152. export const VAR_ITEM_TEMPLATE_IN_WORKFLOW = {
  153. variable: '',
  154. label: '',
  155. type: InputVarType.textInput,
  156. max_length: DEFAULT_VALUE_MAX_LEN,
  157. required: true,
  158. options: [],
  159. }
  160. export const VAR_ITEM_TEMPLATE_IN_PIPELINE = {
  161. variable: '',
  162. label: '',
  163. type: PipelineInputVarType.textInput,
  164. max_length: DEFAULT_VALUE_MAX_LEN,
  165. required: true,
  166. options: [],
  167. }
  168. export const appDefaultIconBackground = '#D5F5F6'
  169. export const NEED_REFRESH_APP_LIST_KEY = 'needRefreshAppList'
  170. export const DATASET_DEFAULT = {
  171. top_k: 4,
  172. score_threshold: 0.8,
  173. }
  174. export const APP_PAGE_LIMIT = 10
  175. export const ANNOTATION_DEFAULT = {
  176. score_threshold: 0.9,
  177. }
  178. export const DEFAULT_AGENT_SETTING = {
  179. enabled: false,
  180. max_iteration: 10,
  181. strategy: AgentStrategy.functionCall,
  182. tools: [],
  183. }
  184. export const DEFAULT_AGENT_PROMPT = {
  185. chat: `Respond to the human as helpfully and accurately as possible.
  186. {{instruction}}
  187. You have access to the following tools:
  188. {{tools}}
  189. 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).
  190. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  191. Provide only ONE action per $JSON_BLOB, as shown:
  192. \`\`\`
  193. {
  194. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  195. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  196. }
  197. \`\`\`
  198. Follow this format:
  199. Question: input question to answer
  200. Thought: consider previous and subsequent steps
  201. Action:
  202. \`\`\`
  203. $JSON_BLOB
  204. \`\`\`
  205. Observation: action result
  206. ... (repeat Thought/Action/Observation N times)
  207. Thought: I know what to respond
  208. Action:
  209. \`\`\`
  210. {
  211. "{{TOOL_NAME_KEY}}": "Final Answer",
  212. "{{ACTION_INPUT_KEY}}": "Final response to human"
  213. }
  214. \`\`\`
  215. 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:.`,
  216. completion: `
  217. Respond to the human as helpfully and accurately as possible.
  218. {{instruction}}
  219. You have access to the following tools:
  220. {{tools}}
  221. 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).
  222. Valid "{{TOOL_NAME_KEY}}" values: "Final Answer" or {{tool_names}}
  223. Provide only ONE action per $JSON_BLOB, as shown:
  224. \`\`\`
  225. {{{{
  226. "{{TOOL_NAME_KEY}}": $TOOL_NAME,
  227. "{{ACTION_INPUT_KEY}}": $ACTION_INPUT
  228. }}}}
  229. \`\`\`
  230. Follow this format:
  231. Question: input question to answer
  232. Thought: consider previous and subsequent steps
  233. Action:
  234. \`\`\`
  235. $JSON_BLOB
  236. \`\`\`
  237. Observation: action result
  238. ... (repeat Thought/Action/Observation N times)
  239. Thought: I know what to respond
  240. Action:
  241. \`\`\`
  242. {{{{
  243. "{{TOOL_NAME_KEY}}": "Final Answer",
  244. "{{ACTION_INPUT_KEY}}": "Final response to human"
  245. }}}}
  246. \`\`\`
  247. 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:.
  248. Question: {{query}}
  249. Thought: {{agent_scratchpad}}
  250. `,
  251. }
  252. export const VAR_REGEX
  253. = /\{\{(#[a-zA-Z0-9_-]{1,50}(\.\d+)?(\.[a-zA-Z_]\w{0,29}){1,10}#)\}\}/gi
  254. export const resetReg = () => (VAR_REGEX.lastIndex = 0)
  255. export const DISABLE_UPLOAD_IMAGE_AS_ICON
  256. = process.env.NEXT_PUBLIC_DISABLE_UPLOAD_IMAGE_AS_ICON === 'true'
  257. export const GITHUB_ACCESS_TOKEN
  258. = process.env.NEXT_PUBLIC_GITHUB_ACCESS_TOKEN || ''
  259. export const SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS = '.difypkg,.difybndl'
  260. export const FULL_DOC_PREVIEW_LENGTH = 50
  261. export const JSON_SCHEMA_MAX_DEPTH = 10
  262. export const MAX_TOOLS_NUM = getNumberConfig(
  263. process.env.NEXT_PUBLIC_MAX_TOOLS_NUM,
  264. DatasetAttr.DATA_PUBLIC_MAX_TOOLS_NUM,
  265. 10,
  266. )
  267. export const MAX_PARALLEL_LIMIT = getNumberConfig(
  268. process.env.NEXT_PUBLIC_MAX_PARALLEL_LIMIT,
  269. DatasetAttr.DATA_PUBLIC_MAX_PARALLEL_LIMIT,
  270. 10,
  271. )
  272. export const TEXT_GENERATION_TIMEOUT_MS = getNumberConfig(
  273. process.env.NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS,
  274. DatasetAttr.DATA_PUBLIC_TEXT_GENERATION_TIMEOUT_MS,
  275. 60000,
  276. )
  277. export const LOOP_NODE_MAX_COUNT = getNumberConfig(
  278. process.env.NEXT_PUBLIC_LOOP_NODE_MAX_COUNT,
  279. DatasetAttr.DATA_PUBLIC_LOOP_NODE_MAX_COUNT,
  280. 100,
  281. )
  282. export const MAX_ITERATIONS_NUM = getNumberConfig(
  283. process.env.NEXT_PUBLIC_MAX_ITERATIONS_NUM,
  284. DatasetAttr.DATA_PUBLIC_MAX_ITERATIONS_NUM,
  285. 99,
  286. )
  287. export const MAX_TREE_DEPTH = getNumberConfig(
  288. process.env.NEXT_PUBLIC_MAX_TREE_DEPTH,
  289. DatasetAttr.DATA_PUBLIC_MAX_TREE_DEPTH,
  290. 50,
  291. )
  292. export const ALLOW_UNSAFE_DATA_SCHEME = getBooleanConfig(
  293. process.env.NEXT_PUBLIC_ALLOW_UNSAFE_DATA_SCHEME,
  294. DatasetAttr.DATA_PUBLIC_ALLOW_UNSAFE_DATA_SCHEME,
  295. false,
  296. )
  297. export const ENABLE_WEBSITE_JINAREADER = getBooleanConfig(
  298. process.env.NEXT_PUBLIC_ENABLE_WEBSITE_JINAREADER,
  299. DatasetAttr.DATA_PUBLIC_ENABLE_WEBSITE_JINAREADER,
  300. true,
  301. )
  302. export const ENABLE_WEBSITE_FIRECRAWL = getBooleanConfig(
  303. process.env.NEXT_PUBLIC_ENABLE_WEBSITE_FIRECRAWL,
  304. DatasetAttr.DATA_PUBLIC_ENABLE_WEBSITE_FIRECRAWL,
  305. true,
  306. )
  307. export const ENABLE_WEBSITE_WATERCRAWL = getBooleanConfig(
  308. process.env.NEXT_PUBLIC_ENABLE_WEBSITE_WATERCRAWL,
  309. DatasetAttr.DATA_PUBLIC_ENABLE_WEBSITE_WATERCRAWL,
  310. false,
  311. )
  312. export const VALUE_SELECTOR_DELIMITER = '@@@'
  313. export const validPassword = /^(?=.*[a-zA-Z])(?=.*\d)\S{8,}$/
  314. export const ZENDESK_WIDGET_KEY = getStringConfig(
  315. process.env.NEXT_PUBLIC_ZENDESK_WIDGET_KEY,
  316. DatasetAttr.NEXT_PUBLIC_ZENDESK_WIDGET_KEY,
  317. '',
  318. )
  319. export const ZENDESK_FIELD_IDS = {
  320. ENVIRONMENT: getStringConfig(
  321. process.env.NEXT_PUBLIC_ZENDESK_FIELD_ID_ENVIRONMENT,
  322. DatasetAttr.NEXT_PUBLIC_ZENDESK_FIELD_ID_ENVIRONMENT,
  323. '',
  324. ),
  325. VERSION: getStringConfig(
  326. process.env.NEXT_PUBLIC_ZENDESK_FIELD_ID_VERSION,
  327. DatasetAttr.NEXT_PUBLIC_ZENDESK_FIELD_ID_VERSION,
  328. '',
  329. ),
  330. EMAIL: getStringConfig(
  331. process.env.NEXT_PUBLIC_ZENDESK_FIELD_ID_EMAIL,
  332. DatasetAttr.NEXT_PUBLIC_ZENDESK_FIELD_ID_EMAIL,
  333. '',
  334. ),
  335. WORKSPACE_ID: getStringConfig(
  336. process.env.NEXT_PUBLIC_ZENDESK_FIELD_ID_WORKSPACE_ID,
  337. DatasetAttr.NEXT_PUBLIC_ZENDESK_FIELD_ID_WORKSPACE_ID,
  338. '',
  339. ),
  340. PLAN: getStringConfig(
  341. process.env.NEXT_PUBLIC_ZENDESK_FIELD_ID_PLAN,
  342. DatasetAttr.NEXT_PUBLIC_ZENDESK_FIELD_ID_PLAN,
  343. '',
  344. ),
  345. }
  346. export const APP_VERSION = pkg.version
  347. export const RAG_PIPELINE_PREVIEW_CHUNK_NUM = 20
  348. export const PROVIDER_WITH_PRESET_TONE = ['langgenius/openai/openai', 'langgenius/azure_openai/azure_openai']
  349. export const STOP_PARAMETER_RULE: ModelParameterRule = {
  350. default: [],
  351. help: {
  352. en_US: 'Up to four sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.',
  353. zh_Hans: '最多四个序列,API 将停止生成更多的 token。返回的文本将不包含停止序列。',
  354. },
  355. label: {
  356. en_US: 'Stop sequences',
  357. zh_Hans: '停止序列',
  358. },
  359. name: 'stop',
  360. required: false,
  361. type: 'tag',
  362. tagPlaceholder: {
  363. en_US: 'Enter sequence and press Tab',
  364. zh_Hans: '输入序列并按 Tab 键',
  365. },
  366. }