ChatGPT-Next-Web/app/config/server.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-04-10 17:21:34 +00:00
import md5 from "spark-md5";
import { DEFAULT_MODELS } from "../constant";
2023-04-10 17:21:34 +00:00
declare global {
namespace NodeJS {
interface ProcessEnv {
2023-11-09 18:43:30 +00:00
PROXY_URL?: string; // docker only
2023-04-10 17:21:34 +00:00
OPENAI_API_KEY?: string;
CODE?: string;
2023-11-09 18:43:30 +00:00
2023-05-19 15:53:27 +00:00
BASE_URL?: string;
2023-11-09 18:43:30 +00:00
OPENAI_ORG_ID?: string; // openai only
2023-04-10 17:21:34 +00:00
VERCEL?: string;
BUILD_MODE?: "standalone" | "export";
BUILD_APP?: string; // is building desktop app
2023-11-09 18:43:30 +00:00
HIDE_USER_API_KEY?: string; // disable user's api key input
DISABLE_GPT4?: string; // allow user to use gpt-4 or not
2023-11-07 16:20:34 +00:00
ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not
DISABLE_FAST_LINK?: string; // disallow parse settings from url or not
CUSTOM_MODELS?: string; // to control custom models
2023-11-09 18:43:30 +00:00
// azure only
AZURE_URL?: string; // https://{azure-url}/openai/deployments/{deploy-name}
AZURE_API_KEY?: string;
AZURE_API_VERSION?: string;
2023-04-10 17:21:34 +00:00
}
}
}
const ACCESS_CODES = (function getAccessCodes(): Set<string> {
const code = process.env.CODE;
try {
const codes = (code?.split(",") ?? [])
.filter((v) => !!v)
.map((v) => md5.hash(v.trim()));
return new Set(codes);
} catch (e) {
return new Set();
}
})();
export const getServerSideConfig = () => {
if (typeof process === "undefined") {
throw Error(
"[Server Config] you are importing a nodejs-only module outside of nodejs",
);
}
2023-11-09 18:43:30 +00:00
const disableGPT4 = !!process.env.DISABLE_GPT4;
let customModels = process.env.CUSTOM_MODELS ?? "";
if (disableGPT4) {
if (customModels) customModels += ",";
customModels += DEFAULT_MODELS.filter((m) => m.name.startsWith("gpt-4"))
.map((m) => "-" + m.name)
.join(",");
}
2023-11-09 18:43:30 +00:00
const isAzure = !!process.env.AZURE_URL;
const apiKeyEnvVar = process.env.OPENAI_API_KEY ?? "";
const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
const randomIndex = Math.floor(Math.random() * apiKeys.length);
const apiKey = apiKeys[randomIndex];
console.log(
`[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`,
);
2023-04-10 17:21:34 +00:00
return {
2023-11-09 18:43:30 +00:00
baseUrl: process.env.BASE_URL,
apiKey,
2023-11-09 18:43:30 +00:00
openaiOrgId: process.env.OPENAI_ORG_ID,
isAzure,
azureUrl: process.env.AZURE_URL,
azureApiKey: process.env.AZURE_API_KEY,
azureApiVersion: process.env.AZURE_API_VERSION,
needCode: ACCESS_CODES.size > 0,
2023-04-10 17:21:34 +00:00
code: process.env.CODE,
codes: ACCESS_CODES,
2023-11-09 18:43:30 +00:00
2023-04-10 17:21:34 +00:00
proxyUrl: process.env.PROXY_URL,
isVercel: !!process.env.VERCEL,
2023-11-09 18:43:30 +00:00
hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
disableGPT4,
2023-11-07 16:20:34 +00:00
hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
disableFastLink: !!process.env.DISABLE_FAST_LINK,
customModels,
2023-04-10 17:21:34 +00:00
};
};