Zhenwei commited on
Commit
e4df10e
·
unverified ·
1 Parent(s): 42d8138

增加多账号机制 (#513)

Browse files
ChuanhuChatbot.py CHANGED
@@ -76,7 +76,10 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
76
  visible=not HIDE_MY_KEY,
77
  label="API-Key",
78
  )
79
- usageTxt = gr.Markdown("**发送消息** 或 **提交key** 以显示额度", elem_id="usage_display")
 
 
 
80
  model_select_dropdown = gr.Dropdown(
81
  label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
82
  )
 
76
  visible=not HIDE_MY_KEY,
77
  label="API-Key",
78
  )
79
+ if multi_api_key:
80
+ usageTxt = gr.Markdown("多账号模式已开启,无需输入key,可直接开始对话", elem_id="usage_display")
81
+ else:
82
+ usageTxt = gr.Markdown("**发送消息** 或 **提交key** 以显示额度", elem_id="usage_display")
83
  model_select_dropdown = gr.Dropdown(
84
  label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
85
  )
config_example.json CHANGED
@@ -8,5 +8,11 @@
8
  "two_column": false,
9
  "formula_ocr": true
10
  }
11
- }
 
 
 
 
 
 
12
  }
 
8
  "two_column": false,
9
  "formula_ocr": true
10
  }
11
+ },
12
+ "multi_api_key": false,
13
+ "api_key_list": [
14
+ "sk-xxxxxxxxxxxxxxxxxxxxxxxx1",
15
+ "sk-xxxxxxxxxxxxxxxxxxxxxxxx2",
16
+ "sk-xxxxxxxxxxxxxxxxxxxxxxxx3"
17
+ ]
18
  }
modules/chat_func.py CHANGED
@@ -35,6 +35,7 @@ initial_prompt = "You are a helpful assistant."
35
  HISTORY_DIR = "history"
36
  TEMPLATES_DIR = "templates"
37
 
 
38
  def get_response(
39
  openai_api_key, system_prompt, history, temperature, top_p, stream, selected_model
40
  ):
@@ -330,7 +331,7 @@ def predict(
330
  else:
331
  display_reference = ""
332
 
333
- if len(openai_api_key) != 51:
334
  status_text = standard_error_msg + no_apikey_msg
335
  logging.info(status_text)
336
  chatbot.append((inputs, ""))
 
35
  HISTORY_DIR = "history"
36
  TEMPLATES_DIR = "templates"
37
 
38
+ @shared.state.switching_api_key # 在不开启多账号模式的时候,这个装饰器不会起作用
39
  def get_response(
40
  openai_api_key, system_prompt, history, temperature, top_p, stream, selected_model
41
  ):
 
331
  else:
332
  display_reference = ""
333
 
334
+ if len(openai_api_key) != 51 and not shared.state.multi_api_key:
335
  status_text = standard_error_msg + no_apikey_msg
336
  logging.info(status_text)
337
  chatbot.append((inputs, ""))
modules/config.py CHANGED
@@ -16,7 +16,8 @@ __all__ = [
16
  "retrieve_proxy",
17
  "log_level",
18
  "advance_docs",
19
- "update_doc_config"
 
20
  ]
21
 
22
  # 添加一个统一的config文件,避免文件过多造成的疑惑(优先级最低)
@@ -36,6 +37,15 @@ if os.environ.get("dockerrun") == "yes":
36
  my_api_key = config.get("openai_api_key", "") # 在这里输入你的 API 密钥
37
  my_api_key = os.environ.get("my_api_key", my_api_key)
38
 
 
 
 
 
 
 
 
 
 
39
  auth_list = config.get("users", []) # 实际上是使用者的列表
40
  authflag = len(auth_list) > 0 # 是否开启认证的状态值,改为判断auth_list长度
41
 
 
16
  "retrieve_proxy",
17
  "log_level",
18
  "advance_docs",
19
+ "update_doc_config",
20
+ "multi_api_key",
21
  ]
22
 
23
  # 添加一个统一的config文件,避免文件过多造成的疑惑(优先级最低)
 
37
  my_api_key = config.get("openai_api_key", "") # 在这里输入你的 API 密钥
38
  my_api_key = os.environ.get("my_api_key", my_api_key)
39
 
40
+ ## 多账户机制
41
+ multi_api_key = config.get("multi_api_key", False) # 是否开启多账户机制
42
+ if multi_api_key:
43
+ api_key_list = config.get("api_key_list", [])
44
+ if len(api_key_list) == 0:
45
+ logging.error("多账号模式已开启,但api_key_list为空,请检查config.json")
46
+ sys.exit(1)
47
+ shared.state.set_api_key_queue(api_key_list)
48
+
49
  auth_list = config.get("users", []) # 实际上是使用者的列表
50
  authflag = len(auth_list) > 0 # 是否开启认证的状态值,改为判断auth_list长度
51
 
modules/shared.py CHANGED
@@ -1,7 +1,10 @@
1
  from modules.presets import COMPLETION_URL, BALANCE_API_URL, USAGE_API_URL, API_HOST
2
  import os
 
 
3
  class State:
4
  interrupted = False
 
5
  completion_url = COMPLETION_URL
6
  balance_api_url = BALANCE_API_URL
7
  usage_api_url = USAGE_API_URL
@@ -28,5 +31,25 @@ class State:
28
  def reset_all(self):
29
  self.interrupted = False
30
  self.completion_url = COMPLETION_URL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  state = State()
 
1
  from modules.presets import COMPLETION_URL, BALANCE_API_URL, USAGE_API_URL, API_HOST
2
  import os
3
+ import queue
4
+
5
  class State:
6
  interrupted = False
7
+ multi_api_key = False
8
  completion_url = COMPLETION_URL
9
  balance_api_url = BALANCE_API_URL
10
  usage_api_url = USAGE_API_URL
 
31
  def reset_all(self):
32
  self.interrupted = False
33
  self.completion_url = COMPLETION_URL
34
+
35
+ def set_api_key_queue(self, api_key_list):
36
+ self.multi_api_key = True
37
+ self.api_key_queue = queue.Queue()
38
+ for api_key in api_key_list:
39
+ self.api_key_queue.put(api_key)
40
+
41
+ def switching_api_key(self, func):
42
+ if not hasattr(self, "api_key_queue"):
43
+ return func
44
+
45
+ def wrapped(*args, **kwargs):
46
+ api_key = self.api_key_queue.get()
47
+ args = list(args)[1:]
48
+ ret = func(api_key, *args, **kwargs)
49
+ self.api_key_queue.put(api_key)
50
+ return ret
51
+
52
+ return wrapped
53
+
54
 
55
  state = State()