server.ts 4.3 KB

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