server.ts 4.0 KB

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