Skip to content

Commit

Permalink
feat: close ChatGPTNextWeb#3300 support multiple api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa authored and kaiwu-astro committed Nov 20, 2023
1 parent c092b64 commit 2ad5ec2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -161,7 +161,7 @@ Access password, separated by comma.

### `OPENAI_API_KEY` (required)

Your openai api key.
Your openai api key, join multiple api keys with comma.

### `BASE_URL` (optional)

Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Expand Up @@ -68,7 +68,7 @@ code1,code2,code3
### `OPENAI_API_KEY` (必填项)

OpanAI 密钥,你在 openai 账户页面申请的 api key。
OpanAI 密钥,你在 openai 账户页面申请的 api key,使用英文逗号隔开多个 key,这样可以随机轮询这些 key

### `CODE` (可选)

Expand Down
10 changes: 9 additions & 1 deletion app/config/server.ts
Expand Up @@ -62,9 +62,17 @@ export const getServerSideConfig = () => {

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 {
baseUrl: process.env.BASE_URL,
apiKey: process.env.OPENAI_API_KEY,
apiKey,
openaiOrgId: process.env.OPENAI_ORG_ID,

isAzure,
Expand Down

0 comments on commit 2ad5ec2

Please sign in to comment.