Browse Source

feat: close #3300 support multiple api keys

Yidadaa 1 year ago
parent
commit
6aade62ce2
3 changed files with 11 additions and 3 deletions
  1. 1 1
      README.md
  2. 1 1
      README_CN.md
  3. 9 1
      app/config/server.ts

+ 1 - 1
README.md

@@ -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)
 

+ 1 - 1
README_CN.md

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

+ 9 - 1
app/config/server.ts

@@ -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,