server.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // tencent only
  30. TENCENT_URL?: string;
  31. TENCENT_SECRET_KEY?: string;
  32. TENCENT_SECRET_ID?: string;
  33. // custom template for preprocessing user input
  34. DEFAULT_INPUT_TEMPLATE?: string;
  35. }
  36. }
  37. }
  38. const ACCESS_CODES = (function getAccessCodes(): Set<string> {
  39. const code = process.env.CODE;
  40. try {
  41. const codes = (code?.split(",") ?? [])
  42. .filter((v) => !!v)
  43. .map((v) => md5.hash(v.trim()));
  44. return new Set(codes);
  45. } catch (e) {
  46. return new Set();
  47. }
  48. })();
  49. function getApiKey(keys?: string) {
  50. const apiKeyEnvVar = keys ?? "";
  51. const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
  52. const randomIndex = Math.floor(Math.random() * apiKeys.length);
  53. const apiKey = apiKeys[randomIndex];
  54. if (apiKey) {
  55. console.log(
  56. `[Server Config] using ${randomIndex + 1} of ${
  57. apiKeys.length
  58. } api key - ${apiKey}`,
  59. );
  60. }
  61. return apiKey;
  62. }
  63. export const getServerSideConfig = () => {
  64. if (typeof process === "undefined") {
  65. throw Error(
  66. "[Server Config] you are importing a nodejs-only module outside of nodejs",
  67. );
  68. }
  69. const disableGPT4 = !!process.env.DISABLE_GPT4;
  70. let customModels = process.env.CUSTOM_MODELS ?? "";
  71. let defaultModel = process.env.DEFAULT_MODEL ?? "";
  72. if (disableGPT4) {
  73. if (customModels) customModels += ",";
  74. customModels += DEFAULT_MODELS.filter((m) => m.name.startsWith("gpt-4"))
  75. .map((m) => "-" + m.name)
  76. .join(",");
  77. if (defaultModel.startsWith("gpt-4")) defaultModel = "";
  78. }
  79. const isAzure = !!process.env.AZURE_URL;
  80. const isGoogle = !!process.env.GOOGLE_API_KEY;
  81. const isAnthropic = !!process.env.ANTHROPIC_API_KEY;
  82. const isTencent = !!process.env.TENCENT_API_KEY;
  83. // const apiKeyEnvVar = process.env.OPENAI_API_KEY ?? "";
  84. // const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
  85. // const randomIndex = Math.floor(Math.random() * apiKeys.length);
  86. // const apiKey = apiKeys[randomIndex];
  87. // console.log(
  88. // `[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`,
  89. // );
  90. const allowedWebDevEndpoints = (
  91. process.env.WHITE_WEBDEV_ENDPOINTS ?? ""
  92. ).split(",");
  93. return {
  94. baseUrl: process.env.BASE_URL,
  95. apiKey: getApiKey(process.env.OPENAI_API_KEY),
  96. openaiOrgId: process.env.OPENAI_ORG_ID,
  97. isAzure,
  98. azureUrl: process.env.AZURE_URL,
  99. azureApiKey: getApiKey(process.env.AZURE_API_KEY),
  100. azureApiVersion: process.env.AZURE_API_VERSION,
  101. isGoogle,
  102. googleApiKey: getApiKey(process.env.GOOGLE_API_KEY),
  103. googleUrl: process.env.GOOGLE_URL,
  104. isAnthropic,
  105. anthropicApiKey: getApiKey(process.env.ANTHROPIC_API_KEY),
  106. anthropicApiVersion: process.env.ANTHROPIC_API_VERSION,
  107. anthropicUrl: process.env.ANTHROPIC_URL,
  108. isTencent,
  109. tencentUrl: process.env.TENCENT_URL,
  110. tencentSecretKey: getApiKey(process.env.TENCENT_SECRET_KEY),
  111. tencentSecretId: process.env.TENCENT_SECRET_ID,
  112. gtmId: process.env.GTM_ID,
  113. needCode: ACCESS_CODES.size > 0,
  114. code: process.env.CODE,
  115. codes: ACCESS_CODES,
  116. proxyUrl: process.env.PROXY_URL,
  117. isVercel: !!process.env.VERCEL,
  118. hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
  119. disableGPT4,
  120. hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
  121. disableFastLink: !!process.env.DISABLE_FAST_LINK,
  122. customModels,
  123. defaultModel,
  124. allowedWebDevEndpoints,
  125. };
  126. };