Spaces:
Running
Running
增加通用性
Browse files- toolbox.py +44 -41
toolbox.py
CHANGED
@@ -518,61 +518,64 @@ def select_api_key(keys, llm_model):
|
|
518 |
api_key = random.choice(avail_key_list) # 随机负载均衡
|
519 |
return api_key
|
520 |
|
521 |
-
def
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
# 格式为:username1:password1;username2:password2
|
546 |
-
for item in os.environ[env_arg].split(";"):
|
547 |
-
r.append(tuple(item.split(":")))
|
548 |
-
elif arg == "API_URL_REDIRECT":
|
549 |
-
# 对于API_URL_REDIRECT的环境变量,我们允许用户使用json格式配置多个url重定向
|
550 |
-
# 格式为一个json字符串,例如:{"https://api.openai.com/v1/chat/completions": "https://ai.open.com/api/conversation"}
|
551 |
-
import json
|
552 |
-
r = json.loads(os.environ[env_arg])
|
553 |
-
elif isinstance(default_value, bool):
|
554 |
-
r = bool(os.environ[env_arg])
|
555 |
elif isinstance(default_value, int):
|
556 |
-
r = int(
|
557 |
elif isinstance(default_value, float):
|
558 |
-
r = float(
|
559 |
elif isinstance(default_value, str):
|
560 |
-
r =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
else:
|
562 |
-
|
|
|
|
|
563 |
return r
|
564 |
|
565 |
@lru_cache(maxsize=128)
|
566 |
def read_single_conf_with_lru_cache(arg):
|
567 |
from colorful import print亮红, print亮绿, print亮蓝
|
568 |
-
default_r = getattr(importlib.import_module('config'), arg)
|
569 |
try:
|
570 |
-
|
|
|
|
|
571 |
except:
|
572 |
try:
|
|
|
573 |
r = getattr(importlib.import_module('config_private'), arg)
|
574 |
except:
|
575 |
-
|
|
|
|
|
576 |
# 在读取API_KEY时,检查一下是不是忘了改config
|
577 |
if arg == 'API_KEY':
|
578 |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
|
|
|
518 |
api_key = random.choice(avail_key_list) # 随机负载均衡
|
519 |
return api_key
|
520 |
|
521 |
+
def read_env_variable(arg, default_value):
|
522 |
+
"""
|
523 |
+
环境变量可以是 `GPT_ACADEMIC_CONFIG`(优先),也可以直接是`CONFIG`
|
524 |
+
例如在windows cmd中,既可以写:
|
525 |
+
set USE_PROXY=True
|
526 |
+
set API_KEY=sk-j7caBpkRoxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
527 |
+
set proxies={"http":"http://127.0.0.1:10085", "https":"http://127.0.0.1:10085",}
|
528 |
+
set AVAIL_LLM_MODELS=["gpt-3.5-turbo", "chatglm"]
|
529 |
+
set AUTHENTICATION=[("username", "password"), ("username2", "password2")]
|
530 |
+
也可以写:
|
531 |
+
set GPT_ACADEMIC_USE_PROXY=True
|
532 |
+
set GPT_ACADEMIC_API_KEY=sk-j7caBpkRoxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
533 |
+
set GPT_ACADEMIC_proxies={"http":"http://127.0.0.1:10085", "https":"http://127.0.0.1:10085",}
|
534 |
+
set GPT_ACADEMIC_AVAIL_LLM_MODELS=["gpt-3.5-turbo", "chatglm"]
|
535 |
+
set GPT_ACADEMIC_AUTHENTICATION=[("username", "password"), ("username2", "password2")]
|
536 |
+
"""
|
537 |
+
from colorful import print亮红, print亮绿
|
538 |
+
arg_with_prefix = "GPT_ACADEMIC_" + arg
|
539 |
+
if arg_with_prefix in os.environ: env_arg = os.environ[arg_with_prefix]
|
540 |
+
elif arg in os.environ: env_arg = os.environ[arg]
|
541 |
+
else:
|
542 |
+
raise KeyError
|
543 |
+
if isinstance(default_value, bool):
|
544 |
+
r = bool(env_arg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
545 |
elif isinstance(default_value, int):
|
546 |
+
r = int(env_arg)
|
547 |
elif isinstance(default_value, float):
|
548 |
+
r = float(env_arg)
|
549 |
elif isinstance(default_value, str):
|
550 |
+
r = env_arg
|
551 |
+
elif isinstance(default_value, dict):
|
552 |
+
r = eval(env_arg)
|
553 |
+
elif isinstance(default_value, list):
|
554 |
+
r = eval(env_arg)
|
555 |
+
elif default_value is None:
|
556 |
+
assert arg == "proxies"
|
557 |
+
r = eval(env_arg)
|
558 |
else:
|
559 |
+
print亮红(f"[ENV_VAR] 环境变量{arg}不支持通过环境变量设置!{default_value}")
|
560 |
+
raise KeyError
|
561 |
+
print亮绿(f"[ENV_VAR] 成功读取环境变量{arg}")
|
562 |
return r
|
563 |
|
564 |
@lru_cache(maxsize=128)
|
565 |
def read_single_conf_with_lru_cache(arg):
|
566 |
from colorful import print亮红, print亮绿, print亮蓝
|
|
|
567 |
try:
|
568 |
+
# 优先级1. 获取环境变量作为配置
|
569 |
+
default_ref = getattr(importlib.import_module('config'), arg) # 读取默认值作为数据类型转换的参考
|
570 |
+
r = read_env_variable(arg, default_ref)
|
571 |
except:
|
572 |
try:
|
573 |
+
# 优先级2. 获取config_private中的配置
|
574 |
r = getattr(importlib.import_module('config_private'), arg)
|
575 |
except:
|
576 |
+
# 优先级3. 获取config中的配置
|
577 |
+
r = getattr(importlib.import_module('config'), arg)
|
578 |
+
|
579 |
# 在读取API_KEY时,检查一下是不是忘了改config
|
580 |
if arg == 'API_KEY':
|
581 |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
|