|
import importlib |
|
import time |
|
import os |
|
from functools import lru_cache |
|
from shared_utils.colorful import log亮红, log亮绿, log亮蓝 |
|
|
|
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 |
|
log亮绿(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: log亮红('Expect `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: |
|
log亮红(f"[ENV_VAR] 环境变量{arg}不支持通过环境变量设置! ") |
|
raise KeyError |
|
except: |
|
log亮红(f"[ENV_VAR] 环境变量{arg}加载失败! ") |
|
raise KeyError(f"[ENV_VAR] 环境变量{arg}加载失败! ") |
|
|
|
log亮绿(f"[ENV_VAR] 成功读取环境变量{arg}") |
|
return r |
|
|
|
|
|
@lru_cache(maxsize=128) |
|
def read_single_conf_with_lru_cache(arg): |
|
from shared_utils.key_pattern_manager import is_any_api_key |
|
try: |
|
|
|
default_ref = getattr(importlib.import_module('config'), arg) |
|
r = read_env_variable(arg, default_ref) |
|
except: |
|
try: |
|
|
|
r = getattr(importlib.import_module('config_private'), arg) |
|
except: |
|
|
|
r = getattr(importlib.import_module('config'), arg) |
|
|
|
|
|
if arg == 'API_URL_REDIRECT': |
|
oai_rd = r.get("https://api.openai.com/v1/chat/completions", None) |
|
if oai_rd and not oai_rd.endswith('/completions'): |
|
log亮红("\n\n[API_URL_REDIRECT] API_URL_REDIRECT填错了。请阅读`https://github.com/binary-husky/gpt_academic/wiki/项目配置说明`。如果您确信自己没填错,无视此消息即可。") |
|
time.sleep(5) |
|
if arg == 'API_KEY': |
|
log亮蓝(f"[API_KEY] 本项目现已支持OpenAI和Azure的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,azure-key3\"") |
|
log亮蓝(f"[API_KEY] 您既可以在config.py中修改api-key(s),也可以在问题输入区输入临时的api-key(s),然后回车键提交后即可生效。") |
|
if is_any_api_key(r): |
|
log亮绿(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功") |
|
else: |
|
log亮红(f"[API_KEY] 您的 API_KEY({r[:15]}***)不满足任何一种已知的密钥格式,请在config文件中修改API密钥之后再运行(详见`https://github.com/binary-husky/gpt_academic/wiki/api_key`)。") |
|
if arg == 'proxies': |
|
if not read_single_conf_with_lru_cache('USE_PROXY'): r = None |
|
if r is None: |
|
log亮红('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问OpenAI家族的模型。建议:检查USE_PROXY选项是否修改。') |
|
else: |
|
log亮绿('[PROXY] 网络代理状态:已配置。配置信息如下:', str(r)) |
|
assert isinstance(r, dict), 'proxies格式错误,请注意proxies选项的格式,不要遗漏括号。' |
|
return r |
|
|
|
|
|
@lru_cache(maxsize=128) |
|
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 |
|
|