feat: close #3300 support multiple api keys

This commit is contained in:
Yidadaa 2023-11-19 18:42:30 +08:00
parent 45b88ebb2a
commit 6aade62ce2
3 changed files with 11 additions and 3 deletions

View File

@ -161,7 +161,7 @@ Access password, separated by comma.
### `OPENAI_API_KEY` (required) ### `OPENAI_API_KEY` (required)
Your openai api key. Your openai api key, join multiple api keys with comma.
### `BASE_URL` (optional) ### `BASE_URL` (optional)

View File

@ -68,7 +68,7 @@ code1,code2,code3
### `OPENAI_API_KEY` (必填项) ### `OPENAI_API_KEY` (必填项)
OpanAI 密钥,你在 openai 账户页面申请的 api key。 OpanAI 密钥,你在 openai 账户页面申请的 api key,使用英文逗号隔开多个 key这样可以随机轮询这些 key
### `CODE` (可选) ### `CODE` (可选)

View File

@ -62,9 +62,17 @@ export const getServerSideConfig = () => {
const isAzure = !!process.env.AZURE_URL; 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`,
);
return { return {
baseUrl: process.env.BASE_URL, baseUrl: process.env.BASE_URL,
apiKey: process.env.OPENAI_API_KEY, apiKey,
openaiOrgId: process.env.OPENAI_ORG_ID, openaiOrgId: process.env.OPENAI_ORG_ID,
isAzure, isAzure,