server.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import md5 from "spark-md5";
  2. import { DEFAULT_MODELS } from "../constant";
  3. declare global {
  4. namespace NodeJS {
  5. interface ProcessEnv {
  6. PROXY_URL?: string; // docker only
  7. OPENAI_API_KEY?: string;
  8. CODE?: string;
  9. BASE_URL?: string;
  10. OPENAI_ORG_ID?: string; // openai only
  11. VERCEL?: string;
  12. BUILD_MODE?: "standalone" | "export";
  13. BUILD_APP?: string; // is building desktop app
  14. HIDE_USER_API_KEY?: string; // disable user's api key input
  15. DISABLE_GPT4?: string; // allow user to use gpt-4 or not
  16. ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not
  17. DISABLE_FAST_LINK?: string; // disallow parse settings from url or not
  18. CUSTOM_MODELS?: string; // to control custom models
  19. DEFAULT_MODEL?: string; // to cnntrol default model in every new chat window
  20. // azure only
  21. AZURE_URL?: string; // https://{azure-url}/openai/deployments/{deploy-name}
  22. AZURE_API_KEY?: string;
  23. AZURE_API_VERSION?: string;
  24. // google only
  25. GOOGLE_API_KEY?: string;
  26. GOOGLE_URL?: string;
  27. // google tag manager
  28. GTM_ID?: string;
  29. // anthropic only
  30. ANTHROPIC_URL?: string;
  31. ANTHROPIC_API_KEY?: string;
  32. ANTHROPIC_API_VERSION?: string;
  33. // baidu only
  34. BAIDU_URL?: string;
  35. BAIDU_API_KEY?: string;
  36. BAIDU_SECRET_KEY?: string;
  37. // custom template for preprocessing user input
  38. DEFAULT_INPUT_TEMPLATE?: string;
  39. }
  40. }
  41. }
  42. const ACCESS_CODES = (function getAccessCodes(): Set<string> {
  43. const code = process.env.CODE;
  44. try {
  45. const codes = (code?.split(",") ?? [])
  46. .filter((v) => !!v)
  47. .map((v) => md5.hash(v.trim()));
  48. return new Set(codes);
  49. } catch (e) {
  50. return new Set();
  51. }
  52. })();
  53. function getApiKey(keys?: string) {
  54. const apiKeyEnvVar = keys ?? "";
  55. const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
  56. const randomIndex = Math.floor(Math.random() * apiKeys.length);
  57. const apiKey = apiKeys[randomIndex];
  58. if (apiKey) {
  59. console.log(
  60. `[Server Config] using ${randomIndex + 1} of ${
  61. apiKeys.length
  62. } api key - ${apiKey}`,
  63. );
  64. }
  65. return apiKey;
  66. }
  67. export const getServerSideConfig = () => {
  68. if (typeof process === "undefined") {
  69. throw Error(
  70. "[Server Config] you are importing a nodejs-only module outside of nodejs",
  71. );
  72. }
  73. const disableGPT4 = !!process.env.DISABLE_GPT4;
  74. let customModels = process.env.CUSTOM_MODELS ?? "";
  75. let defaultModel = process.env.DEFAULT_MODEL ?? "";
  76. if (disableGPT4) {
  77. if (customModels) customModels += ",";
  78. customModels += DEFAULT_MODELS.filter((m) => m.name.startsWith("gpt-4"))
  79. .map((m) => "-" + m.name)
  80. .join(",");
  81. if (defaultModel.startsWith("gpt-4")) defaultModel = "";
  82. }
  83. const isAzure = !!process.env.AZURE_URL;
  84. const isGoogle = !!process.env.GOOGLE_API_KEY;
  85. const isAnthropic = !!process.env.ANTHROPIC_API_KEY;
  86. const isBaidu = !!process.env.BAIDU_API_KEY;
  87. // const apiKeyEnvVar = process.env.OPENAI_API_KEY ?? "";
  88. // const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
  89. // const randomIndex = Math.floor(Math.random() * apiKeys.length);
  90. // const apiKey = apiKeys[randomIndex];
  91. // console.log(
  92. // `[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`,
  93. // );
  94. const allowedWebDevEndpoints = (
  95. process.env.WHITE_WEBDEV_ENDPOINTS ?? ""
  96. ).split(",");
  97. return {
  98. baseUrl: process.env.BASE_URL,
  99. apiKey: getApiKey(process.env.OPENAI_API_KEY),
  100. openaiOrgId: process.env.OPENAI_ORG_ID,
  101. isAzure,
  102. azureUrl: process.env.AZURE_URL,
  103. azureApiKey: getApiKey(process.env.AZURE_API_KEY),
  104. azureApiVersion: process.env.AZURE_API_VERSION,
  105. isGoogle,
  106. googleApiKey: getApiKey(process.env.GOOGLE_API_KEY),
  107. googleUrl: process.env.GOOGLE_URL,
  108. isAnthropic,
  109. anthropicApiKey: getApiKey(process.env.ANTHROPIC_API_KEY),
  110. anthropicApiVersion: process.env.ANTHROPIC_API_VERSION,
  111. anthropicUrl: process.env.ANTHROPIC_URL,
  112. isBaidu,
  113. baiduUrl: process.env.BAIDU_URL,
  114. baiduApiKey: getApiKey(process.env.BAIDU_API_KEY),
  115. baiduSecretKey: process.env.BAIDU_SECRET_KEY,
  116. gtmId: process.env.GTM_ID,
  117. needCode: ACCESS_CODES.size > 0,
  118. code: process.env.CODE,
  119. codes: ACCESS_CODES,
  120. proxyUrl: process.env.PROXY_URL,
  121. isVercel: !!process.env.VERCEL,
  122. hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
  123. disableGPT4,
  124. hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
  125. disableFastLink: !!process.env.DISABLE_FAST_LINK,
  126. customModels,
  127. defaultModel,
  128. allowedWebDevEndpoints,
  129. };
  130. };