server.ts 4.1 KB

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