From 527f9d28ad12d7ed7a9476bb9ed0ef31a8548aec Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sun, 29 Oct 2023 00:34:40 +0800 Subject: [PATCH] change get_conf --- check_proxy.py | 6 ++-- config.py | 4 +++ crazy_functional.py | 2 +- ...345\207\272PDF\347\273\223\346\236\234.py" | 2 +- crazy_functions/agent_fns/pipe.py | 2 +- crazy_functions/crazy_utils.py | 4 +-- crazy_functions/pdf_fns/parse_pdf.py | 2 +- crazy_functions/vt_fns/vt_modify_config.py | 4 +-- ...73\350\257\221\346\221\230\350\246\201.py" | 4 +-- ...76\347\211\207\347\224\237\346\210\220.py" | 2 +- ...32\346\231\272\350\203\275\344\275\223.py" | 2 +- ...23\351\237\263\350\247\206\351\242\221.py" | 2 +- ...07\217Markdown\347\277\273\350\257\221.py" | 2 +- ...201\224\347\275\221\347\232\204ChatGPT.py" | 2 +- ...21\347\232\204ChatGPT_bing\347\211\210.py" | 2 +- ...42\345\260\217\345\212\251\346\211\213.py" | 2 +- docs/WithFastapi.md | 4 +-- main.py | 6 ++-- multi_language.py | 2 +- request_llms/bridge_all.py | 4 +-- request_llms/bridge_chatglmft.py | 2 +- request_llms/bridge_internlm.py | 2 +- request_llms/bridge_jittorllms_llama.py | 2 +- request_llms/bridge_jittorllms_pangualpha.py | 2 +- request_llms/bridge_jittorllms_rwkv.py | 2 +- request_llms/bridge_newbingfree.py | 2 +- request_llms/bridge_stackclaude.py | 4 +-- request_llms/bridge_zhipu.py | 2 +- request_llms/key_manager.py | 29 +++++++++++++++++++ themes/gradios.py | 2 +- themes/theme.py | 2 +- toolbox.py | 24 ++++++++------- 32 files changed, 85 insertions(+), 50 deletions(-) create mode 100644 request_llms/key_manager.py diff --git a/check_proxy.py b/check_proxy.py index 75de7ab48d..977ac276a9 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -46,7 +46,7 @@ def backup_and_download(current_version, remote_version): return new_version_dir os.makedirs(new_version_dir) shutil.copytree('./', backup_dir, ignore=lambda x, y: ['history']) - proxies, = get_conf('proxies') + proxies = get_conf('proxies') r = requests.get( 'https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True) zip_file_path = backup_dir+'/master.zip' @@ -113,7 +113,7 @@ def auto_update(raise_error=False): import requests import time import json - proxies, = get_conf('proxies') + proxies = get_conf('proxies') response = requests.get( "https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=5) remote_json_data = json.loads(response.text) @@ -167,5 +167,5 @@ def warm_up_modules(): import os os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 from toolbox import get_conf - proxies, = get_conf('proxies') + proxies = get_conf('proxies') check_proxy(proxies) diff --git a/config.py b/config.py index 3175867123..ec047ae805 100644 --- a/config.py +++ b/config.py @@ -223,6 +223,10 @@ "Warmup_Modules", "Nougat_Download", "AutoGen"] +# *实验性功能*: 自动检测并屏蔽失效的KEY,请勿使用 +BLOCK_INVALID_APIKEY = False + + # 自定义按钮的最大数量限制 NUM_CUSTOM_BASIC_BTN = 4 diff --git a/crazy_functional.py b/crazy_functional.py index 795bd5f7b7..2d7fa74b1e 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -498,7 +498,7 @@ def get_crazy_functions(): try: from toolbox import get_conf - ENABLE_AUDIO, = get_conf('ENABLE_AUDIO') + ENABLE_AUDIO = get_conf('ENABLE_AUDIO') if ENABLE_AUDIO: from crazy_functions.语音助手 import 语音助手 function_plugins.update({ diff --git "a/crazy_functions/Latex\350\276\223\345\207\272PDF\347\273\223\346\236\234.py" "b/crazy_functions/Latex\350\276\223\345\207\272PDF\347\273\223\346\236\234.py" index 7ec5a4b2da..9edfea6862 100644 --- "a/crazy_functions/Latex\350\276\223\345\207\272PDF\347\273\223\346\236\234.py" +++ "b/crazy_functions/Latex\350\276\223\345\207\272PDF\347\273\223\346\236\234.py" @@ -129,7 +129,7 @@ def is_float(s): yield from update_ui_lastest_msg("调用缓存", chatbot=chatbot, history=history) # 刷新界面 else: yield from update_ui_lastest_msg("开始下载", chatbot=chatbot, history=history) # 刷新界面 - proxies, = get_conf('proxies') + proxies = get_conf('proxies') r = requests.get(url_tar, proxies=proxies) with open(dst, 'wb+') as f: f.write(r.content) diff --git a/crazy_functions/agent_fns/pipe.py b/crazy_functions/agent_fns/pipe.py index 0e430b4075..5ebe3fc620 100644 --- a/crazy_functions/agent_fns/pipe.py +++ b/crazy_functions/agent_fns/pipe.py @@ -20,7 +20,7 @@ def __init__(self, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, w self.system_prompt = system_prompt self.web_port = web_port self.alive = True - self.use_docker, = get_conf('AUTOGEN_USE_DOCKER') + self.use_docker = get_conf('AUTOGEN_USE_DOCKER') # create a thread to monitor self.heartbeat, terminate the instance if no heartbeat for a long time timeout_seconds = 5*60 diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 04a4e67d27..a23c732b58 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -181,7 +181,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( assert len(inputs_array) == len(history_array) assert len(inputs_array) == len(sys_prompt_array) if max_workers == -1: # 读取配置文件 - try: max_workers, = get_conf('DEFAULT_WORKER_NUM') + try: max_workers = get_conf('DEFAULT_WORKER_NUM') except: max_workers = 8 if max_workers <= 0: max_workers = 3 # 屏蔽掉 chatglm的多线程,可能会导致严重卡顿 @@ -602,7 +602,7 @@ def get_files_from_everything(txt, type): # type='.md' import requests from toolbox import get_conf from toolbox import get_log_folder, gen_time_str - proxies, = get_conf('proxies') + proxies = get_conf('proxies') try: r = requests.get(txt, proxies=proxies) except: diff --git a/crazy_functions/pdf_fns/parse_pdf.py b/crazy_functions/pdf_fns/parse_pdf.py index 396b608ab3..6646c5bfec 100644 --- a/crazy_functions/pdf_fns/parse_pdf.py +++ b/crazy_functions/pdf_fns/parse_pdf.py @@ -14,7 +14,7 @@ class GROBID_OFFLINE_EXCEPTION(Exception): pass def get_avail_grobid_url(): - GROBID_URLS, = get_conf('GROBID_URLS') + GROBID_URLS = get_conf('GROBID_URLS') if len(GROBID_URLS) == 0: return None try: _grobid_url = random.choice(GROBID_URLS) # 随机负载均衡 diff --git a/crazy_functions/vt_fns/vt_modify_config.py b/crazy_functions/vt_fns/vt_modify_config.py index 0e2b3146fd..58a8531e8e 100644 --- a/crazy_functions/vt_fns/vt_modify_config.py +++ b/crazy_functions/vt_fns/vt_modify_config.py @@ -7,7 +7,7 @@ def modify_configuration_hot(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_intention): - ALLOW_RESET_CONFIG, = get_conf('ALLOW_RESET_CONFIG') + ALLOW_RESET_CONFIG = get_conf('ALLOW_RESET_CONFIG') if not ALLOW_RESET_CONFIG: yield from update_ui_lastest_msg( lastmsg=f"当前配置不允许被修改!如需激活本功能,请在config.py中设置ALLOW_RESET_CONFIG=True后重启软件。", @@ -66,7 +66,7 @@ class ModifyConfigurationIntention(BaseModel): ) def modify_configuration_reboot(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_intention): - ALLOW_RESET_CONFIG, = get_conf('ALLOW_RESET_CONFIG') + ALLOW_RESET_CONFIG = get_conf('ALLOW_RESET_CONFIG') if not ALLOW_RESET_CONFIG: yield from update_ui_lastest_msg( lastmsg=f"当前配置不允许被修改!如需激活本功能,请在config.py中设置ALLOW_RESET_CONFIG=True后重启软件。", diff --git "a/crazy_functions/\344\270\213\350\275\275arxiv\350\256\272\346\226\207\347\277\273\350\257\221\346\221\230\350\246\201.py" "b/crazy_functions/\344\270\213\350\275\275arxiv\350\256\272\346\226\207\347\277\273\350\257\221\346\221\230\350\246\201.py" index 8b4a5037a2..c711cf4531 100644 --- "a/crazy_functions/\344\270\213\350\275\275arxiv\350\256\272\346\226\207\347\277\273\350\257\221\346\221\230\350\246\201.py" +++ "b/crazy_functions/\344\270\213\350\275\275arxiv\350\256\272\346\226\207\347\277\273\350\257\221\346\221\230\350\246\201.py" @@ -43,7 +43,7 @@ def download_arxiv_(url_pdf): file_path = download_dir+title_str print('下载中') - proxies, = get_conf('proxies') + proxies = get_conf('proxies') r = requests.get(requests_pdf_url, proxies=proxies) with open(file_path, 'wb+') as f: f.write(r.content) @@ -77,7 +77,7 @@ def get_name(_url_): # print('在缓存中') # return arxiv_recall[_url_] - proxies, = get_conf('proxies') + proxies = get_conf('proxies') res = requests.get(_url_, proxies=proxies) bs = BeautifulSoup(res.text, 'html.parser') diff --git "a/crazy_functions/\345\233\276\347\211\207\347\224\237\346\210\220.py" "b/crazy_functions/\345\233\276\347\211\207\347\224\237\346\210\220.py" index 09bd9beb7d..1b7dff5d9d 100644 --- "a/crazy_functions/\345\233\276\347\211\207\347\224\237\346\210\220.py" +++ "b/crazy_functions/\345\233\276\347\211\207\347\224\237\346\210\220.py" @@ -7,7 +7,7 @@ def gen_image(llm_kwargs, prompt, resolution="256x256"): import requests, json, time, os from request_llms.bridge_all import model_info - proxies, = get_conf('proxies') + proxies = get_conf('proxies') # Set up OpenAI API key and model api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model']) chat_endpoint = model_info[llm_kwargs['llm_model']]['endpoint'] diff --git "a/crazy_functions/\345\244\232\346\231\272\350\203\275\344\275\223.py" "b/crazy_functions/\345\244\232\346\231\272\350\203\275\344\275\223.py" index 5a4c4a58ae..99b3e86b60 100644 --- "a/crazy_functions/\345\244\232\346\231\272\350\203\275\344\275\223.py" +++ "b/crazy_functions/\345\244\232\346\231\272\350\203\275\344\275\223.py" @@ -41,7 +41,7 @@ def 多智能体终端(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_ return # 检查当前的模型是否符合要求 - API_URL_REDIRECT, = get_conf('API_URL_REDIRECT') + API_URL_REDIRECT = get_conf('API_URL_REDIRECT') if len(API_URL_REDIRECT) > 0: chatbot.append([f"处理任务: {txt}", f"暂不支持中转."]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 diff --git "a/crazy_functions/\346\200\273\347\273\223\351\237\263\350\247\206\351\242\221.py" "b/crazy_functions/\346\200\273\347\273\223\351\237\263\350\247\206\351\242\221.py" index b946d37d49..b88775b415 100644 --- "a/crazy_functions/\346\200\273\347\273\223\351\237\263\350\247\206\351\242\221.py" +++ "b/crazy_functions/\346\200\273\347\273\223\351\237\263\350\247\206\351\242\221.py" @@ -79,7 +79,7 @@ def AnalyAudio(parse_prompt, file_manifest, llm_kwargs, chatbot, history): chatbot.append([f"将 {i} 发送到openai音频解析终端 (whisper),当前参数:{parse_prompt}", "正在处理 ..."]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 - proxies, = get_conf('proxies') + proxies = get_conf('proxies') response = requests.post(url, headers=headers, files=files, data=data, proxies=proxies).text chatbot.append(["音频解析结果", response]) diff --git "a/crazy_functions/\346\211\271\351\207\217Markdown\347\277\273\350\257\221.py" "b/crazy_functions/\346\211\271\351\207\217Markdown\347\277\273\350\257\221.py" index e245b72d5e..2bdffc8662 100644 --- "a/crazy_functions/\346\211\271\351\207\217Markdown\347\277\273\350\257\221.py" +++ "b/crazy_functions/\346\211\271\351\207\217Markdown\347\277\273\350\257\221.py" @@ -118,7 +118,7 @@ def get_files_from_everything(txt, preference=''): if txt.startswith('http'): import requests from toolbox import get_conf - proxies, = get_conf('proxies') + proxies = get_conf('proxies') # 网络的远程文件 if preference == 'Github': logging.info('正在从github下载资源 ...') diff --git "a/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT.py" "b/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT.py" index be286bcd5e..6fa50ec2e3 100644 --- "a/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT.py" +++ "b/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT.py" @@ -72,7 +72,7 @@ def 连接网络回答问题(txt, llm_kwargs, plugin_kwargs, chatbot, history, s # ------------- < 第1步:爬取搜索引擎的结果 > ------------- from toolbox import get_conf - proxies, = get_conf('proxies') + proxies = get_conf('proxies') urls = google(txt, proxies) history = [] if len(urls) == 0: diff --git "a/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT_bing\347\211\210.py" "b/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT_bing\347\211\210.py" index 666fcb8b29..009ebdce52 100644 --- "a/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT_bing\347\211\210.py" +++ "b/crazy_functions/\350\201\224\347\275\221\347\232\204ChatGPT_bing\347\211\210.py" @@ -72,7 +72,7 @@ def 连接bing搜索回答问题(txt, llm_kwargs, plugin_kwargs, chatbot, histor # ------------- < 第1步:爬取搜索引擎的结果 > ------------- from toolbox import get_conf - proxies, = get_conf('proxies') + proxies = get_conf('proxies') urls = bing_search(txt, proxies) history = [] if len(urls) == 0: diff --git "a/crazy_functions/\350\260\267\346\255\214\346\243\200\347\264\242\345\260\217\345\212\251\346\211\213.py" "b/crazy_functions/\350\260\267\346\255\214\346\243\200\347\264\242\345\260\217\345\212\251\346\211\213.py" index dae8a2bb3d..5924a2860d 100644 --- "a/crazy_functions/\350\260\267\346\255\214\346\243\200\347\264\242\345\260\217\345\212\251\346\211\213.py" +++ "b/crazy_functions/\350\260\267\346\255\214\346\243\200\347\264\242\345\260\217\345\212\251\346\211\213.py" @@ -17,7 +17,7 @@ def get_meta_information(url, chatbot, history): from urllib.parse import urlparse session = requests.session() - proxies, = get_conf('proxies') + proxies = get_conf('proxies') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36', 'Accept-Encoding': 'gzip, deflate, br', diff --git a/docs/WithFastapi.md b/docs/WithFastapi.md index 188b527164..bbbb386e8a 100644 --- a/docs/WithFastapi.md +++ b/docs/WithFastapi.md @@ -16,7 +16,7 @@ nano config.py + demo.queue(concurrency_count=CONCURRENT_COUNT) - # 如果需要在二级路径下运行 - - # CUSTOM_PATH, = get_conf('CUSTOM_PATH') + - # CUSTOM_PATH = get_conf('CUSTOM_PATH') - # if CUSTOM_PATH != "/": - # from toolbox import run_gradio_in_subpath - # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH) @@ -24,7 +24,7 @@ nano config.py - # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png") + 如果需要在二级路径下运行 - + CUSTOM_PATH, = get_conf('CUSTOM_PATH') + + CUSTOM_PATH = get_conf('CUSTOM_PATH') + if CUSTOM_PATH != "/": + from toolbox import run_gradio_in_subpath + run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH) diff --git a/main.py b/main.py index fff1a3e3c5..ee8f5cf78c 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ def main(): CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT') ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME') DARK_MODE, NUM_CUSTOM_BASIC_BTN, SSL_KEYFILE, SSL_CERTFILE = get_conf('DARK_MODE', 'NUM_CUSTOM_BASIC_BTN', 'SSL_KEYFILE', 'SSL_CERTFILE') - INIT_SYS_PROMPT, = get_conf('INIT_SYS_PROMPT') + INIT_SYS_PROMPT = get_conf('INIT_SYS_PROMPT') # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT @@ -48,7 +48,7 @@ def main(): # 高级函数插件 from crazy_functional import get_crazy_functions - DEFAULT_FN_GROUPS, = get_conf('DEFAULT_FN_GROUPS') + DEFAULT_FN_GROUPS = get_conf('DEFAULT_FN_GROUPS') plugins = get_crazy_functions() all_plugin_groups = list(set([g for _, plugin in plugins.items() for g in plugin['Group'].split('|')])) match_group = lambda tags, groups: any([g in groups for g in tags.split('|')]) @@ -436,7 +436,7 @@ def warm_up_mods(): time.sleep(4); warm_up_modules() blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"]) # 如果需要在二级路径下运行 - # CUSTOM_PATH, = get_conf('CUSTOM_PATH') + # CUSTOM_PATH = get_conf('CUSTOM_PATH') # if CUSTOM_PATH != "/": # from toolbox import run_gradio_in_subpath # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH) diff --git a/multi_language.py b/multi_language.py index f78cd997ca..a20fb5af86 100644 --- a/multi_language.py +++ b/multi_language.py @@ -38,7 +38,7 @@ CACHE_ONLY = os.environ.get('CACHE_ONLY', False) -CACHE_FOLDER, = get_conf('PATH_LOGGING') +CACHE_FOLDER = get_conf('PATH_LOGGING') blacklist = ['multi-language', CACHE_FOLDER, '.git', 'private_upload', 'multi_language.py', 'build', '.github', '.vscode', '__pycache__', 'venv'] diff --git a/request_llms/bridge_all.py b/request_llms/bridge_all.py index 83c3661236..b484d1f81e 100644 --- a/request_llms/bridge_all.py +++ b/request_llms/bridge_all.py @@ -56,7 +56,7 @@ def decode(self, *args, **kwargs): azure_endpoint = AZURE_ENDPOINT + f'openai/deployments/{AZURE_ENGINE}/chat/completions?api-version=2023-05-15' # 兼容旧版的配置 try: - API_URL, = get_conf("API_URL") + API_URL = get_conf("API_URL") if API_URL != "https://api.openai.com/v1/chat/completions": openai_endpoint = API_URL print("警告!API_URL配置选项将被弃用,请更换为API_URL_REDIRECT配置") @@ -501,7 +501,7 @@ def decode(self, *args, **kwargs): print(trimmed_format_exc()) # <-- 用于定义和切换多个azure模型 --> -AZURE_CFG_ARRAY, = get_conf("AZURE_CFG_ARRAY") +AZURE_CFG_ARRAY = get_conf("AZURE_CFG_ARRAY") if len(AZURE_CFG_ARRAY) > 0: for azure_model_name, azure_cfg_dict in AZURE_CFG_ARRAY.items(): # 可能会覆盖之前的配置,但这是意料之中的 diff --git a/request_llms/bridge_chatglmft.py b/request_llms/bridge_chatglmft.py index 63f36049ac..d812bae3c3 100644 --- a/request_llms/bridge_chatglmft.py +++ b/request_llms/bridge_chatglmft.py @@ -63,7 +63,7 @@ def run(self): # if not os.path.exists(conf): raise RuntimeError('找不到微调模型信息') # with open(conf, 'r', encoding='utf8') as f: # model_args = json.loads(f.read()) - CHATGLM_PTUNING_CHECKPOINT, = get_conf('CHATGLM_PTUNING_CHECKPOINT') + CHATGLM_PTUNING_CHECKPOINT = get_conf('CHATGLM_PTUNING_CHECKPOINT') assert os.path.exists(CHATGLM_PTUNING_CHECKPOINT), "找不到微调模型检查点" conf = os.path.join(CHATGLM_PTUNING_CHECKPOINT, "config.json") with open(conf, 'r', encoding='utf8') as f: diff --git a/request_llms/bridge_internlm.py b/request_llms/bridge_internlm.py index 3304fe234f..073c193ad4 100644 --- a/request_llms/bridge_internlm.py +++ b/request_llms/bridge_internlm.py @@ -52,7 +52,7 @@ def load_model_and_tokenizer(self): # 🏃‍♂️🏃‍♂️🏃‍♂️ 子进程执行 import torch from transformers import AutoModelForCausalLM, AutoTokenizer - device, = get_conf('LOCAL_MODEL_DEVICE') + device = get_conf('LOCAL_MODEL_DEVICE') if self._model is None: tokenizer = AutoTokenizer.from_pretrained("internlm/internlm-chat-7b", trust_remote_code=True) if device=='cpu': diff --git a/request_llms/bridge_jittorllms_llama.py b/request_llms/bridge_jittorllms_llama.py index af2d9fbdc3..2d3005e52d 100644 --- a/request_llms/bridge_jittorllms_llama.py +++ b/request_llms/bridge_jittorllms_llama.py @@ -53,7 +53,7 @@ def load_model(): import types try: if self.jittorllms_model is None: - device, = get_conf('LOCAL_MODEL_DEVICE') + device = get_conf('LOCAL_MODEL_DEVICE') from .jittorllms.models import get_model # availabel_models = ["chatglm", "pangualpha", "llama", "chatrwkv"] args_dict = {'model': 'llama'} diff --git a/request_llms/bridge_jittorllms_pangualpha.py b/request_llms/bridge_jittorllms_pangualpha.py index df0523b488..26401764c9 100644 --- a/request_llms/bridge_jittorllms_pangualpha.py +++ b/request_llms/bridge_jittorllms_pangualpha.py @@ -53,7 +53,7 @@ def load_model(): import types try: if self.jittorllms_model is None: - device, = get_conf('LOCAL_MODEL_DEVICE') + device = get_conf('LOCAL_MODEL_DEVICE') from .jittorllms.models import get_model # availabel_models = ["chatglm", "pangualpha", "llama", "chatrwkv"] args_dict = {'model': 'pangualpha'} diff --git a/request_llms/bridge_jittorllms_rwkv.py b/request_llms/bridge_jittorllms_rwkv.py index 875673aac7..0021a50d0a 100644 --- a/request_llms/bridge_jittorllms_rwkv.py +++ b/request_llms/bridge_jittorllms_rwkv.py @@ -53,7 +53,7 @@ def load_model(): import types try: if self.jittorllms_model is None: - device, = get_conf('LOCAL_MODEL_DEVICE') + device = get_conf('LOCAL_MODEL_DEVICE') from .jittorllms.models import get_model # availabel_models = ["chatglm", "pangualpha", "llama", "chatrwkv"] args_dict = {'model': 'chatrwkv'} diff --git a/request_llms/bridge_newbingfree.py b/request_llms/bridge_newbingfree.py index 5dddb6105c..cb83a0fb0c 100644 --- a/request_llms/bridge_newbingfree.py +++ b/request_llms/bridge_newbingfree.py @@ -62,7 +62,7 @@ def ready(self): async def async_run(self): # 读取配置 - NEWBING_STYLE, = get_conf('NEWBING_STYLE') + NEWBING_STYLE = get_conf('NEWBING_STYLE') from request_llms.bridge_all import model_info endpoint = model_info['newbing']['endpoint'] while True: diff --git a/request_llms/bridge_stackclaude.py b/request_llms/bridge_stackclaude.py index 3b57615671..a39398444d 100644 --- a/request_llms/bridge_stackclaude.py +++ b/request_llms/bridge_stackclaude.py @@ -146,14 +146,14 @@ def run(self): self.local_history = [] if (self.claude_model is None) or (not self.success): # 代理设置 - proxies, = get_conf('proxies') + proxies = get_conf('proxies') if proxies is None: self.proxies_https = None else: self.proxies_https = proxies['https'] try: - SLACK_CLAUDE_USER_TOKEN, = get_conf('SLACK_CLAUDE_USER_TOKEN') + SLACK_CLAUDE_USER_TOKEN = get_conf('SLACK_CLAUDE_USER_TOKEN') self.claude_model = SlackClient(token=SLACK_CLAUDE_USER_TOKEN, proxy=self.proxies_https) print('Claude组件初始化成功。') except: diff --git a/request_llms/bridge_zhipu.py b/request_llms/bridge_zhipu.py index 2fd19ad508..a1e0de5918 100644 --- a/request_llms/bridge_zhipu.py +++ b/request_llms/bridge_zhipu.py @@ -5,7 +5,7 @@ model_name = '智谱AI大模型' def validate_key(): - ZHIPUAI_API_KEY, = get_conf("ZHIPUAI_API_KEY") + ZHIPUAI_API_KEY = get_conf("ZHIPUAI_API_KEY") if ZHIPUAI_API_KEY == '': return False return True diff --git a/request_llms/key_manager.py b/request_llms/key_manager.py new file mode 100644 index 0000000000..8563d2ef82 --- /dev/null +++ b/request_llms/key_manager.py @@ -0,0 +1,29 @@ +import random + +def Singleton(cls): + _instance = {} + + def _singleton(*args, **kargs): + if cls not in _instance: + _instance[cls] = cls(*args, **kargs) + return _instance[cls] + + return _singleton + + +@Singleton +class OpenAI_ApiKeyManager(): + def __init__(self, mode='blacklist') -> None: + # self.key_avail_list = [] + self.key_black_list = [] + + def add_key_to_blacklist(self, key): + self.key_black_list.append(key) + + def select_avail_key(self, key_list): + # select key from key_list, but avoid keys also in self.key_black_list, raise error if no key can be found + available_keys = [key for key in key_list if key not in self.key_black_list] + if not available_keys: + raise KeyError("No available key found.") + selected_key = random.choice(available_keys) + return selected_key \ No newline at end of file diff --git a/themes/gradios.py b/themes/gradios.py index 7693a23808..96a9c54e60 100644 --- a/themes/gradios.py +++ b/themes/gradios.py @@ -18,7 +18,7 @@ def adjust_theme(): set_theme = gr.themes.ThemeClass() with ProxyNetworkActivate('Download_Gradio_Theme'): logging.info('正在下载Gradio主题,请稍等。') - THEME, = get_conf('THEME') + THEME = get_conf('THEME') if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-') if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-') set_theme = set_theme.from_hub(THEME.lower()) diff --git a/themes/theme.py b/themes/theme.py index 42ee75000d..f59db9f832 100644 --- a/themes/theme.py +++ b/themes/theme.py @@ -1,6 +1,6 @@ import gradio as gr from toolbox import get_conf -THEME, = get_conf('THEME') +THEME = get_conf('THEME') def load_dynamic_theme(THEME): adjust_dynamic_theme = None diff --git a/toolbox.py b/toolbox.py index 137313efa7..5b7a751921 100644 --- a/toolbox.py +++ b/toolbox.py @@ -152,7 +152,7 @@ def decorated(main_input, llm_kwargs, plugin_kwargs, chatbot_with_cookie, histor except Exception as e: from check_proxy import check_proxy from toolbox import get_conf - proxies, = get_conf('proxies') + proxies = get_conf('proxies') tb_str = '```\n' + trimmed_format_exc() + '```' if len(chatbot_with_cookie) == 0: chatbot_with_cookie.clear() @@ -555,14 +555,14 @@ def disable_auto_promotion(chatbot): return def is_the_upload_folder(string): - PATH_PRIVATE_UPLOAD, = get_conf('PATH_PRIVATE_UPLOAD') + PATH_PRIVATE_UPLOAD = get_conf('PATH_PRIVATE_UPLOAD') pattern = r'^PATH_PRIVATE_UPLOAD/[A-Za-z0-9_-]+/\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}$' pattern = pattern.replace('PATH_PRIVATE_UPLOAD', PATH_PRIVATE_UPLOAD) if re.match(pattern, string): return True else: return False def del_outdated_uploads(outdate_time_seconds): - PATH_PRIVATE_UPLOAD, = get_conf('PATH_PRIVATE_UPLOAD') + PATH_PRIVATE_UPLOAD = get_conf('PATH_PRIVATE_UPLOAD') current_time = time.time() one_hour_ago = current_time - outdate_time_seconds # Get a list of all subdirectories in the PATH_PRIVATE_UPLOAD folder @@ -588,7 +588,7 @@ def on_file_uploaded(request: gradio.Request, files, chatbot, txt, txt2, checkbo # 创建工作路径 user_name = "default" if not request.username else request.username time_tag = gen_time_str() - PATH_PRIVATE_UPLOAD, = get_conf('PATH_PRIVATE_UPLOAD') + PATH_PRIVATE_UPLOAD = get_conf('PATH_PRIVATE_UPLOAD') target_path_base = pj(PATH_PRIVATE_UPLOAD, user_name, time_tag) os.makedirs(target_path_base, exist_ok=True) @@ -626,7 +626,7 @@ def on_file_uploaded(request: gradio.Request, files, chatbot, txt, txt2, checkbo def on_report_generated(cookies, files, chatbot): from toolbox import find_recent_files - PATH_LOGGING, = get_conf('PATH_LOGGING') + PATH_LOGGING = get_conf('PATH_LOGGING') if 'files_to_promote' in cookies: report_files = cookies['files_to_promote'] cookies.pop('files_to_promote') @@ -669,7 +669,7 @@ def load_chat_cookies(): return {'api_key': API_KEY, 'llm_model': LLM_MODEL, 'customize_fn_overwrite': customize_fn_overwrite_} def is_openai_api_key(key): - CUSTOM_API_KEY_PATTERN, = get_conf('CUSTOM_API_KEY_PATTERN') + CUSTOM_API_KEY_PATTERN = get_conf('CUSTOM_API_KEY_PATTERN') if len(CUSTOM_API_KEY_PATTERN) != 0: API_MATCH_ORIGINAL = re.match(CUSTOM_API_KEY_PATTERN, key) else: @@ -732,6 +732,7 @@ def select_api_key(keys, llm_model): raise RuntimeError(f"您提供的api-key不满足要求,不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源(右下角更换模型菜单中可切换openai,azure,claude,api2d等请求源)。") api_key = random.choice(avail_key_list) # 随机负载均衡 + if ENABLE return api_key def read_env_variable(arg, default_value): @@ -828,6 +829,7 @@ def get_conf(*args): for arg in args: r = read_single_conf_with_lru_cache(arg) res.append(r) + if len(res) == 1: return res[0] return res @@ -989,7 +991,7 @@ def gen_time_str(): return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) def get_log_folder(user='default', plugin_name='shared'): - PATH_LOGGING, = get_conf('PATH_LOGGING') + PATH_LOGGING = get_conf('PATH_LOGGING') _dir = pj(PATH_LOGGING, user, plugin_name) if not os.path.exists(_dir): os.makedirs(_dir) return _dir @@ -1006,13 +1008,13 @@ def __init__(self, task=None) -> None: else: # 给定了task, 我们检查一下 from toolbox import get_conf - WHEN_TO_USE_PROXY, = get_conf('WHEN_TO_USE_PROXY') + WHEN_TO_USE_PROXY = get_conf('WHEN_TO_USE_PROXY') self.valid = (task in WHEN_TO_USE_PROXY) def __enter__(self): if not self.valid: return self from toolbox import get_conf - proxies, = get_conf('proxies') + proxies = get_conf('proxies') if 'no_proxy' in os.environ: os.environ.pop('no_proxy') if proxies is not None: if 'http' in proxies: os.environ['HTTP_PROXY'] = proxies['http'] @@ -1054,7 +1056,7 @@ def _singleton(*args, **kargs): """ ======================================================================== 第四部分 -接驳虚空终端: +接驳void-terminal: - set_conf: 在运行过程中动态地修改配置 - set_multi_conf: 在运行过程中动态地修改多个配置 - get_plugin_handle: 获取插件的句柄 @@ -1069,7 +1071,7 @@ def set_conf(key, value): read_single_conf_with_lru_cache.cache_clear() get_conf.cache_clear() os.environ[key] = str(value) - altered, = get_conf(key) + altered = get_conf(key) return altered def set_multi_conf(dic):