From 2c7bba5c63c1121e2b6723f6b3d7c5a78bf952b1 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sat, 23 Dec 2023 21:35:42 +0800 Subject: [PATCH] change dash scope api key check behavior --- config.py | 2 +- request_llms/bridge_qwen.py | 16 ++++++---------- request_llms/com_qwenapi.py | 13 +++++++++++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/config.py b/config.py index 17dac34a..861bbed9 100644 --- a/config.py +++ b/config.py @@ -108,7 +108,7 @@ QWEN_LOCAL_MODEL_SELECTION = "Qwen/Qwen-1_8B-Chat-Int8" # 接入通义千问在线大模型 https://dashscope.console.aliyun.com/ -DASHSCOPE_API_KEY = "此处填阿里灵积云API秘钥" # 阿里灵积云API_KEY +DASHSCOPE_API_KEY = "" # 阿里灵积云API_KEY # 百度千帆(LLM_MODEL="qianfan") diff --git a/request_llms/bridge_qwen.py b/request_llms/bridge_qwen.py index 583def8b..18877b94 100644 --- a/request_llms/bridge_qwen.py +++ b/request_llms/bridge_qwen.py @@ -5,16 +5,6 @@ from toolbox import check_packages, report_exception model_name = 'Qwen' -def validate_key(): - DASHSCOPE_API_KEY = get_conf("DASHSCOPE_API_KEY") - if DASHSCOPE_API_KEY == '': return False - return True - -if not validate_key(): - raise RuntimeError('请配置DASHSCOPE_API_KEY') -os.environ['DASHSCOPE_API_KEY'] = get_conf("DASHSCOPE_API_KEY") - - def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=[], console_slience=False): """ ⭐多线程方法 @@ -48,6 +38,12 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp chatbot=chatbot, history=history, delay=0) return + # 检查DASHSCOPE_API_KEY + if get_conf("DASHSCOPE_API_KEY") == "": + yield from update_ui_lastest_msg(f"请配置 DASHSCOPE_API_KEY。", + chatbot=chatbot, history=history, delay=0) + return + if additional_fn is not None: from core_functional import handle_core_functionality inputs, history = handle_core_functionality(additional_fn, inputs, history, chatbot) diff --git a/request_llms/com_qwenapi.py b/request_llms/com_qwenapi.py index 63ebdea2..5807600d 100644 --- a/request_llms/com_qwenapi.py +++ b/request_llms/com_qwenapi.py @@ -7,12 +7,21 @@ timeout_bot_msg = '[Local Message] Request timeout. Network error.' class QwenRequestInstance(): def __init__(self): - + import dashscope self.time_to_yield_event = threading.Event() self.time_to_exit_event = threading.Event() - self.result_buf = "" + def validate_key(): + DASHSCOPE_API_KEY = get_conf("DASHSCOPE_API_KEY") + if DASHSCOPE_API_KEY == '': return False + return True + + if not validate_key(): + raise RuntimeError('请配置 DASHSCOPE_API_KEY') + dashscope.api_key = get_conf("DASHSCOPE_API_KEY") + + def generate(self, inputs, llm_kwargs, history, system_prompt): # import _thread as thread from dashscope import Generation