Spaces:
Runtime error
Runtime error
ReeInk
commited on
Commit
•
e5e3e0a
1
Parent(s):
f187a23
读取环境变量作为配置
Browse files- toolbox.py +53 -2
toolbox.py
CHANGED
@@ -3,6 +3,7 @@ import importlib
|
|
3 |
import traceback
|
4 |
import inspect
|
5 |
import re
|
|
|
6 |
from latex2mathml.converter import convert as tex2mathml
|
7 |
from functools import wraps, lru_cache
|
8 |
|
@@ -517,13 +518,63 @@ def select_api_key(keys, llm_model):
|
|
517 |
api_key = random.choice(avail_key_list) # 随机负载均衡
|
518 |
return api_key
|
519 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
520 |
@lru_cache(maxsize=128)
|
521 |
def read_single_conf_with_lru_cache(arg):
|
522 |
from colorful import print亮红, print亮绿, print亮蓝
|
|
|
523 |
try:
|
524 |
-
r =
|
525 |
except:
|
526 |
-
|
|
|
|
|
|
|
527 |
# 在读取API_KEY时,检查一下是不是忘了改config
|
528 |
if arg == 'API_KEY':
|
529 |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
|
|
|
3 |
import traceback
|
4 |
import inspect
|
5 |
import re
|
6 |
+
import os
|
7 |
from latex2mathml.converter import convert as tex2mathml
|
8 |
from functools import wraps, lru_cache
|
9 |
|
|
|
518 |
api_key = random.choice(avail_key_list) # 随机负载均衡
|
519 |
return api_key
|
520 |
|
521 |
+
def read_single_conf_from_env(arg, default_value):
|
522 |
+
ENV_PREFIX = "GPT_ACADEMIC_" # 环境变量的前缀
|
523 |
+
env_arg = ENV_PREFIX + arg # 环境变量的KEY
|
524 |
+
if arg == "proxies":
|
525 |
+
# 对于proxies,我们使用多个环境变量来配置
|
526 |
+
# HTTP_PROXY: 对应http代理
|
527 |
+
# HTTPS_PROXY: 对应https代理
|
528 |
+
# ALL_PROXY: 对应http和https代理,优先级较HTTP_PROXY和HTTPS_PROXY更低
|
529 |
+
http_proxy = os.environ.get(ENV_PREFIX + "HTTP_PROXY") or os.environ.get("ALL_PROXY")
|
530 |
+
assert http_proxy is not None, f"请设置环境变量{ENV_PREFIX + 'HTTP_PROXY'}"
|
531 |
+
https_proxy = os.environ.get(ENV_PREFIX + "HTTPS_PROXY") or os.environ.get("ALL_PROXY")
|
532 |
+
assert https_proxy is not None, f"请设置环境变量{ENV_PREFIX + 'HTTPS_PROXY'}"
|
533 |
+
r = {
|
534 |
+
"http": http_proxy,
|
535 |
+
"https": https_proxy
|
536 |
+
}
|
537 |
+
elif arg == "AVAIL_LLM_MODELS":
|
538 |
+
r = []
|
539 |
+
# 对于AVAIL_LLM_MODELS的环境变量配置,我们允许用户使用;分隔多个模型
|
540 |
+
for item in os.environ[env_arg].split(";"):
|
541 |
+
r.append(item)
|
542 |
+
elif arg == "AUTHENTICATION":
|
543 |
+
r = []
|
544 |
+
# 对于AUTHENTICATION的环境变量配置,我们允许用户使用;分隔多个账号
|
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 |
+
r = {}
|
550 |
+
# 对于API_URL_REDIRECT的环境变量配置,我们允许用户使用;分隔转发地址
|
551 |
+
# 格式为:url1:redirect1;url2:redirect2
|
552 |
+
for item in os.environ[env_arg].split(";"):
|
553 |
+
k, v = item.split(":")
|
554 |
+
r[k] = v
|
555 |
+
elif isinstance(default_value, bool):
|
556 |
+
r = bool(os.environ[env_arg])
|
557 |
+
elif isinstance(default_value, int):
|
558 |
+
r = int(os.environ[env_arg])
|
559 |
+
elif isinstance(default_value, float):
|
560 |
+
r = float(os.environ[env_arg])
|
561 |
+
elif isinstance(default_value, str):
|
562 |
+
r = os.environ[env_arg]
|
563 |
+
else:
|
564 |
+
raise RuntimeError(f"[CONFIG] 环境变量{arg}不支持自动转换到{type(default_value)}类型")
|
565 |
+
return r
|
566 |
+
|
567 |
@lru_cache(maxsize=128)
|
568 |
def read_single_conf_with_lru_cache(arg):
|
569 |
from colorful import print亮红, print亮绿, print亮蓝
|
570 |
+
default_r = getattr(importlib.import_module('config'), arg)
|
571 |
try:
|
572 |
+
r = read_single_conf_from_env(arg, default_r) # 优先获取环境变量作为配置
|
573 |
except:
|
574 |
+
try:
|
575 |
+
r = getattr(importlib.import_module('config_private'), arg)
|
576 |
+
except:
|
577 |
+
r = default_r
|
578 |
# 在读取API_KEY时,检查一下是不是忘了改config
|
579 |
if arg == 'API_KEY':
|
580 |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
|