Spaces:
Runtime error
Runtime error
import importlib | |
import time | |
import os | |
from functools import lru_cache | |
from colorful import print亮红, print亮绿, print亮蓝 | |
pj = os.path.join | |
default_user_name = 'default_user' | |
def read_env_variable(arg, default_value): | |
""" | |
环境变量可以是 `GPT_ACADEMIC_CONFIG`(优先),也可以直接是`CONFIG` | |
例如在windows cmd中,既可以写: | |
set USE_PROXY=True | |
set API_KEY=sk-j7caBpkRoxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
set proxies={"http":"http://127.0.0.1:10085", "https":"http://127.0.0.1:10085",} | |
set AVAIL_LLM_MODELS=["gpt-3.5-turbo", "chatglm"] | |
set AUTHENTICATION=[("username", "password"), ("username2", "password2")] | |
也可以写: | |
set GPT_ACADEMIC_USE_PROXY=True | |
set GPT_ACADEMIC_API_KEY=sk-j7caBpkRoxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
set GPT_ACADEMIC_proxies={"http":"http://127.0.0.1:10085", "https":"http://127.0.0.1:10085",} | |
set GPT_ACADEMIC_AVAIL_LLM_MODELS=["gpt-3.5-turbo", "chatglm"] | |
set GPT_ACADEMIC_AUTHENTICATION=[("username", "password"), ("username2", "password2")] | |
""" | |
arg_with_prefix = "GPT_ACADEMIC_" + arg | |
if arg_with_prefix in os.environ: | |
env_arg = os.environ[arg_with_prefix] | |
elif arg in os.environ: | |
env_arg = os.environ[arg] | |
else: | |
raise KeyError | |
print(f"[ENV_VAR] 尝试加载{arg},默认值:{default_value} --> 修正值:{env_arg}") | |
try: | |
if isinstance(default_value, bool): | |
env_arg = env_arg.strip() | |
if env_arg == 'True': r = True | |
elif env_arg == 'False': r = False | |
else: print('Enter True or False, but have:', env_arg); r = default_value | |
elif isinstance(default_value, int): | |
r = int(env_arg) | |
elif isinstance(default_value, float): | |
r = float(env_arg) | |
elif isinstance(default_value, str): | |
r = env_arg.strip() | |
elif isinstance(default_value, dict): | |
r = eval(env_arg) | |
elif isinstance(default_value, list): | |
r = eval(env_arg) | |
elif default_value is None: | |
assert arg == "proxies" | |
r = eval(env_arg) | |
else: | |
print亮红(f"[ENV_VAR] 环境变量{arg}不支持通过环境变量设置! ") | |
raise KeyError | |
except: | |
print亮红(f"[ENV_VAR] 环境变量{arg}加载失败! ") | |
raise KeyError(f"[ENV_VAR] 环境变量{arg}加载失败! ") | |
print亮绿(f"[ENV_VAR] 成功读取环境变量{arg}") | |
return r | |
def read_single_conf_with_lru_cache(arg): | |
from shared_utils.key_pattern_manager import is_any_api_key | |
try: | |
# 优先级1. 获取环境变量作为配置 | |
default_ref = getattr(importlib.import_module('config'), arg) # 读取默认值作为数据类型转换的参考 | |
r = read_env_variable(arg, default_ref) | |
except: | |
try: | |
# # 优先级2. 获取config_private中的配置 | |
# r = getattr(importlib.import_module('config_private'), arg) | |
# except: | |
# # 优先级3. 获取config中的配置 | |
r = getattr(importlib.import_module('config'), arg) | |
# 在读取API_KEY时,检查一下是不是忘了改config | |
if arg == 'API_URL_REDIRECT': | |
oai_rd = r.get("https://api.openai.com/v1/chat/completions", None) # API_URL_REDIRECT填写格式是错误的,请阅读`https://github.com/binary-husky/gpt_academic/wiki/项目配置说明` | |
if oai_rd and not oai_rd.endswith('/completions'): | |
print亮红("\n\n[API_URL_REDIRECT] API_URL_REDIRECT填错了。请阅读`https://github.com/binary-husky/gpt_academic/wiki/项目配置说明`。如果您确信自己没填错,无视此消息即可。") | |
time.sleep(5) | |
if arg == 'API_KEY': | |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和Azure的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,azure-key3\"") | |
print亮蓝(f"[API_KEY] 您既可以在config.py中修改api-key(s),也可以在问题输入区输入临时的api-key(s),然后回车键提交后即可生效。") | |
if is_any_api_key(r): | |
print亮绿(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功") | |
else: | |
print亮红("[API_KEY] 您的 API_KEY 不满足任何一种已知的密钥格式,请在config文件中修改API密钥之后再运行。") | |
if arg == 'proxies': | |
if not read_single_conf_with_lru_cache('USE_PROXY'): r = None # 检查USE_PROXY,防止proxies单独起作用 | |
if r is None: | |
print亮红('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问OpenAI家族的模型。建议:检查USE_PROXY选项是否修改。') | |
else: | |
print亮绿('[PROXY] 网络代理状态:已配置。配置信息如下:', r) | |
assert isinstance(r, dict), 'proxies格式错误,请注意proxies选项的格式,不要遗漏括号。' | |
return r | |
def get_conf(*args): | |
""" | |
本项目的所有配置都集中在config.py中。 修改配置有三种方法,您只需要选择其中一种即可: | |
- 直接修改config.py | |
- 创建并修改config_private.py | |
- 修改环境变量(修改docker-compose.yml等价于修改容器内部的环境变量) | |
注意:如果您使用docker-compose部署,请修改docker-compose(等价于修改容器内部的环境变量) | |
""" | |
res = [] | |
for arg in args: | |
r = read_single_conf_with_lru_cache(arg) | |
res.append(r) | |
if len(res) == 1: return res[0] | |
return res | |
def set_conf(key, value): | |
from toolbox import read_single_conf_with_lru_cache | |
read_single_conf_with_lru_cache.cache_clear() | |
get_conf.cache_clear() | |
os.environ[key] = str(value) | |
altered = get_conf(key) | |
return altered | |
def set_multi_conf(dic): | |
for k, v in dic.items(): set_conf(k, v) | |
return | |