From e64c26e6176ae5d09b7c37495a29224769e8c780 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sat, 28 Oct 2023 19:53:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_all.py | 2 +- request_llm/bridge_chatgpt.py | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 70e2c43..f85d1b6 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -505,7 +505,7 @@ if len(AZURE_CFG_ARRAY) > 0: } }) if azure_model_name not in AVAIL_LLM_MODELS: - azure_model_name += [azure_model_name] + AVAIL_LLM_MODELS += [azure_model_name] diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index f91eea5..9903da9 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -45,16 +45,18 @@ def decode_chunk(chunk): chunk_decoded = chunk.decode() chunkjson = None has_choices = False + choice_valid = False has_content = False has_role = False try: chunkjson = json.loads(chunk_decoded[6:]) - has_choices = ('choices' in chunkjson) and (len(chunkjson['choices']) > 0) - if has_choices: has_content = "content" in chunkjson['choices'][0]["delta"] - if has_choices: has_role = "role" in chunkjson['choices'][0]["delta"] + has_choices = 'choices' in chunkjson + if has_choices: choice_valid = (len(chunkjson['choices']) > 0) + if has_choices and choice_valid: has_content = "content" in chunkjson['choices'][0]["delta"] + if has_choices and choice_valid: has_role = "role" in chunkjson['choices'][0]["delta"] except: pass - return chunk_decoded, chunkjson, has_choices, has_content, has_role + return chunk_decoded, chunkjson, has_choices, choice_valid, has_content, has_role from functools import lru_cache @lru_cache(maxsize=32) @@ -64,7 +66,6 @@ def verify_endpoint(endpoint): """ if "你亲手写的api名称" in endpoint: raise ValueError("Endpoint不正确, 请检查AZURE_ENDPOINT的配置! 当前的Endpoint为:" + endpoint) - print(endpoint) return endpoint def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False): @@ -97,7 +98,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", if retry > MAX_RETRY: raise TimeoutError if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') - stream_response = response.iter_lines() + stream_response = response.iter_lines() result = '' json_data = None while True: @@ -227,7 +228,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp return # 提前读取一些信息 (用于判断异常) - chunk_decoded, chunkjson, has_choices, has_content, has_role = decode_chunk(chunk) + chunk_decoded, chunkjson, has_choices, choice_valid, has_content, has_role = decode_chunk(chunk) if is_head_of_the_stream and (r'"object":"error"' not in chunk_decoded) and (r"content" not in chunk_decoded): # 数据流的第一帧不携带content @@ -235,7 +236,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp if chunk: try: - if not has_choices: + if has_choices and not choice_valid: # 一些垃圾第三方接口的出现这样的错误 continue # 前者是API2D的结束条件,后者是OPENAI的结束条件 @@ -287,6 +288,8 @@ def handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg) chatbot[-1] = (chatbot[-1][0], "[Local Message] Your account is not active. OpenAI以账户失效为由, 拒绝服务." + openai_website) elif "associated with a deactivated account" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] You are associated with a deactivated account. OpenAI以账户失效为由, 拒绝服务." + openai_website) + elif "API key has been deactivated" in error_msg: + chatbot[-1] = (chatbot[-1][0], "[Local Message] API key has been deactivated. OpenAI以账户失效为由, 拒绝服务." + openai_website) elif "bad forward key" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] Bad forward key. API2D账户额度不足.") elif "Not enough point" in error_msg: From b14d4de0b1d6e653069a57c31c720980d49729e0 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sat, 28 Oct 2023 20:08:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=E9=BB=98=E8=AE=A4=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E6=8F=90=E7=A4=BA=E8=AF=8D=E8=BD=AC=E7=A7=BB=E5=88=B0?= =?UTF-8?q?Config=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++-- config.py | 4 ++++ docs/use_azure.md | 8 ++++---- main.py | 4 ++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 77ff15e..c93b57a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ > **Note** > -> 2023.10.8: Gradio, Pydantic依赖调整,已修改 `requirements.txt`。请及时**更新代码**,安装依赖时,请严格选择`requirements.txt`中**指定的版本**。 +> 2023.10.28: 紧急修复了若干问题,安装依赖时,请选择`requirements.txt`中**指定的版本**。 > > `pip install -r requirements.txt` @@ -310,6 +310,7 @@ Tip:不指定文件直接点击 `载入对话历史存档` 可以查看历史h ### II:版本: - version 3.60(todo): 优化虚空终端,引入code interpreter和更多插件 +- version 3.56: 支持动态追加基础功能按钮,新汇报PDF汇总页面 - version 3.55: 重构前端界面,引入悬浮窗口与菜单栏 - version 3.54: 新增动态代码解释器(Code Interpreter)(待完善) - version 3.53: 支持动态选择不同界面主题,提高稳定性&解决多用户冲突问题 @@ -344,7 +345,14 @@ GPT Academic开发者QQ群:`610599535` 1. `Chuanhu-Small-and-Beautiful` [网址](https://github.com/GaiZhenbiao/ChuanhuChatGPT/) -### IV:参考与学习 +### IV:本项目的开发分支 + +1. `master` 分支: 主分支,稳定版 + +2. `frontier` 分支: 开发分支,测试版 + + +### V:参考与学习 ``` 代码中参考了很多其他优秀项目中的设计,顺序不分先后: diff --git a/config.py b/config.py index 56c8ea3..abea7ea 100644 --- a/config.py +++ b/config.py @@ -49,6 +49,10 @@ THEME = "Default" AVAIL_THEMES = ["Default", "Chuanhu-Small-and-Beautiful", "High-Contrast", "Gstaff/Xkcd", "NoCrypt/Miku"] +# 默认的系统提示词(system prompt) +INIT_SYS_PROMPT = "Serve me as a writing and programming assistant." + + # 对话窗的高度 (仅在LAYOUT="TOP-DOWN"时生效) CHATBOT_HEIGHT = 1115 diff --git a/docs/use_azure.md b/docs/use_azure.md index 041bc52..0e192ba 100644 --- a/docs/use_azure.md +++ b/docs/use_azure.md @@ -7,25 +7,25 @@ ## 方法二(新方法,接入多个Azure模型,并支持动态切换) - 在方法一的基础上,注册并获取多组 AZURE_ENDPOINT,AZURE_API_KEY,AZURE_ENGINE -- 修改config中的AZURE_CFG_ARRAY配置项,按照格式填入多个Azure模型的配置,如下所示: +- 修改config中的AZURE_CFG_ARRAY和AVAIL_LLM_MODELS配置项,按照格式填入多个Azure模型的配置,如下所示: ``` AZURE_CFG_ARRAY = { - "azure-gpt-3.5": # 第一个模型,azure模型必须以"azure-"开头 + "azure-gpt-3.5": # 第一个模型,azure模型必须以"azure-"开头,注意您还需要将"azure-gpt-3.5"加入AVAIL_LLM_MODELS(模型下拉菜单) { "AZURE_ENDPOINT": "https://你亲手写的api名称.openai.azure.com/", "AZURE_API_KEY": "cccccccccccccccccccccccccccccccc", "AZURE_ENGINE": "填入你亲手写的部署名1", "AZURE_MODEL_MAX_TOKEN": 4096, }, - "azure-gpt-4": # 第二个模型,azure模型必须以"azure-"开头 + "azure-gpt-4": # 第二个模型,azure模型必须以"azure-"开头,注意您还需要将"azure-gpt-4"加入AVAIL_LLM_MODELS(模型下拉菜单) { "AZURE_ENDPOINT": "https://你亲手写的api名称.openai.azure.com/", "AZURE_API_KEY": "dddddddddddddddddddddddddddddddd", "AZURE_ENGINE": "填入你亲手写的部署名2", "AZURE_MODEL_MAX_TOKEN": 8192, }, - "azure-gpt-3.5-16k": # 第三个模型,azure模型必须以"azure-"开头 + "azure-gpt-3.5-16k": # 第三个模型,azure模型必须以"azure-"开头,注意您还需要将"azure-gpt-3.5-16k"加入AVAIL_LLM_MODELS(模型下拉菜单) { "AZURE_ENDPOINT": "https://你亲手写的api名称.openai.azure.com/", "AZURE_API_KEY": "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", diff --git a/main.py b/main.py index 9f38995..f091475 100644 --- a/main.py +++ b/main.py @@ -14,13 +14,13 @@ 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') # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT from check_proxy import get_current_version from themes.theme import adjust_theme, advanced_css, theme_declaration, load_dynamic_theme - initial_prompt = "Serve me as a writing and programming assistant." title_html = f"

GPT 学术优化 {get_current_version()}

{theme_declaration}" description = "Github源代码开源和更新[地址🚀](https://github.com/binary-husky/gpt_academic), " description += "感谢热情的[开发者们❤️](https://github.com/binary-husky/gpt_academic/graphs/contributors)." @@ -153,7 +153,7 @@ def main(): top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) max_length_sl = gr.Slider(minimum=256, maximum=1024*32, value=4096, step=128, interactive=True, label="Local LLM MaxLength",) - system_prompt = gr.Textbox(show_label=True, lines=2, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) + system_prompt = gr.Textbox(show_label=True, lines=2, placeholder=f"System Prompt", label="System prompt", value=INIT_SYS_PROMPT) with gr.Tab("界面外观", elem_id="interact-panel"): theme_dropdown = gr.Dropdown(AVAIL_THEMES, value=THEME, label="更换UI主题").style(container=False)