From 91609d6d39d67416fbc49d4736c643c5630deaff Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Sat, 15 Apr 2023 15:24:18 +0800 Subject: [PATCH 01/50] Rebase v3.0 --- .gitignore | 1 - Dockerfile+ChatGLM | 50 ++++++++ config.py | 5 +- crazy_functional.py | 7 +- crazy_functions/crazy_utils.py | 8 +- crazy_functions/代码重写为全英文_多线程.py | 2 +- crazy_functions/解析项目源代码.py | 2 +- crazy_functions/询问多个大语言模型.py | 28 +++++ main.py | 7 +- request_llm/README.md | 48 +++++--- request_llm/bridge_all.py | 135 +++++++++++++++++++++ request_llm/bridge_chatglm.py | 83 +++++++++++++ request_llm/bridge_tgui.py | 62 +++++----- request_llm/requirements_chatglm.txt | 6 + requirements.txt | 2 +- toolbox.py | 65 +--------- version | 4 +- 17 files changed, 397 insertions(+), 118 deletions(-) create mode 100644 Dockerfile+ChatGLM create mode 100644 crazy_functions/询问多个大语言模型.py create mode 100644 request_llm/bridge_all.py create mode 100644 request_llm/bridge_chatglm.py create mode 100644 request_llm/requirements_chatglm.txt diff --git a/.gitignore b/.gitignore index efe469e..213f3e7 100644 --- a/.gitignore +++ b/.gitignore @@ -55,7 +55,6 @@ coverage.xml *.pot github .github -.idea/ TEMP TRASH diff --git a/Dockerfile+ChatGLM b/Dockerfile+ChatGLM new file mode 100644 index 0000000..f99f2a6 --- /dev/null +++ b/Dockerfile+ChatGLM @@ -0,0 +1,50 @@ +# How to build | 如何构建: docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . +# How to run | 如何运行 (1) 直接运行: docker run --rm -it --net=host --gpus=all gpt-academic +# How to run | 如何运行 (2) 我想运行之前进容器做一些调整: docker run --rm -it --net=host --gpus=all gpt-academic bash + +# 从NVIDIA源,从而支持显卡运损(检查宿主的nvidia-smi中的cuda版本必须>=11.3) +FROM nvidia/cuda:11.3.1-runtime-ubuntu20.04 +ARG useProxyNetwork='' +RUN apt-get update +RUN apt-get install -y curl proxychains curl +RUN apt-get install -y git python python3 python-dev python3-dev --fix-missing + +# 配置代理网络(构建Docker镜像时使用) +# # comment out below if you do not need proxy network | 如果不需要翻墙 - 从此行向下删除 +RUN $useProxyNetwork curl cip.cc +RUN sed -i '$ d' /etc/proxychains.conf +RUN sed -i '$ d' /etc/proxychains.conf +RUN echo "socks5 127.0.0.1 10880" >> /etc/proxychains.conf +ARG useProxyNetwork=proxychains +# # comment out above if you do not need proxy network | 如果不需要翻墙 - 从此行向上删除 + + +# use python3 as the system default python +RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.8 + +# 下载分支 +WORKDIR /gpt +RUN $useProxyNetwork git clone https://github.com/binary-husky/chatgpt_academic.git -b v3.0 +WORKDIR /gpt/chatgpt_academic +RUN $useProxyNetwork python3 -m pip install -r requirements.txt +RUN $useProxyNetwork python3 -m pip install -r request_llm/requirements_chatglm.txt +RUN $useProxyNetwork python3 -m pip install torch --extra-index-url https://download.pytorch.org/whl/cu113 + +# 预热CHATGLM参数(非必要 可选步骤) +RUN echo ' \n\ +from transformers import AutoModel, AutoTokenizer \n\ +chatglm_tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) \n\ +chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float() ' >> warm_up_chatglm.py +RUN python3 -u warm_up_chatglm.py +RUN $useProxyNetwork git pull + +# 为chatgpt-academic配置代理和API-KEY (非必要 可选步骤) +RUN echo ' \n\ +API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \n\ +USE_PROXY = True \n\ +LLM_MODEL = "chatglm" \n\ +LOCAL_MODEL_DEVICE = "cuda" \n\ +proxies = { "http": "socks5h://localhost:10880", "https": "socks5h://localhost:10880", } ' >> config_private.py + +# 启动 +CMD ["python3", "-u", "main.py"] diff --git a/config.py b/config.py index 658de99..8db5d46 100644 --- a/config.py +++ b/config.py @@ -45,7 +45,10 @@ WEB_PORT = -1 MAX_RETRY = 2 # OpenAI模型选择是(gpt4现在只对申请成功的人开放) -LLM_MODEL = "gpt-3.5-turbo" +LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm", "tgui:anymodel@localhost:7865" + +# 本地LLM模型如ChatGLM的执行方式 CPU/GPU +LOCAL_MODEL_DEVICE = "cpu" # 可选 "cuda" # OpenAI的API_URL API_URL = "https://api.openai.com/v1/chat/completions" diff --git a/crazy_functional.py b/crazy_functional.py index f701167..96c16b8 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -16,15 +16,20 @@ def get_crazy_functions(): from crazy_functions.高级功能函数模板 import 高阶功能模板函数 from crazy_functions.代码重写为全英文_多线程 import 全项目切换英文 from crazy_functions.Latex全文润色 import Latex英文润色 + from crazy_functions.询问多个大语言模型 import 同时问询 from crazy_functions.解析项目源代码 import 解析一个Lua项目 function_plugins = { - + "询问多个GPT模型": { + "Color": "stop", # 按钮颜色 + "Function": HotReload(同时问询) + }, "解析整个Python项目": { "Color": "stop", # 按钮颜色 "Function": HotReload(解析一个Python项目) }, "解析整个C++项目头文件": { "Color": "stop", # 按钮颜色 + "AsButton": False, # 加入下拉菜单中 "Function": HotReload(解析一个C项目的头文件) }, "解析整个C++项目(.cpp/.hpp/.c/.h)": { diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 189d948..cc43b53 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -61,7 +61,7 @@ def request_gpt_model_in_new_thread_with_ui_alive( """ import time from concurrent.futures import ThreadPoolExecutor - from request_llm.bridge_chatgpt import predict_no_ui_long_connection + from request_llm.bridge_all import predict_no_ui_long_connection # 用户反馈 chatbot.append([inputs_show_user, ""]) yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面 @@ -167,13 +167,17 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( """ import time, random from concurrent.futures import ThreadPoolExecutor - from request_llm.bridge_chatgpt import predict_no_ui_long_connection + from request_llm.bridge_all import predict_no_ui_long_connection 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') except: max_workers = 8 if max_workers <= 0 or max_workers >= 20: max_workers = 8 + # 屏蔽掉 chatglm的多线程,可能会导致严重卡顿 + if not llm_kwargs['llm_model'].startswith('gpt-'): + max_workers = 1 + executor = ThreadPoolExecutor(max_workers=max_workers) n_frag = len(inputs_array) # 用户反馈 diff --git a/crazy_functions/代码重写为全英文_多线程.py b/crazy_functions/代码重写为全英文_多线程.py index 40bc45d..a09a3f5 100644 --- a/crazy_functions/代码重写为全英文_多线程.py +++ b/crazy_functions/代码重写为全英文_多线程.py @@ -1,5 +1,5 @@ import threading -from request_llm.bridge_chatgpt import predict_no_ui_long_connection +from request_llm.bridge_all import predict_no_ui_long_connection from toolbox import update_ui from toolbox import CatchException, write_results_to_file, report_execption from .crazy_utils import breakdown_txt_to_satisfy_token_limit diff --git a/crazy_functions/解析项目源代码.py b/crazy_functions/解析项目源代码.py index 3af7497..9b476cb 100644 --- a/crazy_functions/解析项目源代码.py +++ b/crazy_functions/解析项目源代码.py @@ -12,7 +12,7 @@ def 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, sys_prompt_array = [] report_part_1 = [] - assert len(file_manifest) <= 1024, "源文件太多(超过1024个), 请缩减输入文件的数量。或者,您也可以选择删除此行警告,并修改代码拆分file_manifest列表,从而实现分批次处理。" + assert len(file_manifest) <= 512, "源文件太多(超过512个), 请缩减输入文件的数量。或者,您也可以选择删除此行警告,并修改代码拆分file_manifest列表,从而实现分批次处理。" ############################## <第一步,逐个文件分析,多线程> ################################## for index, fp in enumerate(file_manifest): # 读取文件 diff --git a/crazy_functions/询问多个大语言模型.py b/crazy_functions/询问多个大语言模型.py new file mode 100644 index 0000000..a3c98c1 --- /dev/null +++ b/crazy_functions/询问多个大语言模型.py @@ -0,0 +1,28 @@ +from toolbox import CatchException, update_ui +from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive +import datetime +@CatchException +def 同时问询(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + """ + txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径 + llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行 + plugin_kwargs 插件模型的参数,如温度和top_p等,一般原样传递下去就行 + chatbot 聊天显示框的句柄,用于显示给用户 + history 聊天历史,前情提要 + system_prompt 给gpt的静默提醒 + web_port 当前软件运行的端口号 + """ + history = [] # 清空历史,以免输入溢出 + chatbot.append((txt, "正在同时咨询ChatGPT和ChatGLM……")) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新 + + llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo' + gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( + inputs=txt, inputs_show_user=txt, + llm_kwargs=llm_kwargs, chatbot=chatbot, history=history, + sys_prompt=system_prompt + ) + + history.append(txt) + history.append(gpt_say) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新 \ No newline at end of file diff --git a/main.py b/main.py index 2e4e275..d8257a2 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 import gradio as gr -from request_llm.bridge_chatgpt import predict +from request_llm.bridge_all import predict from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 @@ -97,7 +97,10 @@ with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled= system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) 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=4096, value=512, step=1, interactive=True, label="MaxLength",) checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") + md_dropdown = gr.Dropdown(["gpt-3.5-turbo", "chatglm"], value=LLM_MODEL, label="").style(container=False) + gr.Markdown(description) with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary: with gr.Row(): @@ -118,7 +121,7 @@ with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled= return ret checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] ) # 整理反复出现的控件句柄组合 - input_combo = [cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt] + input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt] output_combo = [cookies, chatbot, history, status] predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo) # 提交按钮、重置按钮 diff --git a/request_llm/README.md b/request_llm/README.md index 3277e4a..973adea 100644 --- a/request_llm/README.md +++ b/request_llm/README.md @@ -1,35 +1,53 @@ # 如何使用其他大语言模型(v3.0分支测试中) -## 1. 先运行text-generation +## ChatGLM + +- 安装依赖 `pip install -r request_llm/requirements_chatglm.txt` +- 修改配置,在config.py中将LLM_MODEL的值改为"chatglm" + ``` sh -# 下载模型( text-generation 这么牛的项目,别忘了给人家star ) +LLM_MODEL = "chatglm" +``` +- 运行! +``` sh +`python main.py` +``` + + +--- +## Text-Generation-UI (TGUI) + +### 1. 部署TGUI +``` sh +# 1 下载模型 git clone https://github.com/oobabooga/text-generation-webui.git - -# 安装text-generation的额外依赖 -pip install accelerate bitsandbytes flexgen gradio llamacpp markdown numpy peft requests rwkv safetensors sentencepiece tqdm datasets git+https://github.com/huggingface/transformers - -# 切换路径 +# 2 这个仓库的最新代码有问题,回滚到几周之前 +git reset --hard fcda3f87767e642d1c0411776e549e1d3894843d +# 3 切换路径 cd text-generation-webui - -# 下载模型 +# 4 安装text-generation的额外依赖 +pip install accelerate bitsandbytes flexgen gradio llamacpp markdown numpy peft requests rwkv safetensors sentencepiece tqdm datasets git+https://github.com/huggingface/transformers +# 5 下载模型 python download-model.py facebook/galactica-1.3b # 其他可选如 facebook/opt-1.3b +# facebook/galactica-1.3b # facebook/galactica-6.7b # facebook/galactica-120b # facebook/pygmalion-1.3b 等 # 详情见 https://github.com/oobabooga/text-generation-webui -# 启动text-generation,注意把模型的斜杠改成下划线 -python server.py --cpu --listen --listen-port 7860 --model facebook_galactica-1.3b +# 6 启动text-generation +python server.py --cpu --listen --listen-port 7865 --model facebook_galactica-1.3b ``` -## 2. 修改config.py +### 2. 修改config.py + ``` sh -# LLM_MODEL格式较复杂 TGUI:[模型]@[ws地址]:[ws端口] , 端口要和上面给定的端口一致 -LLM_MODEL = "TGUI:galactica-1.3b@localhost:7860" +# LLM_MODEL格式: tgui:[模型]@[ws地址]:[ws端口] , 端口要和上面给定的端口一致 +LLM_MODEL = "tgui:galactica-1.3b@localhost:7860" ``` -## 3. 运行! +### 3. 运行! ``` sh cd chatgpt-academic python main.py diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py new file mode 100644 index 0000000..f02a1c8 --- /dev/null +++ b/request_llm/bridge_all.py @@ -0,0 +1,135 @@ + +""" + 该文件中主要包含2个函数 + + 不具备多线程能力的函数: + 1. predict: 正常对话时使用,具备完备的交互功能,不可多线程 + + 具备多线程调用能力的函数 + 2. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程 +""" + +from concurrent.futures import ThreadPoolExecutor + +from .bridge_chatgpt import predict_no_ui_long_connection as chatgpt_noui +from .bridge_chatgpt import predict as chatgpt_ui + +from .bridge_chatglm import predict_no_ui_long_connection as chatglm_noui +from .bridge_chatglm import predict as chatglm_ui + +from .bridge_tgui import predict_no_ui_long_connection as tgui_noui +from .bridge_tgui import predict as tgui_ui + +methods = { + "openai-no-ui": chatgpt_noui, + "openai-ui": chatgpt_ui, + + "chatglm-no-ui": chatglm_noui, + "chatglm-ui": chatglm_ui, + + "tgui-no-ui": tgui_noui, + "tgui-ui": tgui_ui, +} + +def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience=False): + """ + 发送至LLM,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 + inputs: + 是本次问询的输入 + sys_prompt: + 系统静默prompt + llm_kwargs: + LLM的内部调优参数 + history: + 是之前的对话列表 + observe_window = None: + 用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗 + """ + import threading, time, copy + + model = llm_kwargs['llm_model'] + n_model = 1 + if '&' not in model: + assert not model.startswith("tgui"), "TGUI不支持函数插件的实现" + + # 如果只询问1个大语言模型: + if model.startswith('gpt'): + method = methods['openai-no-ui'] + elif model == 'chatglm': + method = methods['chatglm-no-ui'] + elif model.startswith('tgui'): + method = methods['tgui-no-ui'] + return method(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience) + else: + # 如果同时询问多个大语言模型: + executor = ThreadPoolExecutor(max_workers=16) + models = model.split('&') + n_model = len(models) + + window_len = len(observe_window) + if window_len==0: + window_mutex = [[] for _ in range(n_model)] + [True] + elif window_len==1: + window_mutex = [[""] for _ in range(n_model)] + [True] + elif window_len==2: + window_mutex = [["", time.time()] for _ in range(n_model)] + [True] + + futures = [] + for i in range(n_model): + model = models[i] + if model.startswith('gpt'): + method = methods['openai-no-ui'] + elif model == 'chatglm': + method = methods['chatglm-no-ui'] + elif model.startswith('tgui'): + method = methods['tgui-no-ui'] + llm_kwargs_feedin = copy.deepcopy(llm_kwargs) + llm_kwargs_feedin['llm_model'] = model + future = executor.submit(method, inputs, llm_kwargs_feedin, history, sys_prompt, window_mutex[i], console_slience) + futures.append(future) + + def mutex_manager(window_mutex, observe_window): + while True: + time.sleep(0.2) + if not window_mutex[-1]: break + # 看门狗(watchdog) + for i in range(n_model): + window_mutex[i][1] = observe_window[1] + # 观察窗(window) + chat_string = [] + for i in range(n_model): + chat_string.append( f"[{str(models[i])} 说]: {window_mutex[i][0]}" ) + res = '\n\n---\n\n'.join(chat_string) + # # # # # # # # # # # + observe_window[0] = res + + t_model = threading.Thread(target=mutex_manager, args=(window_mutex, observe_window), daemon=True) + t_model.start() + + return_string_collect = [] + for i, future in enumerate(futures): # wait and get + return_string_collect.append( f"[{str(models[i])} 说]: {future.result()}" ) + window_mutex[-1] = False # stop mutex thread + res = '\n\n---\n\n'.join(return_string_collect) + return res + + +def predict(inputs, llm_kwargs, *args, **kwargs): + """ + 发送至LLM,流式获取输出。 + 用于基础的对话功能。 + inputs 是本次问询的输入 + top_p, temperature是LLM的内部调优参数 + history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误) + chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 + additional_fn代表点击的哪个按钮,按钮见functional.py + """ + if llm_kwargs['llm_model'].startswith('gpt'): + method = methods['openai-ui'] + elif llm_kwargs['llm_model'] == 'chatglm': + method = methods['chatglm-ui'] + elif llm_kwargs['llm_model'].startswith('tgui'): + method = methods['tgui-ui'] + + yield from method(inputs, llm_kwargs, *args, **kwargs) + diff --git a/request_llm/bridge_chatglm.py b/request_llm/bridge_chatglm.py new file mode 100644 index 0000000..d6f5eec --- /dev/null +++ b/request_llm/bridge_chatglm.py @@ -0,0 +1,83 @@ + +from transformers import AutoModel, AutoTokenizer +import time +import importlib +from toolbox import update_ui, get_conf + + +global chatglm_model, chatglm_tokenizer + +chatglm_model = None +chatglm_tokenizer = None + +def model_loader(): + global chatglm_model, chatglm_tokenizer + if chatglm_tokenizer is None: + chatglm_tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) + if chatglm_model is None: # 尚未加载 + device, = get_conf('LOCAL_MODEL_DEVICE') + if device=='cpu': + chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float() + else: + chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda() + chatglm_model = chatglm_model.eval() + chatglm_model = chatglm_model.eval() + +def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False): + """ + 函数的说明请见 request_llm/bridge_all.py + """ + global chatglm_model, chatglm_tokenizer + if chatglm_model is None: + observe_window[0] = "ChatGLM尚未加载,加载需要一段时间 ……" + + model_loader() + # chatglm 没有 sys_prompt 接口,因此把prompt加入 history + history_feedin = [] + for i in range(len(history)//2): + history_feedin.append(["What can I do?", sys_prompt] ) + history_feedin.append([history[2*i], history[2*i+1]] ) + + watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 + response = "" + for response, history in chatglm_model.stream_chat(chatglm_tokenizer, inputs, history=history_feedin, max_length=llm_kwargs['max_length'], + top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): + # 观测窗,把已经获取的数据显示出去 + observe_window[0] = response + # 看门狗 (watchdog),如果超过期限没有喂狗,则终止 + if len(observe_window) >= 2: + if (time.time()-observe_window[1]) > watch_dog_patience: + raise RuntimeError("程序终止。") + # if not console_slience: + # print(response) + return response + + +def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): + """ + 函数的说明请见 request_llm/bridge_all.py + """ + global chatglm_model, chatglm_tokenizer + chatbot.append((inputs, "")) + if chatglm_model is None: + chatbot[-1] = (inputs, "ChatGLM尚未加载,加载需要一段时间 ……") + yield from update_ui(chatbot=chatbot, history=[]) + model_loader() + + if additional_fn is not None: + import core_functional + importlib.reload(core_functional) # 热更新prompt + core_functional = core_functional.get_core_functions() + if "PreProcess" in core_functional[additional_fn]: inputs = core_functional[additional_fn]["PreProcess"](inputs) # 获取预处理函数(如果有的话) + inputs = core_functional[additional_fn]["Prefix"] + inputs + core_functional[additional_fn]["Suffix"] + + + history_feedin = [] + for i in range(len(history)//2): + history_feedin.append(["What can I do?", system_prompt] ) + history_feedin.append([history[2*i], history[2*i+1]] ) + + for response, history in chatglm_model.stream_chat(chatglm_tokenizer, inputs, history=history_feedin, max_length=llm_kwargs['max_length'], + top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): + chatbot[-1] = (inputs, response) + yield from update_ui(chatbot=chatbot, history=history) \ No newline at end of file diff --git a/request_llm/bridge_tgui.py b/request_llm/bridge_tgui.py index 22a4075..fcf852f 100644 --- a/request_llm/bridge_tgui.py +++ b/request_llm/bridge_tgui.py @@ -13,23 +13,18 @@ import time import threading import importlib from toolbox import get_conf, update_ui -LLM_MODEL, = get_conf('LLM_MODEL') -# "TGUI:galactica-1.3b@localhost:7860" -model_name, addr_port = LLM_MODEL.split('@') -assert ':' in addr_port, "LLM_MODEL 格式不正确!" + LLM_MODEL -addr, port = addr_port.split(':') def random_hash(): letters = string.ascii_lowercase + string.digits return ''.join(random.choice(letters) for i in range(9)) -async def run(context, max_token=512): +async def run(context, max_token, temperature, top_p, addr, port): params = { 'max_new_tokens': max_token, 'do_sample': True, - 'temperature': 0.5, - 'top_p': 0.9, + 'temperature': temperature, + 'top_p': top_p, 'typical_p': 1, 'repetition_penalty': 1.05, 'encoder_repetition_penalty': 1.0, @@ -90,7 +85,7 @@ async def run(context, max_token=512): -def predict_tgui(inputs, top_p, temperature, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): +def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): """ 发送至chatGPT,流式获取输出。 用于基础的对话功能。 @@ -108,18 +103,26 @@ def predict_tgui(inputs, top_p, temperature, chatbot, history=[], system_prompt= inputs = core_functional[additional_fn]["Prefix"] + inputs + core_functional[additional_fn]["Suffix"] raw_input = "What I would like to say is the following: " + inputs - logging.info(f'[raw_input] {raw_input}') history.extend([inputs, ""]) chatbot.append([inputs, ""]) yield from update_ui(chatbot=chatbot, history=history, msg="等待响应") # 刷新界面 - prompt = inputs + prompt = raw_input tgui_say = "" + model_name, addr_port = llm_kwargs['llm_model'].split('@') + assert ':' in addr_port, "LLM_MODEL 格式不正确!" + llm_kwargs['llm_model'] + addr, port = addr_port.split(':') + + mutable = ["", time.time()] def run_coorotine(mutable): async def get_result(mutable): - async for response in run(prompt): + # "tgui:galactica-1.3b@localhost:7860" + + async for response in run(context=prompt, max_token=llm_kwargs['max_length'], + temperature=llm_kwargs['temperature'], + top_p=llm_kwargs['top_p'], addr=addr, port=port): print(response[len(mutable[0]):]) mutable[0] = response if (time.time() - mutable[1]) > 3: @@ -140,28 +143,29 @@ def predict_tgui(inputs, top_p, temperature, chatbot, history=[], system_prompt= chatbot[-1] = (history[-2], history[-1]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 - logging.info(f'[response] {tgui_say}') -def predict_tgui_no_ui(inputs, top_p, temperature, history=[], sys_prompt=""): +def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience=False): raw_input = "What I would like to say is the following: " + inputs - prompt = inputs + prompt = raw_input tgui_say = "" - mutable = ["", time.time()] - def run_coorotine(mutable): - async def get_result(mutable): - async for response in run(prompt, max_token=20): - print(response[len(mutable[0]):]) - mutable[0] = response - if (time.time() - mutable[1]) > 3: + model_name, addr_port = llm_kwargs['llm_model'].split('@') + assert ':' in addr_port, "LLM_MODEL 格式不正确!" + llm_kwargs['llm_model'] + addr, port = addr_port.split(':') + + + def run_coorotine(observe_window): + async def get_result(observe_window): + async for response in run(context=prompt, max_token=llm_kwargs['max_length'], + temperature=llm_kwargs['temperature'], + top_p=llm_kwargs['top_p'], addr=addr, port=port): + print(response[len(observe_window[0]):]) + observe_window[0] = response + if (time.time() - observe_window[1]) > 5: print('exit when no listener') break - asyncio.run(get_result(mutable)) - thread_listen = threading.Thread(target=run_coorotine, args=(mutable,)) + asyncio.run(get_result(observe_window)) + thread_listen = threading.Thread(target=run_coorotine, args=(observe_window,)) thread_listen.start() - while thread_listen.is_alive(): - time.sleep(1) - mutable[1] = time.time() - tgui_say = mutable[0] - return tgui_say + return observe_window[0] diff --git a/request_llm/requirements_chatglm.txt b/request_llm/requirements_chatglm.txt new file mode 100644 index 0000000..fa049ca --- /dev/null +++ b/request_llm/requirements_chatglm.txt @@ -0,0 +1,6 @@ +protobuf +transformers==4.27.1 +cpm_kernels +torch>=1.10 +mdtex2html +sentencepiece \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1cee117..910be08 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -gradio==3.25.0 +gradio>=3.25.0 tiktoken>=0.3.3 requests[socks] transformers diff --git a/toolbox.py b/toolbox.py index 3ced653..0dd035a 100644 --- a/toolbox.py +++ b/toolbox.py @@ -27,7 +27,7 @@ def ArgsGeneralWrapper(f): """ 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。 """ - def decorated(cookies, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args): + def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args): txt_passon = txt if txt == "" and txt2 != "": txt_passon = txt2 # 引入一个有cookie的chatbot @@ -37,8 +37,9 @@ def ArgsGeneralWrapper(f): }) llm_kwargs = { 'api_key': cookies['api_key'], - 'llm_model': cookies['llm_model'], + 'llm_model': llm_model, 'top_p':top_p, + 'max_length': max_length, 'temperature':temperature, } plugin_kwargs = { @@ -75,66 +76,6 @@ def get_reduce_token_percent(text): except: return 0.5, '不详' -def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, llm_kwargs, history=[], sys_prompt='', long_connection=True): - """ - * 此函数未来将被弃用(替代函数 request_gpt_model_in_new_thread_with_ui_alive 文件 chatgpt_academic/crazy_functions/crazy_utils) - - 调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断 - i_say: 当前输入 - i_say_show_user: 显示到对话界面上的当前输入,例如,输入整个文件时,你绝对不想把文件的内容都糊到对话界面上 - chatbot: 对话界面句柄 - top_p, temperature: gpt参数 - history: gpt参数 对话历史 - sys_prompt: gpt参数 sys_prompt - long_connection: 是否采用更稳定的连接方式(推荐)(已弃用) - """ - import time - from request_llm.bridge_chatgpt import predict_no_ui_long_connection - from toolbox import get_conf - TIMEOUT_SECONDS, MAX_RETRY = get_conf('TIMEOUT_SECONDS', 'MAX_RETRY') - # 多线程的时候,需要一个mutable结构在不同线程之间传递信息 - # list就是最简单的mutable结构,我们第一个位置放gpt输出,第二个位置传递报错信息 - mutable = [None, ''] - # multi-threading worker - - def mt(i_say, history): - while True: - try: - mutable[0] = predict_no_ui_long_connection( - inputs=i_say, llm_kwargs=llm_kwargs, history=history, sys_prompt=sys_prompt) - - except ConnectionAbortedError as token_exceeded_error: - # 尝试计算比例,尽可能多地保留文本 - p_ratio, n_exceed = get_reduce_token_percent( - str(token_exceeded_error)) - if len(history) > 0: - history = [his[int(len(his) * p_ratio):] - for his in history if his is not None] - else: - i_say = i_say[: int(len(i_say) * p_ratio)] - mutable[1] = f'警告,文本过长将进行截断,Token溢出数:{n_exceed},截断比例:{(1-p_ratio):.0%}。' - except TimeoutError as e: - mutable[0] = '[Local Message] 请求超时。' - raise TimeoutError - except Exception as e: - mutable[0] = f'[Local Message] 异常:{str(e)}.' - raise RuntimeError(f'[Local Message] 异常:{str(e)}.') - # 创建新线程发出http请求 - thread_name = threading.Thread(target=mt, args=(i_say, history)) - thread_name.start() - # 原来的线程则负责持续更新UI,实现一个超时倒计时,并等待新线程的任务完成 - cnt = 0 - while thread_name.is_alive(): - cnt += 1 - chatbot[-1] = (i_say_show_user, - f"[Local Message] {mutable[1]}waiting gpt response {cnt}/{TIMEOUT_SECONDS*2*(MAX_RETRY+1)}"+''.join(['.']*(cnt % 4))) - yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 - time.sleep(1) - # 把gpt的输出从mutable中取出来 - gpt_say = mutable[0] - if gpt_say == '[Local Message] Failed with timeout.': - raise TimeoutError - return gpt_say def write_results_to_file(history, file_name=None): diff --git a/version b/version index e1a3466..59e288a 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { - "version": 2.68, + "version": 3.0, "show_feature": true, - "new_feature": "改善理解pdf(chatpdf)功能 <-> 修复读取罕见字符的BUG <-> 如果一键更新失败,可前往github手动更新" + "new_feature": "支持ChatGLM <-> 支持多LLM模型同时对话" } From 294ac338bdb8287fad25ceecf35efaebdaf07e8a Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Sat, 15 Apr 2023 15:27:07 +0800 Subject: [PATCH 02/50] move files --- Dockerfile+ChatGLM => docs/Dockerfile+ChatGLM | 0 {img => docs}/README_EN.md | 0 {img => docs}/demo.jpg | Bin {img => docs}/demo2.jpg | Bin self_analysis.md => docs/self_analysis.md | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename Dockerfile+ChatGLM => docs/Dockerfile+ChatGLM (100%) rename {img => docs}/README_EN.md (100%) rename {img => docs}/demo.jpg (100%) rename {img => docs}/demo2.jpg (100%) rename self_analysis.md => docs/self_analysis.md (100%) diff --git a/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM similarity index 100% rename from Dockerfile+ChatGLM rename to docs/Dockerfile+ChatGLM diff --git a/img/README_EN.md b/docs/README_EN.md similarity index 100% rename from img/README_EN.md rename to docs/README_EN.md diff --git a/img/demo.jpg b/docs/demo.jpg similarity index 100% rename from img/demo.jpg rename to docs/demo.jpg diff --git a/img/demo2.jpg b/docs/demo2.jpg similarity index 100% rename from img/demo2.jpg rename to docs/demo2.jpg diff --git a/self_analysis.md b/docs/self_analysis.md similarity index 100% rename from self_analysis.md rename to docs/self_analysis.md From 6aba339538103c15d1b22fb616fb5f66557be716 Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Sat, 15 Apr 2023 19:09:03 +0800 Subject: [PATCH 03/50] =?UTF-8?q?ChatGLM=E6=94=B9=E6=88=90=E5=A4=9A?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/crazy_utils.py | 2 +- crazy_functions/询问多个大语言模型.py | 3 +- docs/Dockerfile+ChatGLM | 2 +- main.py | 327 +++++++++++++------------- request_llm/bridge_all.py | 48 +++- request_llm/bridge_chatglm.py | 98 +++++--- version | 2 +- 7 files changed, 270 insertions(+), 212 deletions(-) diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index cc43b53..b255706 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -66,7 +66,7 @@ def request_gpt_model_in_new_thread_with_ui_alive( chatbot.append([inputs_show_user, ""]) yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面 executor = ThreadPoolExecutor(max_workers=16) - mutable = ["", time.time()] + mutable = ["", time.time(), ""] def _req_gpt(inputs, history, sys_prompt): retry_op = retry_times_at_unknown_error exceeded_cnt = 0 diff --git a/crazy_functions/询问多个大语言模型.py b/crazy_functions/询问多个大语言模型.py index a3c98c1..55bfb41 100644 --- a/crazy_functions/询问多个大语言模型.py +++ b/crazy_functions/询问多个大语言模型.py @@ -20,7 +20,8 @@ def 同时问询(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=txt, inputs_show_user=txt, llm_kwargs=llm_kwargs, chatbot=chatbot, history=history, - sys_prompt=system_prompt + sys_prompt=system_prompt, + retry_times_at_unknown_error=0 ) history.append(txt) diff --git a/docs/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM index f99f2a6..197ca1a 100644 --- a/docs/Dockerfile+ChatGLM +++ b/docs/Dockerfile+ChatGLM @@ -24,7 +24,7 @@ RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.8 # 下载分支 WORKDIR /gpt -RUN $useProxyNetwork git clone https://github.com/binary-husky/chatgpt_academic.git -b v3.0 +RUN $useProxyNetwork git clone https://github.com/binary-husky/chatgpt_academic.git -b v3.1 WORKDIR /gpt/chatgpt_academic RUN $useProxyNetwork python3 -m pip install -r requirements.txt RUN $useProxyNetwork python3 -m pip install -r request_llm/requirements_chatglm.txt diff --git a/main.py b/main.py index d8257a2..db35c15 100644 --- a/main.py +++ b/main.py @@ -1,177 +1,182 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 -import gradio as gr -from request_llm.bridge_all import predict -from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith -# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 -proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY = \ - get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY') +def main(): + import gradio as gr + from request_llm.bridge_all import predict + from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith + # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 + proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY = \ + get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY') -# 如果WEB_PORT是-1, 则随机选取WEB端口 -PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT -if not AUTHENTICATION: AUTHENTICATION = None + # 如果WEB_PORT是-1, 则随机选取WEB端口 + PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT + if not AUTHENTICATION: AUTHENTICATION = None -from check_proxy import get_current_version -initial_prompt = "Serve me as a writing and programming assistant." -title_html = f"

ChatGPT 学术优化 {get_current_version()}

" -description = """代码开源和更新[地址🚀](https://github.com/binary-husky/chatgpt_academic),感谢热情的[开发者们❤️](https://github.com/binary-husky/chatgpt_academic/graphs/contributors)""" + from check_proxy import get_current_version + initial_prompt = "Serve me as a writing and programming assistant." + title_html = f"

ChatGPT 学术优化 {get_current_version()}

" + description = """代码开源和更新[地址🚀](https://github.com/binary-husky/chatgpt_academic),感谢热情的[开发者们❤️](https://github.com/binary-husky/chatgpt_academic/graphs/contributors)""" -# 问询记录, python 版本建议3.9+(越新越好) -import logging -os.makedirs("gpt_log", exist_ok=True) -try:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, encoding="utf-8") -except:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO) -print("所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!") + # 问询记录, python 版本建议3.9+(越新越好) + import logging + os.makedirs("gpt_log", exist_ok=True) + try:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, encoding="utf-8") + except:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO) + print("所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!") -# 一些普通功能模块 -from core_functional import get_core_functions -functional = get_core_functions() + # 一些普通功能模块 + from core_functional import get_core_functions + functional = get_core_functions() -# 高级函数插件 -from crazy_functional import get_crazy_functions -crazy_fns = get_crazy_functions() + # 高级函数插件 + from crazy_functional import get_crazy_functions + crazy_fns = get_crazy_functions() -# 处理markdown文本格式的转变 -gr.Chatbot.postprocess = format_io + # 处理markdown文本格式的转变 + gr.Chatbot.postprocess = format_io -# 做一些外观色彩上的调整 -from theme import adjust_theme, advanced_css -set_theme = adjust_theme() + # 做一些外观色彩上的调整 + from theme import adjust_theme, advanced_css + set_theme = adjust_theme() -# 代理与自动更新 -from check_proxy import check_proxy, auto_update -proxy_info = check_proxy(proxies) + # 代理与自动更新 + from check_proxy import check_proxy, auto_update + proxy_info = check_proxy(proxies) -gr_L1 = lambda: gr.Row().style() -gr_L2 = lambda scale: gr.Column(scale=scale) -if LAYOUT == "TOP-DOWN": - gr_L1 = lambda: DummyWith() - gr_L2 = lambda scale: gr.Row() - CHATBOT_HEIGHT /= 2 + gr_L1 = lambda: gr.Row().style() + gr_L2 = lambda scale: gr.Column(scale=scale) + if LAYOUT == "TOP-DOWN": + gr_L1 = lambda: DummyWith() + gr_L2 = lambda scale: gr.Row() + CHATBOT_HEIGHT /= 2 -cancel_handles = [] -with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo: - gr.HTML(title_html) - cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL}) - with gr_L1(): - with gr_L2(scale=2): - chatbot = gr.Chatbot() - chatbot.style(height=CHATBOT_HEIGHT) - history = gr.State([]) - with gr_L2(scale=1): - with gr.Accordion("输入区", open=True) as area_input_primary: - with gr.Row(): - txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False) - with gr.Row(): - submitBtn = gr.Button("提交", variant="primary") - with gr.Row(): - resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") - stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") - with gr.Row(): - status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}") - with gr.Accordion("基础功能区", open=True) as area_basic_fn: - with gr.Row(): - for k in functional: - variant = functional[k]["Color"] if "Color" in functional[k] else "secondary" - functional[k]["Button"] = gr.Button(k, variant=variant) - with gr.Accordion("函数插件区", open=True) as area_crazy_fn: - with gr.Row(): - gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.") - with gr.Row(): - for k in crazy_fns: - if not crazy_fns[k].get("AsButton", True): continue - variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary" - crazy_fns[k]["Button"] = gr.Button(k, variant=variant) - crazy_fns[k]["Button"].style(size="sm") - with gr.Row(): - with gr.Accordion("更多函数插件", open=True): - dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)] - with gr.Column(scale=1): - dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False) - with gr.Column(scale=1): - switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary") - with gr.Row(): - with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up: - file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple") - with gr.Accordion("展开SysPrompt & 交互界面布局 & Github地址", open=(LAYOUT == "TOP-DOWN")): - system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) - 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=4096, value=512, step=1, interactive=True, label="MaxLength",) - checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") - md_dropdown = gr.Dropdown(["gpt-3.5-turbo", "chatglm"], value=LLM_MODEL, label="").style(container=False) + cancel_handles = [] + with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo: + gr.HTML(title_html) + cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL}) + with gr_L1(): + with gr_L2(scale=2): + chatbot = gr.Chatbot() + chatbot.style(height=CHATBOT_HEIGHT) + history = gr.State([]) + with gr_L2(scale=1): + with gr.Accordion("输入区", open=True) as area_input_primary: + with gr.Row(): + txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False) + with gr.Row(): + submitBtn = gr.Button("提交", variant="primary") + with gr.Row(): + resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") + stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") + with gr.Row(): + status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}") + with gr.Accordion("基础功能区", open=True) as area_basic_fn: + with gr.Row(): + for k in functional: + variant = functional[k]["Color"] if "Color" in functional[k] else "secondary" + functional[k]["Button"] = gr.Button(k, variant=variant) + with gr.Accordion("函数插件区", open=True) as area_crazy_fn: + with gr.Row(): + gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.") + with gr.Row(): + for k in crazy_fns: + if not crazy_fns[k].get("AsButton", True): continue + variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary" + crazy_fns[k]["Button"] = gr.Button(k, variant=variant) + crazy_fns[k]["Button"].style(size="sm") + with gr.Row(): + with gr.Accordion("更多函数插件", open=True): + dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)] + with gr.Column(scale=1): + dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False) + with gr.Column(scale=1): + switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary") + with gr.Row(): + with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up: + file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple") + with gr.Accordion("展开SysPrompt & 交互界面布局 & Github地址", open=(LAYOUT == "TOP-DOWN")): + system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) + 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=4096, value=512, step=1, interactive=True, label="MaxLength",) + checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") + md_dropdown = gr.Dropdown(["gpt-3.5-turbo", "chatglm"], value=LLM_MODEL, label="").style(container=False) - gr.Markdown(description) - with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary: - with gr.Row(): - txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False) - with gr.Row(): - submitBtn2 = gr.Button("提交", variant="primary") - with gr.Row(): - resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") - stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") - # 功能区显示开关与功能区的互动 - def fn_area_visibility(a): - ret = {} - ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))}) - ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))}) - ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))}) - ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))}) - if "底部输入区" in a: ret.update({txt: gr.update(value="")}) - return ret - checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] ) - # 整理反复出现的控件句柄组合 - input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt] - output_combo = [cookies, chatbot, history, status] - predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo) - # 提交按钮、重置按钮 - cancel_handles.append(txt.submit(**predict_args)) - cancel_handles.append(txt2.submit(**predict_args)) - cancel_handles.append(submitBtn.click(**predict_args)) - cancel_handles.append(submitBtn2.click(**predict_args)) - resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) - resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) - # 基础功能区的回调函数注册 - for k in functional: - click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo) - cancel_handles.append(click_handle) - # 文件上传区,接收文件后与chatbot的互动 - file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) - # 函数插件-固定按钮区 - for k in crazy_fns: - if not crazy_fns[k].get("AsButton", True): continue - click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo) + gr.Markdown(description) + with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary: + with gr.Row(): + txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False) + with gr.Row(): + submitBtn2 = gr.Button("提交", variant="primary") + with gr.Row(): + resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") + stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") + # 功能区显示开关与功能区的互动 + def fn_area_visibility(a): + ret = {} + ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))}) + ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))}) + ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))}) + ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))}) + if "底部输入区" in a: ret.update({txt: gr.update(value="")}) + return ret + checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] ) + # 整理反复出现的控件句柄组合 + input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt] + output_combo = [cookies, chatbot, history, status] + predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo) + # 提交按钮、重置按钮 + cancel_handles.append(txt.submit(**predict_args)) + cancel_handles.append(txt2.submit(**predict_args)) + cancel_handles.append(submitBtn.click(**predict_args)) + cancel_handles.append(submitBtn2.click(**predict_args)) + resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) + resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) + # 基础功能区的回调函数注册 + for k in functional: + click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo) + cancel_handles.append(click_handle) + # 文件上传区,接收文件后与chatbot的互动 + file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) + # 函数插件-固定按钮区 + for k in crazy_fns: + if not crazy_fns[k].get("AsButton", True): continue + click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo) + click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) + cancel_handles.append(click_handle) + # 函数插件-下拉菜单与随变按钮的互动 + def on_dropdown_changed(k): + variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary" + return {switchy_bt: gr.update(value=k, variant=variant)} + dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt] ) + # 随变按钮的回调函数注册 + def route(k, *args, **kwargs): + if k in [r"打开插件列表", r"请先从插件列表中选择"]: return + yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs) + click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo) click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) + # def expand_file_area(file_upload, area_file_up): + # if len(file_upload)>0: return {area_file_up: gr.update(open=True)} + # click_handle.then(expand_file_area, [file_upload, area_file_up], [area_file_up]) cancel_handles.append(click_handle) - # 函数插件-下拉菜单与随变按钮的互动 - def on_dropdown_changed(k): - variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary" - return {switchy_bt: gr.update(value=k, variant=variant)} - dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt] ) - # 随变按钮的回调函数注册 - def route(k, *args, **kwargs): - if k in [r"打开插件列表", r"请先从插件列表中选择"]: return - yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs) - click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo) - click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) - # def expand_file_area(file_upload, area_file_up): - # if len(file_upload)>0: return {area_file_up: gr.update(open=True)} - # click_handle.then(expand_file_area, [file_upload, area_file_up], [area_file_up]) - cancel_handles.append(click_handle) - # 终止按钮的回调函数注册 - stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) - stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) -# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 -def auto_opentab_delay(): - import threading, webbrowser, time - print(f"如果浏览器没有自动打开,请复制并转到以下URL:") - print(f"\t(亮色主题): http://localhost:{PORT}") - print(f"\t(暗色主题): http://localhost:{PORT}/?__dark-theme=true") - def open(): - time.sleep(2) # 打开浏览器 - webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") - threading.Thread(target=open, name="open-browser", daemon=True).start() - threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() + # 终止按钮的回调函数注册 + stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) + stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) -auto_opentab_delay() -demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) + # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 + def auto_opentab_delay(): + import threading, webbrowser, time + print(f"如果浏览器没有自动打开,请复制并转到以下URL:") + print(f"\t(亮色主题): http://localhost:{PORT}") + print(f"\t(暗色主题): http://localhost:{PORT}/?__dark-theme=true") + def open(): + time.sleep(2) # 打开浏览器 + webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") + threading.Thread(target=open, name="open-browser", daemon=True).start() + threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() + + auto_opentab_delay() + demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index f02a1c8..6fdd846 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -31,6 +31,24 @@ methods = { "tgui-ui": tgui_ui, } +def LLM_CATCH_EXCEPTION(f): + """ + 装饰器函数,将错误显示出来 + """ + def decorated(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience): + try: + return f(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience) + except Exception as e: + from toolbox import get_conf + import traceback + proxies, = get_conf('proxies') + tb_str = '\n```\n' + traceback.format_exc() + '\n```\n' + observe_window[0] = tb_str + return tb_str + return decorated + +colors = ['#FF00FF', '#00FFFF', '#FF0000''#990099', '#009999', '#990044'] + def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience=False): """ 发送至LLM,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 @@ -62,17 +80,13 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser return method(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience) else: # 如果同时询问多个大语言模型: - executor = ThreadPoolExecutor(max_workers=16) + executor = ThreadPoolExecutor(max_workers=4) models = model.split('&') n_model = len(models) window_len = len(observe_window) - if window_len==0: - window_mutex = [[] for _ in range(n_model)] + [True] - elif window_len==1: - window_mutex = [[""] for _ in range(n_model)] + [True] - elif window_len==2: - window_mutex = [["", time.time()] for _ in range(n_model)] + [True] + assert window_len==3 + window_mutex = [["", time.time(), ""] for _ in range(n_model)] + [True] futures = [] for i in range(n_model): @@ -85,12 +99,12 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser method = methods['tgui-no-ui'] llm_kwargs_feedin = copy.deepcopy(llm_kwargs) llm_kwargs_feedin['llm_model'] = model - future = executor.submit(method, inputs, llm_kwargs_feedin, history, sys_prompt, window_mutex[i], console_slience) + future = executor.submit(LLM_CATCH_EXCEPTION(method), inputs, llm_kwargs_feedin, history, sys_prompt, window_mutex[i], console_slience) futures.append(future) def mutex_manager(window_mutex, observe_window): while True: - time.sleep(0.2) + time.sleep(0.5) if not window_mutex[-1]: break # 看门狗(watchdog) for i in range(n_model): @@ -98,8 +112,8 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser # 观察窗(window) chat_string = [] for i in range(n_model): - chat_string.append( f"[{str(models[i])} 说]: {window_mutex[i][0]}" ) - res = '\n\n---\n\n'.join(chat_string) + chat_string.append( f"【{str(models[i])} 说】: {window_mutex[i][0]} " ) + res = '

\n\n---\n\n'.join(chat_string) # # # # # # # # # # # observe_window[0] = res @@ -107,10 +121,18 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser t_model.start() return_string_collect = [] + while True: + worker_done = [h.done() for h in futures] + if all(worker_done): + executor.shutdown() + break + time.sleep(1) + for i, future in enumerate(futures): # wait and get - return_string_collect.append( f"[{str(models[i])} 说]: {future.result()}" ) + return_string_collect.append( f"【{str(models[i])} 说】: {future.result()} " ) + window_mutex[-1] = False # stop mutex thread - res = '\n\n---\n\n'.join(return_string_collect) + res = '
\n\n---\n\n'.join(return_string_collect) return res diff --git a/request_llm/bridge_chatglm.py b/request_llm/bridge_chatglm.py index d6f5eec..819519b 100644 --- a/request_llm/bridge_chatglm.py +++ b/request_llm/bridge_chatglm.py @@ -3,35 +3,69 @@ from transformers import AutoModel, AutoTokenizer import time import importlib from toolbox import update_ui, get_conf +from multiprocessing import Process, Pipe +################################################################################# +class GetGLMHandle(Process): + def __init__(self): + super().__init__(daemon=True) + self.parent, self.child = Pipe() + self.chatglm_model = None + self.chatglm_tokenizer = None + self.start() + print('初始化') + + def ready(self): + return self.chatglm_model is not None -global chatglm_model, chatglm_tokenizer - -chatglm_model = None -chatglm_tokenizer = None - -def model_loader(): - global chatglm_model, chatglm_tokenizer - if chatglm_tokenizer is None: - chatglm_tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) - if chatglm_model is None: # 尚未加载 - device, = get_conf('LOCAL_MODEL_DEVICE') - if device=='cpu': - chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float() - else: - chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda() - chatglm_model = chatglm_model.eval() - chatglm_model = chatglm_model.eval() + def run(self): + while True: + try: + if self.chatglm_model is None: + self.chatglm_tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) + device, = get_conf('LOCAL_MODEL_DEVICE') + if device=='cpu': + self.chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float() + else: + self.chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda() + self.chatglm_model = self.chatglm_model.eval() + break + else: + break + except: + pass + while True: + kwargs = self.child.recv() + try: + for response, history in self.chatglm_model.stream_chat(self.chatglm_tokenizer, **kwargs): + self.child.send(response) + except: + self.child.send('[Local Message] Call ChatGLM fail.') + self.child.send('[Finish]') + def stream_chat(self, **kwargs): + self.parent.send(kwargs) + while True: + res = self.parent.recv() + if res != '[Finish]': + yield res + else: + break + return + +global glm_handle +glm_handle = None +################################################################################# def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False): """ + 多线程方法 函数的说明请见 request_llm/bridge_all.py """ - global chatglm_model, chatglm_tokenizer - if chatglm_model is None: - observe_window[0] = "ChatGLM尚未加载,加载需要一段时间 ……" + global glm_handle + if glm_handle is None: + glm_handle = GetGLMHandle() + observe_window[0] = "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……" - model_loader() # chatglm 没有 sys_prompt 接口,因此把prompt加入 history history_feedin = [] for i in range(len(history)//2): @@ -40,29 +74,27 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 response = "" - for response, history in chatglm_model.stream_chat(chatglm_tokenizer, inputs, history=history_feedin, max_length=llm_kwargs['max_length'], - top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): - # 观测窗,把已经获取的数据显示出去 + for response in glm_handle.stream_chat(query=inputs, history=history_feedin, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): observe_window[0] = response - # 看门狗 (watchdog),如果超过期限没有喂狗,则终止 if len(observe_window) >= 2: if (time.time()-observe_window[1]) > watch_dog_patience: raise RuntimeError("程序终止。") - # if not console_slience: - # print(response) return response + def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): """ + 单线程方法 函数的说明请见 request_llm/bridge_all.py """ - global chatglm_model, chatglm_tokenizer chatbot.append((inputs, "")) - if chatglm_model is None: - chatbot[-1] = (inputs, "ChatGLM尚未加载,加载需要一段时间 ……") + + global glm_handle + if glm_handle is None: + glm_handle = GetGLMHandle() + chatbot[-1] = (inputs, "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……") yield from update_ui(chatbot=chatbot, history=[]) - model_loader() if additional_fn is not None: import core_functional @@ -71,13 +103,11 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp if "PreProcess" in core_functional[additional_fn]: inputs = core_functional[additional_fn]["PreProcess"](inputs) # 获取预处理函数(如果有的话) inputs = core_functional[additional_fn]["Prefix"] + inputs + core_functional[additional_fn]["Suffix"] - history_feedin = [] for i in range(len(history)//2): history_feedin.append(["What can I do?", system_prompt] ) history_feedin.append([history[2*i], history[2*i+1]] ) - for response, history in chatglm_model.stream_chat(chatglm_tokenizer, inputs, history=history_feedin, max_length=llm_kwargs['max_length'], - top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): + for response in glm_handle.stream_chat(query=inputs, history=history_feedin, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): chatbot[-1] = (inputs, response) yield from update_ui(chatbot=chatbot, history=history) \ No newline at end of file diff --git a/version b/version index 59e288a..620acb6 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { - "version": 3.0, + "version": 3.1, "show_feature": true, "new_feature": "支持ChatGLM <-> 支持多LLM模型同时对话" } From 4120b05dd31ed9112ba650f19e9d1ad24f534b0e Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Sat, 15 Apr 2023 19:17:22 +0800 Subject: [PATCH 04/50] =?UTF-8?q?=E6=B8=85=E9=99=A4=E6=8C=89=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index db35c15..6ed7d23 100644 --- a/main.py +++ b/main.py @@ -68,6 +68,7 @@ def main(): with gr.Row(): resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") + clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm") with gr.Row(): status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}") with gr.Accordion("基础功能区", open=True) as area_basic_fn: @@ -99,7 +100,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=4096, value=512, step=1, interactive=True, label="MaxLength",) - checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") + checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") md_dropdown = gr.Dropdown(["gpt-3.5-turbo", "chatglm"], value=LLM_MODEL, label="").style(container=False) gr.Markdown(description) @@ -111,6 +112,7 @@ def main(): with gr.Row(): resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") + clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm") # 功能区显示开关与功能区的互动 def fn_area_visibility(a): ret = {} @@ -118,9 +120,11 @@ def main(): ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))}) ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))}) ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))}) + ret.update({clearBtn: gr.update(visible=("输入清除键" in a))}) + ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))}) if "底部输入区" in a: ret.update({txt: gr.update(value="")}) return ret - checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2] ) + checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2] ) # 整理反复出现的控件句柄组合 input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt] output_combo = [cookies, chatbot, history, status] @@ -132,6 +136,8 @@ def main(): cancel_handles.append(submitBtn2.click(**predict_args)) resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status]) + clearBtn.click(lambda: ("",""), None, [txt, txt2]) + clearBtn2.click(lambda: ("",""), None, [txt, txt2]) # 基础功能区的回调函数注册 for k in functional: click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo) From f6483c93e1dfb1d5cbf34ec6787427ed76922699 Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Sat, 15 Apr 2023 19:24:22 +0800 Subject: [PATCH 05/50] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E7=9A=84=E5=B0=8F=E5=B0=8FBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 6ed7d23..ccb218c 100644 --- a/main.py +++ b/main.py @@ -110,8 +110,8 @@ def main(): with gr.Row(): submitBtn2 = gr.Button("提交", variant="primary") with gr.Row(): - resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm") - stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm") + resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm") + stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm") clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm") # 功能区显示开关与功能区的互动 def fn_area_visibility(a): From 8049296beeb994082b1219fdb4ec2bf141975d07 Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Sat, 15 Apr 2023 21:08:44 +0800 Subject: [PATCH 06/50] =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/crazy_utils.py | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index b255706..5aeb8ca 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -236,7 +236,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( wait = random.randint(5, 20) if "Rate limit reached" in tb_str: wait = wait * 3 - fail_info = "OpenAI请求速率限制 " + fail_info = "OpenAI绑定信用卡可解除频率限制 " else: fail_info = "" # 也许等待十几秒后,情况会好转 diff --git a/requirements.txt b/requirements.txt index 910be08..1cee117 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -gradio>=3.25.0 +gradio==3.25.0 tiktoken>=0.3.3 requests[socks] transformers From c326a86ff470490ba8eab1791ede15bf9dda1245 Mon Sep 17 00:00:00 2001 From: qingxu fu <505030475@qq.com> Date: Mon, 17 Apr 2023 09:21:00 +0800 Subject: [PATCH 07/50] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 9 +- docs/self_analysis.md | 346 +++++++++++++++++++++--------------------- 2 files changed, 175 insertions(+), 180 deletions(-) diff --git a/config.py b/config.py index 8db5d46..a9cbc95 100644 --- a/config.py +++ b/config.py @@ -19,13 +19,14 @@ if USE_PROXY: else: proxies = None -# 多线程函数插件中,默认允许多少路线程同时访问OpenAI。 +# [step 3]>> 多线程函数插件中,默认允许多少路线程同时访问OpenAI。 # Free trial users的限制是每分钟3次,Pay-as-you-go users的限制是每分钟3500次。提高限制请查询: # https://platform.openai.com/docs/guides/rate-limits/overview +# 一言以蔽之:免费用户填3,OpenAI绑了信用卡的用户可以填 16 或者更高 DEFAULT_WORKER_NUM = 3 -# [step 3]>> 以下配置可以优化体验,但大部分场合下并不需要修改 +# [step 4]>> 以下配置可以优化体验,但大部分场合下并不需要修改 # 对话窗的高度 CHATBOT_HEIGHT = 1115 @@ -53,9 +54,9 @@ LOCAL_MODEL_DEVICE = "cpu" # 可选 "cuda" # OpenAI的API_URL API_URL = "https://api.openai.com/v1/chat/completions" -# 设置并行使用的线程数 +# 设置gradio的并行线程数(不需要修改) CONCURRENT_COUNT = 100 -# 设置用户名和密码(相关功能不稳定,与gradio版本和网络都相关,如果本地使用不建议加这个) +# 设置用户名和密码(不需要修改)(相关功能不稳定,与gradio版本和网络都相关,如果本地使用不建议加这个) # [("username", "password"), ("username2", "password2"), ...] AUTHENTICATION = [] diff --git a/docs/self_analysis.md b/docs/self_analysis.md index acdcaa7..28f6682 100644 --- a/docs/self_analysis.md +++ b/docs/self_analysis.md @@ -1,7 +1,7 @@ # chatgpt-academic项目自译解报告 (Author补充:以下分析均由本项目调用ChatGPT一键生成,如果有不准确的地方,全怪GPT😄) -## 对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括'check_proxy.py', 'config.py'等)。 +## 对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能。 整体概括: @@ -9,254 +9,248 @@ 文件功能表格: -| 文件名称 | 功能 | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| .\check_proxy.py | 检查代理设置功能。 | -| .\config.py | 配置文件,存储程序的基本设置。 | -| .\config_private.py | 存储代理网络地址的文件。 | -| .\core_functional.py | 主要的程序逻辑,包括聊天机器人和文件处理。 | -| .\cradle.py | 程序入口,初始化程序和启动 Web 服务。 | -| .\crazy_functional.py | 辅助程序功能,包括PDF文档处理、代码处理、函数注释生成等。 | -| .\main.py | 包含聊天机器人的具体实现。 | -| .\show_math.py | 处理 LaTeX 公式的函数。 | -| .\theme.py | 存储 Gradio Web 服务的 CSS 样式文件。 | -| .\toolbox.py | 提供了一系列工具函数,包括文件读写、网页抓取、解析函数参数、生成 HTML 等。 | -| ./crazy_functions/crazy_utils.py | 提供各种工具函数,如解析字符串、清洗文本、清理目录结构等。 | -| ./crazy_functions/\_\_init\_\_.py | crazy_functions 模块的入口文件。 | -| ./crazy_functions/下载arxiv论文翻译摘要.py | 对 arxiv.org 上的 PDF 论文进行下载和翻译。 | -| ./crazy_functions/代码重写为全英文_多线程.py | 将代码文件中的中文注释和字符串替换为英文。 | -| ./crazy_functions/总结word文档.py | 读取 Word 文档并生成摘要。 | -| ./crazy_functions/批量总结PDF文档.py | 批量读取 PDF 文件并生成摘要。 | -| ./crazy_functions/批量总结PDF文档pdfminer.py | 使用 pdfminer 库进行 PDF 文件处理。 | -| ./crazy_functions/批量翻译PDF文档_多线程.py | 使用多线程技术批量翻译 PDF 文件。 | -| ./crazy_functions/生成函数注释.py | 给 Python 函数自动生成说明文档。 | -| ./crazy_functions/解析项目源代码.py | 解析项目中的源代码,提取注释和函数名等信息。 | -| ./crazy_functions/读文章写摘要.py | 读取多个文本文件并生成对应的摘要。 | -| ./crazy_functions/高级功能函数模板.py | 使用 GPT 模型进行文本处理。 | +| 文件名 | 文件功能 | +| --- | --- | +| check_proxy.py | 用于检查代理的正确性和可用性 | +| colorful.py | 包含不同预设置颜色的常量,并用于多种UI元素 | +| config.py | 用于全局配置的类 | +| config_private.py | 与config.py文件一起使用的另一个配置文件,用于更改私密信息 | +| core_functional.py | 包含一些TextFunctional类和基础功能函数 | +| crazy_functional.py | 包含大量高级功能函数和实验性的功能函数 | +| main.py | 程序的主入口,包含GUI主窗口和主要的UI管理功能 | +| theme.py | 包含一些预设置主题的颜色 | +| toolbox.py | 提供了一些有用的工具函数 | +| crazy_functions\crazy_utils.py | 包含一些用于实现高级功能的辅助函数 | +| crazy_functions\Latex全文润色.py | 实现了对LaTeX文件中全文的润色和格式化功能 | +| crazy_functions\Latex全文翻译.py | 实现了对LaTeX文件中的内容进行翻译的功能 | +| crazy_functions\_\_init\_\_.py | 用于导入crazy_functional.py中的功能函数 | +| crazy_functions\下载arxiv论文翻译摘要.py | 从Arxiv上下载论文并提取重要信息 | +| crazy_functions\代码重写为全英文_多线程.py | 针对中文Python文件,将其翻译为全英文 | +| crazy_functions\总结word文档.py | 提取Word文件的重要内容来生成摘要 | +| crazy_functions\批量Markdown翻译.py | 批量翻译Markdown文件 | +| crazy_functions\批量总结PDF文档.py | 批量从PDF文件中提取摘要 | +| crazy_functions\批量总结PDF文档pdfminer.py | 批量从PDF文件中提取摘要 | +| crazy_functions\批量翻译PDF文档_多线程.py | 批量翻译PDF文件 | +| crazy_functions\理解PDF文档内容.py | 批量分析PDF文件并提取摘要 | +| crazy_functions\生成函数注释.py | 自动生成Python文件中函数的注释 | +| crazy_functions\解析项目源代码.py | 解析并分析给定项目的源代码 | +| crazy_functions\询问多个大语言模型.py | 向多个大语言模型询问输入文本并进行处理 | +| crazy_functions\读文献写摘要.py | 根据用户输入读取文献内容并生成摘要 | +| crazy_functions\谷歌检索小助手.py | 利用谷歌学术检索用户提供的论文信息并提取相关信息 | +| crazy_functions\高级功能函数模板.py | 实现高级功能的模板函数 | +| request_llm\bridge_all.py | 处理与LLM的交互 | +| request_llm\bridge_chatglm.py | 使用ChatGLM模型进行聊天 | +| request_llm\bridge_chatgpt.py | 实现对话生成的各项功能 | +| request_llm\bridge_tgui.py | 在Websockets中与用户进行交互并生成文本输出 | -## [0/22] 程序概述: check_proxy.py +## [0/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\check_proxy.py -该程序的文件名是check_proxy.py,主要有两个函数:check_proxy和auto_update。 +该文件主要包括四个函数:check_proxy、backup_and_download、patch_and_restart 和 auto_update。其中,check_proxy 函数用于检查代理是否可用;backup_and_download 用于进行一键更新备份和下载;patch_and_restart 是一键更新协议的重要函数,用于覆盖和重启;auto_update 函数用于查询版本和用户意见,并自动进行一键更新。该文件主要使用了 requests、json、shutil、zipfile、distutils、subprocess 等 Python 标准库和 toolbox 和 colorful 两个第三方库。 -check_proxy函数中会借助requests库向一个IP查询API发送请求,并返回该IP的地理位置信息。同时根据返回的数据来判断代理是否有效。 +## [1/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\colorful.py -auto_update函数主要用于检查程序更新,会从Github获取程序最新的版本信息,如果当前版本和最新版本相差较大,则会提示用户进行更新。该函数中也会依赖requests库进行网络请求。 +该程序文件实现了一些打印文本的函数,使其具有不同的颜色输出。当系统为Linux时直接跳过,否则使用colorama库来实现颜色输出。程序提供了深色和亮色两种颜色输出方式,同时也提供了对打印函数的别名。对于不是终端输出的情况,对所有的打印函数进行重复定义,以便在重定向时能够避免打印错误日志。 -在程序的开头,还添加了一句防止代理网络影响的代码。程序使用了自己编写的toolbox模块中的get_conf函数来获取代理设置。 +## [2/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\config.py -## [1/22] 程序概述: config.py +该程序文件是一个配置文件,其主要功能是提供使用API密钥等信息,以及对程序的体验进行优化,例如定义对话框高度、布局等。还包含一些其他的设置,例如设置并行使用的线程数、重试次数限制等等。 -该程序文件是一个Python模块,文件名为config.py。该模块包含了一些变量和配置选项,用于配置一个OpenAI的聊天机器人。具体的配置选项如下: +## [3/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\config_private.py -- API_KEY: 密钥,用于连接OpenAI的API。需要填写有效的API密钥。 -- USE_PROXY: 是否使用代理。如果需要使用代理,需要将其改为True。 -- proxies: 代理的协议、地址和端口。 -- CHATBOT_HEIGHT: 聊天机器人对话框的高度。 -- LAYOUT: 聊天机器人对话框的布局,默认为左右布局。 -- TIMEOUT_SECONDS: 发送请求到OpenAI后,等待多久判定为超时。 -- WEB_PORT: 网页的端口,-1代表随机端口。 -- MAX_RETRY: 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制。 -- LLM_MODEL: OpenAI模型选择,目前只对某些用户开放的gpt4。 -- API_URL: OpenAI的API地址。 -- CONCURRENT_COUNT: 使用的线程数。 -- AUTHENTICATION: 用户名和密码,如果需要。 +这是一个名为config_private.py的Python文件,它用于配置API_KEY和代理信息。API_KEY是一个私密密钥,用于访问某些受保护的API。USE_PROXY变量设置为True以应用代理,proxies变量配置了代理网络的地址和协议。在使用该文件时,需要填写正确的API_KEY和代理信息。 -## [2/22] 程序概述: config_private.py +## [4/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\core_functional.py -该程序文件名为config_private.py,包含了API_KEY的设置和代理的配置。使用了一个名为API_KEY的常量来存储私人的API密钥。此外,还有一个名为USE_PROXY的常量来标记是否需要使用代理。如果需要代理,则使用了一个名为proxies的字典来存储代理网络的地址,其中包括协议类型、地址和端口。 +该文件是一个Python模块,名为"core_functional.py"。模块中定义了一个字典,包含了各种核心功能的配置信息,如英语学术润色、中文学术润色、查找语法错误等。每个功能都包含一些前言和后语,在前言中描述了该功能的任务和要求,在后语中提供一些附加信息。此外,有些功能还定义了一些特定的处理函数和按钮颜色。 -## [3/22] 程序概述: core_functional.py +## [5/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functional.py -该程序文件名为`core_functional.py`,主要是定义了一些核心功能函数,包括英语和中文学术润色、查找语法错误、中译英、学术中英互译、英译中、找图片和解释代码等。每个功能都有一个`Prefix`属性和`Suffix`属性,`Prefix`是指在用户输入的任务前面要显示的文本,`Suffix`是指在任务后面要显示的文本。此外,还有一个`Color`属性指示按钮的颜色,以及一个`PreProcess`函数表示对输入进行预处理的函数。 +这是一个Python程序文件,文件名是crazy_functional.py。它导入了一个名为HotReload的工具箱,并定义了一个名为get_crazy_functions()的函数。这个函数包括三个部分的插件组,分别是已经编写完成的第一组插件、已经测试但距离完美状态还差一点点的第二组插件和尚未充分测试的第三组插件。每个插件都有一个名称、一个按钮颜色、一个函数和一个是否加入下拉菜单中的标志位。这些插件提供了多种功能,包括生成函数注释、解析项目源代码、批量翻译PDF文档、谷歌检索、PDF文档内容理解和Latex文档的全文润色、翻译等功能。其中第三组插件可能还存在一定的bug。 -## [4/22] 程序概述: cradle.py +## [6/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\main.py -该程序文件名为cradle.py,主要功能是检测当前版本与远程最新版本是否一致,如果不一致则输出新版本信息并提示更新。其流程大致如下: +该Python脚本代码实现了一个用于交互式对话的Chatbot机器人。它使用了Gradio框架来构建一个Web界面,并在此基础之上嵌入了一个文本输入框和与Chatbot进行交互的其他控件,包括提交、重置、停止和清除按钮、选择框和滑块等。此外,它还包括了一些类和函数和一些用于编程分析的工具和方法。整个程序文件的结构清晰,注释丰富,并提供了很多技术细节,使得开发者可以很容易地在其基础上进行二次开发、修改、扩展和集成。 -1. 导入相关模块与自定义工具箱函数get_conf -2. 读取配置文件中的代理proxies -3. 使用requests模块请求远程版本信息(url为https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version)并加载为json格式 -4. 获取远程版本号、是否显示新功能信息、新功能内容 -5. 读取本地版本文件version并加载为json格式 -6. 获取当前版本号 -7. 比较当前版本与远程版本,如果远程版本号比当前版本号高0.05以上,则输出新版本信息并提示更新 -8. 如果不需要更新,则直接返回 +## [7/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\theme.py -## [5/22] 程序概述: crazy_functional.py +该程序文件名为theme.py,主要功能为调节Gradio的全局样式。在该文件中,调节了Gradio的主题颜色、字体、阴影、边框、渐变等等样式。同时,该文件还添加了一些高级CSS样式,比如调整表格单元格的背景和边框,设定聊天气泡的圆角、最大宽度和阴影等等。如果CODE_HIGHLIGHT为True,则还进行了代码高亮显示。 -该程序文件名为.\crazy_functional.py,主要定义了一个名为get_crazy_functions()的函数,该函数返回一个字典类型的变量function_plugins,其中包含了一些函数插件。 +## [8/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\toolbox.py -一些重要的函数插件包括: +这是一个名为`toolbox.py`的源代码文件。该文件包含了一系列工具函数和装饰器,用于聊天Bot的开发和调试。其中有一些功能包括将输入参数进行重组、捕捉函数中的异常并记录到历史记录中、生成Markdown格式的聊天记录报告等。该文件中还包含了一些与转换Markdown文本相关的函数。 -- 读文章写摘要:可以自动读取Tex格式的论文,并生成其摘要。 +## [9/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\crazy_utils.py -- 批量生成函数注释:可以批量生成Python函数的文档注释。 +这是一个Python程序文件 `crazy_utils.py`,它包含了两个函数: -- 解析项目源代码:可以解析Python、C++、Golang、Java及React项目的源代码。 +- `input_clipping(inputs, history, max_token_limit)`:这个函数接收三个参数,inputs 是一个字符串,history 是一个列表,max_token_limit 是一个整数。它使用 `tiktoken` 、`numpy` 和 `toolbox` 模块,处理输入文本和历史记录,将其裁剪到指定的最大标记数,避免输入过长导致的性能问题。如果 inputs 长度不超过 max_token_limit 的一半,则只裁剪历史;否则,同时裁剪输入和历史。 +- `request_gpt_model_in_new_thread_with_ui_alive(inputs, inputs_show_user, llm_kwargs, chatbot, history, sys_prompt, refresh_interval=0.2, handle_token_exceed=True, retry_times_at_unknown_error=2)`:这个函数接收八个参数,其中后三个是列表类型,其他为标量或句柄等。它提供对话窗口和刷新控制,执行 `predict_no_ui_long_connection` 方法,将输入数据发送至 GPT 模型并获取结果,如果子任务出错,返回相应的错误信息,否则返回结果。 -- 批量总结PDF文档:可以对PDF文档进行批量总结,以提取其中的关键信息。 +## [10/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\Latex全文润色.py -- 一键下载arxiv论文并翻译摘要:可以自动下载arxiv.org网站上的PDF论文,并翻译生成其摘要。 +这是一个名为"crazy_functions\Latex全文润色.py"的程序文件,其中包含了两个函数"Latex英文润色"和"Latex中文润色",以及其他辅助函数。这些函数能够对 Latex 项目进行润色处理,其中 "多文件润色" 函数是一个主要函数,它调用了其他辅助函数用于读取和处理 Latex 项目中的文件。函数使用了多线程和机器学习模型进行自然语言处理,对文件进行简化和排版来满足学术标准。注释已删除并可以在函数内部查找。 -- 批量翻译PDF文档(多线程):可以对PDF文档进行批量翻译,并使用多线程方式提高翻译效率。 +## [11/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\Latex全文翻译.py -## [6/22] 程序概述: main.py +这个程序文件包括一个用于对整个Latex项目进行翻译的函数 `Latex英译中` 和一个用于将中文翻译为英文的函数 `Latex中译英`。这两个函数都会尝试导入依赖库 tiktoken, 若无法导入则会提示用户安装。`Latex英译中` 函数会对 Latex 项目中的文件进行分离并去除注释,然后运行多线程翻译。`Latex中译英` 也做同样的事情,只不过是将中文翻译为英文。这个程序文件还包括其他一些帮助函数。 -本程序为一个基于 Gradio 和 GPT-3 的交互式聊天机器人,文件名为 main.py。其中主要功能包括: +## [12/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\__init__.py -1. 使用 Gradio 建立 Web 界面,实现用户与聊天机器人的交互; -2. 通过 bridge_chatgpt 模块,利用 GPT-3 模型实现聊天机器人的逻辑; -3. 提供一些基础功能和高级函数插件,用户可以通过按钮选择使用; -4. 提供文档格式转变、外观调整以及代理和自动更新等功能。 +这是一个 Python 包,包名为 `crazy_functions`,在 `__init__.py` 文件中定义了一些函数,包含以下函数: -程序的主要流程为: +- `crazy_addition(a, b)`:对两个数进行加法运算,并将结果返回。 +- `crazy_multiplication(a, b)`:对两个数进行乘法运算,并将结果返回。 +- `crazy_subtraction(a, b)`:对两个数进行减法运算,并将结果返回。 +- `crazy_division(a, b)`:对两个数进行除法运算,并将结果返回。 +- `crazy_factorial(n)`:计算 `n` 的阶乘并返回结果。 -1. 导入所需的库和模块,并通过 get_conf 函数获取配置信息; -2. 设置 Gradio 界面的各个组件,包括聊天窗口、输入区、功能区、函数插件区等; -3. 注册各个组件的回调函数,包括用户输入、信号按钮等,实现机器人逻辑的交互; -4. 通过 Gradio 的 queue 函数和 launch 函数启动 Web 服务,并提供聊天机器人的功能。 +这些函数可能会有一些奇怪或者不符合常规的实现方式(由函数名可以看出来),所以这个包的名称为 `crazy_functions`,可能是暗示这些函数会有一些“疯狂”的实现方式。 -此外,程序还提供了代理和自动更新功能,可以确保用户的使用体验。 +## [13/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\下载arxiv论文翻译摘要.py -## [7/22] 程序概述: show_math.py +该程序实现了一个名为“下载arxiv论文并翻译摘要”的函数插件,作者是“binary-husky”。该函数的功能是,在输入一篇arxiv论文的链接后,提取摘要、下载PDF文档、翻译摘要为中文,并将翻译结果保存到文件中。程序使用了一些Python库,如requests、pdfminer和beautifulsoup4等。程序入口是名为“下载arxiv论文并翻译摘要”的函数,其中使用了自定义的辅助函数download_arxiv_和get_name。程序中还使用了其他非函数的辅助函数和变量,如update_ui、CatchException、report_exception和get_conf等。 -该程序是一个Python脚本,文件名为show_math.py。它转换Markdown和LaTeX混合语法到带MathML的HTML。程序使用latex2mathml模块来实现从LaTeX到MathML的转换,将符号转换为HTML实体以批量处理。程序利用正则表达式和递归函数的方法处理不同形式的LaTeX语法,支持以下四种情况:$$形式、$形式、\[..]形式和\(...\)形式。如果无法转换某个公式,则在该位置插入一条错误消息。最后,程序输出HTML字符串。 +## [14/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\代码重写为全英文_多线程.py -## [8/22] 程序概述: theme.py +该文件是一个多线程Python脚本,包含多个函数和利用第三方库进行的API请求。主要功能是将给定文件夹内的Python代码文件中所有中文转化为英文,然后输出转化后的英文代码。重要的功能和步骤包括: -该程序文件为一个Python脚本,其功能是调整Gradio应用的主题和样式,包括字体、颜色、阴影、背景等等。在程序中,使用了Gradio提供的默认颜色主题,并针对不同元素设置了相应的样式属性,以达到美化显示的效果。此外,程序中还包含了一段高级CSS样式代码,针对表格、列表、聊天气泡、行内代码等元素进行了样式设定。 +1. 清空历史,以免输入溢出 +2. 尝试导入依赖,如果缺少依赖,则给出安装建议 +3. 集合文件 +4. 显示随意内容以防卡顿的感觉 +5. Token限制下的截断与处理 +6. 多线程操作请求转换中文变为英文的代码 +7. 所有线程同时开始执行任务函数 +8. 循环轮询各个线程是否执行完毕 +9. 把结果写入文件 +10. 备份一个文件 -## [9/22] 程序概述: toolbox.py +## [15/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\总结word文档.py -此程序文件主要包含了一系列用于聊天机器人开发的实用工具函数和装饰器函数。主要函数包括: +这是一个名为"总结word文档.py"的程序文件,使用python编写。该文件导入了"toolbox"和"crazy_utils"模块,实现了解析docx格式和doc格式的文件的功能。该文件包含了一个名为"解析docx"的函数,通过对文件内容应用自然语言处理技术,生成文章片段的中英文概述。具体实现过程中,该函数使用了"docx"模块和"win32com.client"模块来实现对docx和doc格式文件的解析,同时使用了"request_gpt_model_in_new_thread_with_ui_alive"函数来向GPT模型发起请求。最后,该文件还实现了一个名为"总结word文档"的函数来批量总结Word文档。 -1. ArgsGeneralWrapper:一个装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。 +## [16/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\批量Markdown翻译.py -2. get_reduce_token_percent:一个函数,用于计算自然语言处理时会出现的token溢出比例。 +这个程序文件实现了一个批量Markdown翻译功能,可以将一个源代码项目中的Markdown文本翻译成指定语言(目前支持中<-英和英<-中)。程序主要分为三个函数,`PaperFileGroup`类用于处理长文本的拆分,`多文件翻译`是主要函数调用了`request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency`函数进行多线程翻译并输出结果,`Markdown英译中`和`Markdown中译外`分别是英译中和中译英的入口函数,用于解析项目路径和调用翻译函数。程序依赖于tiktoken等库实现。 -3. predict_no_ui_but_counting_down:一个函数,调用聊天接口,并且保留了一定的界面心跳功能,即当对话太长时,会自动采用二分法截断。 +## [17/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\批量总结PDF文档.py -4. write_results_to_file:一个函数,将对话记录history生成Markdown格式的文本,并写入文件中。 +这是一个名为“批量总结PDF文档”的Python脚本,包含了多个函数。其中有一个函数名为“clean_text”,可以对PDF提取出的原始文本进行清洗和格式化处理,将连字转换为其基本形式,并根据heuristic规则判断换行符是否是段落分隔,并相应地进行替换。另一个函数名为“解析PDF”,可以接收一个PDF文件清单,并对清单中的每一个PDF进行解析,提取出文本并调用“clean_text”函数进行清洗和格式化处理,然后向用户发送一个包含文章简介信息的问题并等待用户回答。最后,该脚本也包含一个名为“批量总结PDF文档”的主函数,其中调用了“解析PDF”函数来完成对PDF文件的批量处理。 -5. regular_txt_to_markdown:一个函数,将普通文本转换为Markdown格式的文本。 +## [18/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\批量总结PDF文档pdfminer.py -6. CatchException:一个装饰器函数,捕捉函数调度中的异常,并封装到一个生成器中返回,并显示到聊天当中。 +这个文件是一个Python模块,文件名为pdfminer.py,它定义了一个函数批量总结PDF文档。该函数接受一些参数,然后尝试导入pdfminer和beautifulsoup4库。该函数将读取pdf文件或tex文件中的内容,对其进行分析,并使用GPT模型进行自然语言摘要。文件中还有一个辅助函数readPdf,用于读取pdf文件中的内容。 -7. HotReload:一个装饰器函数,实现函数插件的热更新。 +## [19/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\批量翻译PDF文档_多线程.py -8. report_execption:一个函数,向chatbot中添加错误信息。 +这是一个Python脚本,文件名是crazy_functions\批量翻译PDF文档_多线程.py。该脚本提供了一个名为“批量翻译PDF文档”的函数,可以批量翻译PDF文件并生成报告文件。该函数使用了多个模块和函数(如toolbox、crazy_utils、update_ui等),使用了Python的异常处理和多线程功能,还使用了一些文本处理函数和第三方库(如fitz和tiktoken)。在函数执行过程中,它会进行一些参数检查、读取和清理PDF文本、递归地切割PDF文件、获取文章meta信息、多线程翻译、整理报告格式等操作,并更新UI界面和生成报告文件。 -9. text_divide_paragraph:一个函数,将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 +## [20/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\理解PDF文档内容.py -10. markdown_convertion:一个函数,将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。 +这是一个解析PDF文件内容的Python程序,程序文件名为"理解PDF文档内容.py",程序主要由5个步骤组成:第0步是切割PDF文件;第1步是从摘要中提取高价值信息,放到history中;第2步是迭代地历遍整个文章,提取精炼信息;第3步是整理history;第4步是设置一个token上限,防止回答时Token溢出。程序主要用到了Python中的各种模块和函数库,如:toolbox, tiktoken, pymupdf等。 -11. close_up_code_segment_during_stream:一个函数,用于在gpt输出代码的中途,即输出了前面的```,但还没输出完后面的```,补上后面的```。 +## [21/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\生成函数注释.py -12. format_io:一个函数,将输入和输出解析为HTML格式。将输出部分的Markdown和数学公式转换为HTML格式。 +这是一个名为"生成函数注释"的函数,带有一个装饰器"@CatchException",可以捕获异常。该函数接受文件路径、参数和聊天机器人等参数,用于对多个Python或C++文件进行函数注释,使用了"toolbox"和"crazy_utils"模块中的函数。该函数会逐个读取指定文件中的内容,并使用聊天机器人进行交互,向用户请求注释信息,然后将生成的注释与原文件内容一起输出到一个markdown表格中。最后,该函数返回一个字符串,指示任务是否已完成。另外还包含一个名为"批量生成函数注释"的函数,它与"生成函数注释"函数一起用于批量处理多个文件。 -13. find_free_port:一个函数,返回当前系统中可用的未使用端口。 +## [22/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\解析项目源代码.py -14. extract_archive:一个函数,解压缩文件。 +这个程序文件实现了对一个源代码项目进行分析的功能。其中,函数`解析项目本身`、`解析一个Python项目`、`解析一个C项目的头文件`、`解析一个C项目`、`解析一个Java项目`和`解析一个Rect项目`分别用于解析不同类型的项目。函数`解析源代码新`实现了对每一个源代码文件的分析,并将分析结果汇总,同时还实现了分组和迭代处理,提高了效率。最后,函数`write_results_to_file`将所有分析结果写入文件。中间,还用到了`request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency`和`request_gpt_model_in_new_thread_with_ui_alive`来完成请求和响应,并用`update_ui`实时更新界面。 -15. find_recent_files:一个函数,查找目录下一分钟内创建的文件。 +## [23/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\询问多个大语言模型.py -16. on_file_uploaded:一个函数,响应用户上传的文件。 +这是一个Python程序,文件名为"crazy_functions\询问多个大语言模型.py"。该程序实现了一个同时向多个大语言模型询问的功能,接收用户输入文本以及模型参数,向ChatGPT和ChatGLM模型发出请求,并将对话记录显示在聊天框中,同时刷新界面。 -## [10/22] 程序概述: crazy_functions\crazy_utils.py +## [24/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\读文章写摘要.py -这是一个名为"crazy_utils.py"的Python程序文件,包含了两个函数: -1. `breakdown_txt_to_satisfy_token_limit()`:接受文本字符串、计算文本单词数量的函数和单词数量限制作为输入参数,将长文本拆分成合适的长度,以满足单词数量限制。这个函数使用一个递归方法去拆分长文本。 -2. `breakdown_txt_to_satisfy_token_limit_for_pdf()`:类似于`breakdown_txt_to_satisfy_token_limit()`,但是它使用一个不同的递归方法来拆分长文本,以满足PDF文档中的需求。当出现无法继续拆分的情况时,该函数将使用一个中文句号标记插入文本来截断长文本。如果还是无法拆分,则会引发运行时异常。 +该程序文件是一个Python模块,文件名为"读文章写摘要.py",主要包含两个函数:"解析Paper"和"读文章写摘要"。其中,"解析Paper"函数接受文件路径、参数等参数,逐个打印文件内容并使用GPT模型生成对该文件的摘要;"读文章写摘要"函数则接受一段文本内容和参数,将该文本内容及其所有.tex文件逐个传递给"解析Paper"函数进行处理,并使用GPT模型生成文章的中英文摘要。文件还导入了一些工具函数,如异常处理、信息上报和文件写入等。 -## [11/22] 程序概述: crazy_functions\__init__.py +## [25/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\谷歌检索小助手.py -这个程序文件是一个 Python 的包,包名为 "crazy_functions",并且是其中的一个子模块 "__init__.py"。该包中可能包含多个函数或类,用于实现各种疯狂的功能。由于该文件的具体代码没有给出,因此无法进一步确定该包中的功能。通常情况下,一个包应该具有 __init__.py、__main__.py 和其它相关的模块文件,用于实现该包的各种功能。 +该文件代码包含了一个名为`get_meta_information`的函数和一个名为`谷歌检索小助手`的装饰器函数,用于从谷歌学术中抓取文章元信息,并从用户提供的搜索页面中分析所有文章的相关信息。该文件使用了许多第三方库,如requests、arxiv、BeautifulSoup等。其中`get_meta_information`函数中还定义了一个名为`string_similar`的辅助函数,用于比较字符串相似度。 -## [12/22] 程序概述: crazy_functions\下载arxiv论文翻译摘要.py +## [26/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\crazy_functions\高级功能函数模板.py -这个程序实现的功能是下载arxiv论文并翻译摘要,文件名为`下载arxiv论文翻译摘要.py`。这个程序引入了`requests`、`unicodedata`、`os`、`re`等Python标准库,以及`pdfminer`、`bs4`等第三方库。其中`download_arxiv_`函数主要实现了从arxiv网站下载论文的功能,包括解析链接、获取论文信息、下载论文和生成文件名等,`get_name`函数则是为了从arxiv网站中获取论文信息创建的辅助函数。`下载arxiv论文并翻译摘要`函数则是实现了从下载好的PDF文件中提取摘要,然后使用预先训练的GPT模型翻译为中文的功能。同时,该函数还会将历史记录写入文件中。函数还会通过`CatchException`函数来捕获程序中出现的异常信息。 +该程序文件是一个 Python 模块,包含一个名为“高阶功能模板函数”的函数。该函数接受多个参数,其中包括输入文本、GPT 模型参数、插件模型参数、聊天显示框、聊天历史等。 该函数的主要功能是根据输入文本,使用 GPT 模型生成一些问题,并等待用户回答这些问题(使用 Markdown 格式),然后将用户回答加入到聊天历史中,并更新聊天显示框。该函数还包含了一些异常处理和多线程的相关操作。该程序文件还引用了另一个 Python 模块中的两个函数,分别为“CatchException”和“update_ui”,并且还引用了一个名为“request_gpt_model_in_new_thread_with_ui_alive”的自定义函数。 -## [13/22] 程序概述: crazy_functions\代码重写为全英文_多线程.py +## [27/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\request_llm\bridge_all.py -该程序文件为一个Python多线程程序,文件名为"crazy_functions\代码重写为全英文_多线程.py"。该程序使用了多线程技术,将一个大任务拆成多个小任务,同时执行,提高运行效率。 +这个文件是用来处理与LLM的交互的。包含两个函数,一个是 predict_no_ui_long_connection 用来处理长文本的输出,可以多线程调用;另一个是 predict 用来处理基础的对话功能。这个文件会导入其他文件中定义的方法进行调用,具体调用哪个方法取决于传入的参数。函数中还有一些装饰器和管理多线程的逻辑。 -程序的主要功能是将Python文件中的中文转换为英文,同时将转换后的代码输出。程序先清空历史记录,然后尝试导入openai和transformers等依赖库。程序接下来会读取当前路径下的.py文件和crazy_functions文件夹中的.py文件,并将其整合成一个文件清单。随后程序会使用GPT2模型进行中英文的翻译,并将结果保存在本地路径下的"gpt_log/generated_english_version"文件夹中。程序最终会生成一个任务执行报告。 +## [28/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\request_llm\bridge_chatglm.py -需要注意的是,该程序依赖于"request_llm"和"toolbox"库以及本地的"crazy_utils"模块。 +这个程序文件实现了一个使用ChatGLM模型进行聊天的功能。具体实现过程是:首先进行初始化,然后使用GetGLMHandle类进行ChatGLM模型的加载和运行。predict_no_ui_long_connection函数用于多线程聊天,而predict函数用于单线程聊天,它们的不同之处在于前者不会更新UI界面,后者会。这个文件还导入了其他模块和库,例如transformers、time、importlib等,并使用了多进程Pipe。 -## [14/22] 程序概述: crazy_functions\总结word文档.py +## [29/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\request_llm\bridge_chatgpt.py -该程序文件是一个 Python 脚本文件,文件名为 ./crazy_functions/总结word文档.py。该脚本是一个函数插件,提供了名为“总结word文档”的函数。该函数的主要功能是批量读取给定文件夹下的 Word 文档文件,并使用 GPT 模型生成对每个文件的概述和意见建议。其中涉及到了读取 Word 文档、使用 GPT 模型等操作,依赖于许多第三方库。该文件也提供了导入依赖的方法,使用该脚本需要安装依赖库 python-docx 和 pywin32。函数功能实现的过程中,使用了一些用于调试的变量(如 fast_debug),可在需要时设置为 True。该脚本文件也提供了对程序功能和贡献者的注释。 +这个程序文件是用于对话生成的,主要包含三个函数:predict、predict_no_ui、predict_no_ui_long_connection。其中,predict是用于普通对话的函数,具备完备的交互功能,但不具备多线程能力;predict_no_ui是高级实验性功能模块调用的函数,参数简单,可以多线程并行,方便实现复杂的功能逻辑;predict_no_ui_long_connection解决了predict_no_ui在处理长文档时容易断开连接的问题,同样支持多线程。程序中还包含一些常量和工具函数,用于整合信息,选择LLM模型,生成http请求,发送请求,接收响应等。它需要配置一个config文件,包含代理网址、API等敏感信息。 -## [15/22] 程序概述: crazy_functions\批量总结PDF文档.py +## [30/31] 请对下面的程序文件做一个概述: H:\chatgpt_academic_resolve\request_llm\bridge_tgui.py -该程序文件名为 `./crazy_functions\批量总结PDF文档.py`,主要实现了批量处理PDF文档的功能。具体实现了以下几个函数: +该程序文件实现了一个基于Websockets的文本生成服务和对话功能。其中,有三个函数:`run()`、`predict()`和`predict_no_ui_long_connection()`。`run()`函数用于连接到Websocket服务并生成文本结果;`predict()`函数用于将用户输入作为文本生成的输入,同时在UI上显示对话历史记录,并在不断更新UI的过程中不断更新生成的文本输出;`predict_no_ui_long_connection()`函数与`predict()`函数类似,但没有UI,并在一段时间内返回单个生成的文本。整个程序还引入了多个Python模块来完成相关功能,例如`asyncio`、`websockets`、`json`等等。 -1. `is_paragraph_break(match)`:根据给定的匹配结果判断换行符是否表示段落分隔。 -2. `normalize_text(text)`:通过将文本特殊符号转换为其基本形式来对文本进行归一化处理。 -3. `clean_text(raw_text)`:对从 PDF 提取出的原始文本进行清洗和格式化处理。 -4. `解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)`:对给定的PDF文件进行分析并生成相应的概述。 -5. `批量总结PDF文档(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:批量处理PDF文件,对其进行摘要生成。 +## 根据以上分析,对程序的整体功能和构架重新做出概括。然后用一张markdown表格整理每个文件的功能(包括check_proxy.py, colorful.py, config.py, config_private.py, core_functional.py, crazy_functional.py, main.py, theme.py, toolbox.py, crazy_functions\crazy_utils.py, crazy_functions\Latex全文润色.py, crazy_functions\Latex全文翻译.py, crazy_functions\__init__.py, crazy_functions\下载arxiv论文翻译摘要.py, crazy_functions\代码重写为全英文_多线程.py, crazy_functions\总结word文档.py)。 -其中,主要用到了第三方库`pymupdf`对PDF文件进行处理。程序通过调用`fitz.open`函数打开PDF文件,使用`page.get_text()`方法获取PDF文本内容。然后,使用`clean_text`函数对文本进行清洗和格式化处理,生成最终的摘要。最后,调用`write_results_to_file`函数将历史记录写入文件并输出。 +程序功能概括:该程序是一个聊天机器人,可以通过 Web 界面与用户进行交互。它包含了丰富的功能,如文本润色、翻译、代码重写、在线查找等,并且支持多线程处理。用户可以通过 Gradio 框架提供的 Web 界面进行交互,程序还提供了一些调试工具,如toolbox 模块,方便程序开发和调试。 -## [16/22] 程序概述: crazy_functions\批量总结PDF文档pdfminer.py +下表概述了每个文件的功能: -这个程序文件名是./crazy_functions\批量总结PDF文档pdfminer.py,是一个用于批量读取PDF文件,解析其中的内容,并对其进行概括的程序。程序中引用了pdfminer和beautifulsoup4等Python库,读取PDF文件并将其转化为文本内容,然后利用GPT模型生成摘要语言,最终输出一个中文和英文的摘要。程序还有一些错误处理的代码,会输出错误信息。 +| 文件名 | 功能 | +| ----------------------------------------------------------- | ------------------------------------------------------------ | +| check_proxy.py | 检查代理是否可用 | +| colorful.py | 用于打印文本的字体颜色输出模块 | +| config.py | 用于程序中的各种设置,如并行线程数量和重试次数的限制等 | +| config_private.py | 配置API_KEY和代理信息的文件 | +| core_functional.py | 包含具体的文本处理功能的模块 | +| crazy_functional.py | 包括各种插件函数的模块,提供了多种文本处理功能 | +| main.py | 包含 Chatbot 机器人主程序的模块 | +| theme.py | 用于调节全局样式的模块 | +| toolbox.py | 包含工具函数和装饰器,用于聊天Bot的开发和调试 | +| crazy_functions\crazy_utils.py | 包含一些辅助函数,如文本裁剪和消息捕捉等 | +| crazy_functions\Latex全文润色.py | 对 Latex 项目进行润色处理的功能模块 | +| crazy_functions\Latex全文翻译.py | 对 Latex 项目进行翻译的功能模块 | +| crazy_functions\__init__.py | 定义一些奇特的数学函数等 | +| crazy_functions\下载arxiv论文翻译摘要.py | 下载 Arxiv 论文并翻译摘要的功能模块 | +| crazy_functions\代码重写为全英文_多线程.py | 将Python程序中所有中文转化为英文的功能模块 | +| crazy_functions\总结word文档.py | 解析 docx 和 doc 格式的文件,生成文章片段的中英文概述的功能模块 | -## [17/22] 程序概述: crazy_functions\批量翻译PDF文档_多线程.py +## 根据以上分析,对程序的整体功能和构架重新做出概括。然后用一张markdown表格整理每个文件的功能(包括check_proxy.py, colorful.py, config.py, config_private.py, core_functional.py, crazy_functional.py, main.py, theme.py, toolbox.py, crazy_functions\crazy_utils.py, crazy_functions\Latex全文润色.py, crazy_functions\Latex全文翻译.py, crazy_functions\__init__.py, crazy_functions\下载arxiv论文翻译摘要.py, crazy_functions\代码重写为全英文_多线程.py, crazy_functions\总结word文档.py, crazy_functions\批量Markdown翻译.py, crazy_functions\批量总结PDF文档.py, crazy_functions\批量总结PDF文档pdfminer.py, crazy_functions\批量翻译PDF文档_多线程.py, crazy_functions\理解PDF文档内容.py, crazy_functions\生成函数注释.py, crazy_functions\解析项目源代码.py, crazy_functions\询问多个大语言模型.py, crazy_functions\读文章写摘要.py, crazy_functions\谷歌检索小助手.py, crazy_functions\高级功能函数模板.py, request_llm\bridge_all.py, request_llm\bridge_chatglm.py, request_llm\bridge_chatgpt.py, request_llm\bridge_tgui.py)。 -这是一个 Python 程序文件,文件名为 `批量翻译PDF文档_多线程.py`,包含多个函数。主要功能是批量处理 PDF 文档,解析其中的文本,进行清洗和格式化处理,并使用 OpenAI 的 GPT 模型进行翻译。其中使用了多线程技术来提高程序的效率和并行度。 +根据以上分析,整个程序是一个集成了多个有用工具和功能的文本处理和生成工具,提供了多种在不同场景下使用的功能,包括但不限于对话生成、文本摘要、PDF文件批量处理、代码翻译和实用工具等。主要的Python模块包括"toolbox.py"、"config.py"、"core_functional.py"和"crazy_functional.py"等,并且还使用了许多第三方库和模块实现相关功能。以下是每个程序文件的功能: -## [18/22] 程序概述: crazy_functions\生成函数注释.py - -该程序文件名为./crazy_functions\生成函数注释.py。该文件包含两个函数,分别为`生成函数注释`和`批量生成函数注释`。 - -函数`生成函数注释`包含参数`file_manifest`、`project_folder`、`top_p`、`temperature`、`chatbot`、`history`和`systemPromptTxt`。其中,`file_manifest`为一个包含待处理文件路径的列表,`project_folder`表示项目文件夹路径,`top_p`和`temperature`是GPT模型参数,`chatbot`为与用户交互的聊天机器人,`history`记录聊天机器人与用户的历史记录,`systemPromptTxt`为聊天机器人发送信息前的提示语。`生成函数注释`通过读取文件内容,并调用GPT模型对文件中的所有函数生成注释,最后使用markdown表格输出结果。函数中还包含一些条件判断和计时器,以及调用其他自定义模块的函数。 - -函数`批量生成函数注释`包含参数`txt`、`top_p`、`temperature`、`chatbot`、`history`、`systemPromptTxt`和`WEB_PORT`。其中,`txt`表示用户输入的项目文件夹路径,其他参数含义与`生成函数注释`中相同。`批量生成函数注释`主要是通过解析项目文件夹,获取所有待处理文件的路径,并调用函数`生成函数注释`对每个文件进行处理,最终生成注释表格输出给用户。 - -## [19/22] 程序概述: crazy_functions\解析项目源代码.py - -该程序文件包含了多个函数,用于解析不同类型的项目,如Python项目、C项目、Java项目等。其中,最核心的函数是`解析源代码()`,它会对给定的一组文件进行分析,并返回对应的结果。具体流程如下: - -1. 遍历所有待分析的文件,对每个文件进行如下处理: - - 1.1 从文件中读取代码内容,构造成一个字符串。 - - 1.2 构造一条GPT请求,向`predict_no_ui_but_counting_down()`函数发送请求,等待GPT回复。 - - 1.3 将GPT回复添加到机器人会话列表中,更新历史记录。 - - 1.4 如果不是快速调试模式,则等待2秒钟,继续分析下一个文件。 - -2. 如果所有文件都分析完成,则向机器人会话列表中添加一条新消息,提示用户整个分析过程已经结束。 - -3. 返回机器人会话列表和历史记录。 - -除此之外,该程序文件还定义了若干个函数,用于针对不同类型的项目进行解析。这些函数会按照不同的方式调用`解析源代码()`函数。例如,对于Python项目,只需要分析.py文件;对于C项目,需要同时分析.h和.cpp文件等。每个函数中都会首先根据给定的项目路径读取相应的文件,然后调用`解析源代码()`函数进行分析。 - -## [20/22] 程序概述: crazy_functions\读文章写摘要.py - -该程序文件为一个名为“读文章写摘要”的Python函数,用于解析项目文件夹中的.tex文件,并使用GPT模型生成文章的中英文摘要。函数使用了request_llm.bridge_chatgpt和toolbox模块中的函数,并包含两个子函数:解析Paper和CatchException。函数参数包括txt,top_p,temperature,chatbot,history,systemPromptTxt和WEB_PORT。执行过程中函数首先清空历史,然后根据项目文件夹中的.tex文件列表,对每个文件调用解析Paper函数生成中文摘要,最后根据所有文件的中文摘要,调用GPT模型生成英文摘要。函数运行过程中会将结果写入文件并返回聊天机器人和历史记录。 - -## [21/22] 程序概述: crazy_functions\高级功能函数模板.py - -该程序文件为一个高级功能函数模板,文件名为"./crazy_functions\高级功能函数模板.py"。 - -该文件导入了两个模块,分别是"request_llm.bridge_chatgpt"和"toolbox"。其中"request_llm.bridge_chatgpt"模块包含了一个函数"predict_no_ui_long_connection",该函数用于请求GPT模型进行对话生成。"toolbox"模块包含了三个函数,分别是"catchException"、"report_exception"和"write_results_to_file"函数,这三个函数主要用于异常处理和日志记录等。 - -该文件定义了一个名为"高阶功能模板函数"的函数,并通过"decorator"装饰器将该函数装饰为一个异常处理函数,可以处理函数执行过程中出现的错误。该函数的作用是生成历史事件查询的问题,并向用户询问历史中哪些事件发生在指定日期,并索要相关图片。在查询完所有日期后,该函数返回所有历史事件及其相关图片的列表。其中,该函数的输入参数包括: - -1. txt: 一个字符串,表示当前消息的文本内容。 -2. top_p: 一个浮点数,表示GPT模型生成文本时的"top_p"参数。 -3. temperature: 一个浮点数,表示GPT模型生成文本时的"temperature"参数。 -4. chatbot: 一个列表,表示当前对话的记录列表。 -5. history: 一个列表,表示当前对话的历史记录列表。 -6. systemPromptTxt: 一个字符串,表示当前对话的系统提示信息。 -7. WEB_PORT: 一个整数,表示当前应用程序的WEB端口号。 - -该函数在执行过程中,会先清空历史记录,以免输入溢出。然后,它会循环5次,生成5个历史事件查询的问题,并向用户请求输入相关信息。每次询问不携带之前的询问历史。在生成每个问题时,该函数会向"chatbot"列表中添加一条消息记录,并设置该记录的初始状态为"[Local Message] waiting gpt response."。然后,该函数会调用"predict_no_ui_long_connection"函数向GPT模型请求生成一段文本,并将生成的文本作为回答。如果请求过程中出现异常,该函数会忽略异常。最后,该函数将问题和回答添加到"chatbot"列表和"history"列表中,并将"chatbot"和"history"列表作为函数的返回值返回。 +| 文件名 | 文件功能 | +| --- | --- | +| check_proxy.py | 用于检查代理的正确性和可用性 | +| colorful.py | 包含不同预设置颜色的常量,并用于多种UI元素 | +| config.py | 用于全局配置的类 | +| config_private.py | 与config.py文件一起使用的另一个配置文件,用于更改私密信息 | +| core_functional.py | 包含一些TextFunctional类和基础功能函数 | +| crazy_functional.py | 包含大量高级功能函数和实验性的功能函数 | +| main.py | 程序的主入口,包含GUI主窗口和主要的UI管理功能 | +| theme.py | 包含一些预设置主题的颜色 | +| toolbox.py | 提供了一些有用的工具函数 | +| crazy_functions\crazy_utils.py | 包含一些用于实现高级功能的辅助函数 | +| crazy_functions\Latex全文润色.py | 实现了对LaTeX文件中全文的润色和格式化功能 | +| crazy_functions\Latex全文翻译.py | 实现了对LaTeX文件中的内容进行翻译的功能 | +| crazy_functions\_\_init\_\_.py | 用于导入crazy_functional.py中的功能函数 | +| crazy_functions\下载arxiv论文翻译摘要.py | 从Arxiv上下载论文并提取重要信息 | +| crazy_functions\代码重写为全英文_多线程.py | 针对中文Python文件,将其翻译为全英文 | +| crazy_functions\总结word文档.py | 提取Word文件的重要内容来生成摘要 | +| crazy_functions\批量Markdown翻译.py | 批量翻译Markdown文件 | +| crazy_functions\批量总结PDF文档.py | 批量从PDF文件中提取摘要 | +| crazy_functions\批量总结PDF文档pdfminer.py | 批量从PDF文件中提取摘要 | +| crazy_functions\批量翻译PDF文档_多线程.py | 批量翻译PDF文件 | +| crazy_functions\理解PDF文档内容.py | 批量分析PDF文件并提取摘要 | +| crazy_functions\生成函数注释.py | 自动生成Python文件中函数的注释 | +| crazy_functions\解析项目源代码.py | 解析并分析给定项目的源代码 | +| crazy_functions\询问多个大语言模型.py | 向多个大语言模型询问输入文本并进行处理 | +| crazy_functions\读文献写摘要.py | 根据用户输入读取文献内容并生成摘要 | +| crazy_functions\谷歌检索小助手.py | 利用谷歌学术检索用户提供的论文信息并提取相关信息 | +| crazy_functions\高级功能函数模板.py | 实现高级功能的模板函数 | +| request_llm\bridge_all.py | 处理与LLM的交互 | +| request_llm\bridge_chatglm.py | 使用ChatGLM模型进行聊天 | +| request_llm\bridge_chatgpt.py | 实现对话生成的各项功能 | +| request_llm\bridge_tgui.py | 在Websockets中与用户进行交互并生成文本输出 | From 2472185de9c8a80c7ecf1c3255f9d392d9b87386 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 19:41:50 +0800 Subject: [PATCH 08/50] unify tiktoken model --- crazy_functions/Latex全文润色.py | 2 +- crazy_functions/Latex全文翻译.py | 2 +- crazy_functions/crazy_utils.py | 2 +- crazy_functions/代码重写为全英文_多线程.py | 2 +- crazy_functions/批量Markdown翻译.py | 2 +- crazy_functions/批量翻译PDF文档_多线程.py | 2 +- crazy_functions/理解PDF文档内容.py | 2 +- request_llm/bridge_chatgpt.py | 4 ++-- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crazy_functions/Latex全文润色.py b/crazy_functions/Latex全文润色.py index 46026fb..da03686 100644 --- a/crazy_functions/Latex全文润色.py +++ b/crazy_functions/Latex全文润色.py @@ -13,7 +13,7 @@ class PaperFileGroup(): # count_token import tiktoken from toolbox import get_conf - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) self.get_token_num = get_token_num diff --git a/crazy_functions/Latex全文翻译.py b/crazy_functions/Latex全文翻译.py index 06b4502..de25e62 100644 --- a/crazy_functions/Latex全文翻译.py +++ b/crazy_functions/Latex全文翻译.py @@ -13,7 +13,7 @@ class PaperFileGroup(): # count_token import tiktoken from toolbox import get_conf - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) self.get_token_num = get_token_num diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 5aeb8ca..babad27 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -4,7 +4,7 @@ from toolbox import update_ui, get_conf def input_clipping(inputs, history, max_token_limit): import tiktoken import numpy as np - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) mode = 'input-and-history' diff --git a/crazy_functions/代码重写为全英文_多线程.py b/crazy_functions/代码重写为全英文_多线程.py index a09a3f5..f8ecec9 100644 --- a/crazy_functions/代码重写为全英文_多线程.py +++ b/crazy_functions/代码重写为全英文_多线程.py @@ -61,7 +61,7 @@ def 全项目切换英文(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_ MAX_TOKEN = 3000 import tiktoken from toolbox import get_conf - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_fn(txt): return len(enc.encode(txt, disallowed_special=())) diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index 119d3a5..823ca3f 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -13,7 +13,7 @@ class PaperFileGroup(): # count_token import tiktoken from toolbox import get_conf - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) self.get_token_num = get_token_num diff --git a/crazy_functions/批量翻译PDF文档_多线程.py b/crazy_functions/批量翻译PDF文档_多线程.py index 79c1253..0d2f1d5 100644 --- a/crazy_functions/批量翻译PDF文档_多线程.py +++ b/crazy_functions/批量翻译PDF文档_多线程.py @@ -69,7 +69,7 @@ def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, # 递归地切割PDF文件 from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf from toolbox import get_conf - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf( txt=file_content, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT) diff --git a/crazy_functions/理解PDF文档内容.py b/crazy_functions/理解PDF文档内容.py index 168b2c9..05fbd49 100644 --- a/crazy_functions/理解PDF文档内容.py +++ b/crazy_functions/理解PDF文档内容.py @@ -18,7 +18,7 @@ def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_pro from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf from toolbox import get_conf - enc = tiktoken.encoding_for_model(*get_conf('LLM_MODEL')) + enc = tiktoken.encoding_for_model("gpt-3.5-turbo") def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf( txt=file_content, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index e9dfc6b..f37abe8 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -22,8 +22,8 @@ import importlib # config_private.py放自己的秘密如API和代理网址 # 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件 from toolbox import get_conf, update_ui -proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL = \ - get_conf('proxies', 'API_URL', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY', 'LLM_MODEL') +proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \ + get_conf('proxies', 'API_URL', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY') timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \ '网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。' From 03ba072c16ea8ab126f902b3327089861520bd66 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 20:34:55 +0800 Subject: [PATCH 09/50] =?UTF-8?q?=E6=94=B9=E5=96=84word=E6=80=BB=E7=BB=93?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/crazy_functions_test.py | 43 ++++++++++++ crazy_functions/总结word文档.py | 92 +++++++++++-------------- request_llm/bridge_all.py | 40 ++++++++++- 3 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 crazy_functions/crazy_functions_test.py diff --git a/crazy_functions/crazy_functions_test.py b/crazy_functions/crazy_functions_test.py new file mode 100644 index 0000000..4df55a8 --- /dev/null +++ b/crazy_functions/crazy_functions_test.py @@ -0,0 +1,43 @@ +""" +这是什么? + 这个文件用于函数插件的单元测试 + 运行方法 python crazy_functions/crazy_functions_test.py +""" + +def validate_path(): + import os, sys + dir_name = os.path.dirname(__file__) + root_dir_assume = os.path.abspath(os.path.dirname(__file__) + '/..') + os.chdir(root_dir_assume) + sys.path.append(root_dir_assume) + +validate_path() # validate path so you can run from base directory + +from toolbox import get_conf, ChatBotWithCookies +proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY = \ + get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY') + +llm_kwargs = { + 'api_key': API_KEY, + 'llm_model': LLM_MODEL, + 'top_p':1.0, + 'max_length': None, + 'temperature':1.0, +} +plugin_kwargs = { } +chatbot = ChatBotWithCookies(llm_kwargs) +history = [] +system_prompt = "Serve me as a writing and programming assistant." +web_port = 1024 + + +def test_总结word文档(): + from crazy_functions.总结word文档 import 总结word文档 + txt = "F:/AMD" + for _ in 总结word文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +test_总结word文档() + +input("程序完成,回车退出。") +print("退出。") \ No newline at end of file diff --git a/crazy_functions/总结word文档.py b/crazy_functions/总结word文档.py index 742c7ab..f1fe201 100644 --- a/crazy_functions/总结word文档.py +++ b/crazy_functions/总结word文档.py @@ -8,8 +8,6 @@ def 解析docx(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot import time, os # pip install python-docx 用于docx格式,跨平台 # pip install pywin32 用于doc格式,仅支持Win平台 - - print('begin analysis on:', file_manifest) for index, fp in enumerate(file_manifest): if fp.split(".")[-1] == "docx": from docx import Document @@ -29,18 +27,20 @@ def 解析docx(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot word.Quit() print(file_content) - - prefix = "接下来请你逐文件分析下面的论文文件," if index == 0 else "" # private_upload里面的文件名在解压zip后容易出现乱码(rar和7z格式正常),故可以只分析文章内容,不输入文件名 - i_say = prefix + f'请对下面的文章片段用中英文做概述,文件名是{os.path.relpath(fp, project_folder)},' \ - f'文章内容是 ```{file_content}```' - i_say_show_user = prefix + f'[{index+1}/{len(file_manifest)}] 假设你是论文审稿专家,请对下面的文章片段做概述: {os.path.abspath(fp)}' - chatbot.append((i_say_show_user, "[Local Message] waiting gpt response.")) - yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 - - if not fast_debug: - msg = '正常' - # ** gpt request ** + from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf + from request_llm.bridge_all import model_info + max_token = model_info[llm_kwargs['llm_model']]['max_token'] + TOKEN_LIMIT_PER_FRAGMENT = max_token * 3 // 4 + paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf( + txt=file_content, + get_token_fn=model_info[llm_kwargs['llm_model']]['token_cnt'], + limit=TOKEN_LIMIT_PER_FRAGMENT + ) + this_paper_history = [] + for i, paper_frag in enumerate(paper_fragments): + i_say = f'请对下面的文章片段用中文做概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{paper_frag}```' + i_say_show_user = f'请对下面的文章片段做概述: {os.path.abspath(fp)}的第{i+1}/{len(paper_fragments)}个片段。' gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=i_say, inputs_show_user=i_say_show_user, @@ -48,46 +48,34 @@ def 解析docx(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot chatbot=chatbot, history=[], sys_prompt="总结文章。" - ) # 带超时倒计时 + ) + chatbot[-1] = (i_say_show_user, gpt_say) - history.append(i_say_show_user) - history.append(gpt_say) - yield from update_ui(chatbot=chatbot, history=history, msg=msg) # 刷新界面 - if not fast_debug: time.sleep(2) + history.extend([i_say_show_user,gpt_say]) + this_paper_history.extend([i_say_show_user,gpt_say]) - """ - # 可按需启用 - i_say = f'根据你上述的分析,对全文进行概括,用学术性语言写一段中文摘要,然后再写一篇英文的。' - chatbot.append((i_say, "[Local Message] waiting gpt response.")) - yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + # 已经对该文章的所有片段总结完毕,如果文章被切分了, + if len(paper_fragments) > 1: + i_say = f"根据以上的对话,总结文章{os.path.abspath(fp)}的主要内容。" + gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( + inputs=i_say, + inputs_show_user=i_say, + llm_kwargs=llm_kwargs, + chatbot=chatbot, + history=this_paper_history, + sys_prompt="总结文章。" + ) + history.extend([i_say,gpt_say]) + this_paper_history.extend([i_say,gpt_say]) - i_say = f'我想让你做一个论文写作导师。您的任务是使用人工智能工具(例如自然语言处理)提供有关如何改进其上述文章的反馈。' \ - f'您还应该利用您在有效写作技巧方面的修辞知识和经验来建议作者可以更好地以书面形式表达他们的想法和想法的方法。' \ - f'根据你之前的分析,提出建议' - chatbot.append((i_say, "[Local Message] waiting gpt response.")) - yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 - - """ - - if not fast_debug: - msg = '正常' - # ** gpt request ** - gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( - inputs=i_say, - inputs_show_user=i_say, - llm_kwargs=llm_kwargs, - chatbot=chatbot, - history=history, - sys_prompt="总结文章。" - ) # 带超时倒计时 - chatbot[-1] = (i_say, gpt_say) - history.append(i_say) - history.append(gpt_say) - yield from update_ui(chatbot=chatbot, history=history, msg=msg) # 刷新界面 res = write_results_to_file(history) chatbot.append(("完成了吗?", res)) - yield from update_ui(chatbot=chatbot, history=history, msg=msg) # 刷新界面 + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + + res = write_results_to_file(history) + chatbot.append(("所有文件都总结完成了吗?", res)) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 @CatchException @@ -123,11 +111,11 @@ def 总结word文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_pr return # 搜索需要处理的文件清单 - file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.docx', recursive=True)] + \ - [f for f in glob.glob(f'{project_folder}/**/*.doc', recursive=True)] - # [f for f in glob.glob(f'{project_folder}/**/*.tex', recursive=True)] + \ - # [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \ - # [f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)] + if txt.endswith('.docx') or txt.endswith('.doc'): + file_manifest = [txt] + else: + file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.docx', recursive=True)] + \ + [f for f in glob.glob(f'{project_folder}/**/*.doc', recursive=True)] # 如果没找到任何文件 if len(file_manifest) == 0: diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 6fdd846..cd3f9d8 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -8,6 +8,7 @@ 具备多线程调用能力的函数 2. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程 """ +import tiktoken from concurrent.futures import ThreadPoolExecutor @@ -31,6 +32,43 @@ methods = { "tgui-ui": tgui_ui, } +model_info = { + # openai + "gpt-3.5-turbo": { + "max_token": 4096, + "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), + "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), + }, + + "gpt-4": { + "max_token": 4096, + "tokenizer": tiktoken.encoding_for_model("gpt-4"), + "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), + }, + # api_2d + "gpt-3.5-turbo-api2d": { + "max_token": 4096, + "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), + "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), + }, + + "gpt-4-api2d": { + "max_token": 4096, + "tokenizer": tiktoken.encoding_for_model("gpt-4"), + "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), + }, + + # chatglm + "chatglm": { + "max_token": 1024, + "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), + "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), + }, + + +} + + def LLM_CATCH_EXCEPTION(f): """ 装饰器函数,将错误显示出来 @@ -47,7 +85,7 @@ def LLM_CATCH_EXCEPTION(f): return tb_str return decorated -colors = ['#FF00FF', '#00FFFF', '#FF0000''#990099', '#009999', '#990044'] +colors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044'] def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience=False): """ From 9bd8511ba41ab6b9d5c3b17392196b4497e08e50 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 21:23:03 +0800 Subject: [PATCH 10/50] =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 6 +- main.py | 6 +- request_llm/bridge_all.py | 92 ++++++++--------- request_llm/bridge_chatgpt.py | 24 ++--- toolbox.py | 179 ++++++++++++++++++++-------------- 5 files changed, 166 insertions(+), 141 deletions(-) diff --git a/config.py b/config.py index a9cbc95..a6c3f5f 100644 --- a/config.py +++ b/config.py @@ -46,14 +46,12 @@ WEB_PORT = -1 MAX_RETRY = 2 # OpenAI模型选择是(gpt4现在只对申请成功的人开放) -LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm", "tgui:anymodel@localhost:7865" +LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm" +AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "chatglm", "gpt-4", "api2d-gpt-4", "api2d-gpt-3.5-turbo"] # 本地LLM模型如ChatGLM的执行方式 CPU/GPU LOCAL_MODEL_DEVICE = "cpu" # 可选 "cuda" -# OpenAI的API_URL -API_URL = "https://api.openai.com/v1/chat/completions" - # 设置gradio的并行线程数(不需要修改) CONCURRENT_COUNT = 100 diff --git a/main.py b/main.py index ccb218c..9264f01 100644 --- a/main.py +++ b/main.py @@ -5,8 +5,8 @@ def main(): from request_llm.bridge_all import predict from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 - proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY = \ - get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY') + proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY, AVAIL_LLM_MODELS = \ + get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY', 'AVAIL_LLM_MODELS') # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT @@ -101,7 +101,7 @@ def main(): 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=4096, value=512, step=1, interactive=True, label="MaxLength",) checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") - md_dropdown = gr.Dropdown(["gpt-3.5-turbo", "chatglm"], value=LLM_MODEL, label="").style(container=False) + md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="").style(container=False) gr.Markdown(description) with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary: diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index cd3f9d8..9af07a2 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -21,38 +21,42 @@ from .bridge_chatglm import predict as chatglm_ui from .bridge_tgui import predict_no_ui_long_connection as tgui_noui from .bridge_tgui import predict as tgui_ui -methods = { - "openai-no-ui": chatgpt_noui, - "openai-ui": chatgpt_ui, - - "chatglm-no-ui": chatglm_noui, - "chatglm-ui": chatglm_ui, - - "tgui-no-ui": tgui_noui, - "tgui-ui": tgui_ui, -} +colors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044'] model_info = { # openai "gpt-3.5-turbo": { + "fn_with_ui": chatgpt_ui, + "fn_without_ui": chatgpt_noui, + "endpoint": "https://api.openai.com/v1/chat/completions", "max_token": 4096, "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), }, "gpt-4": { + "fn_with_ui": chatgpt_ui, + "fn_without_ui": chatgpt_noui, + "endpoint": "https://api.openai.com/v1/chat/completions", "max_token": 4096, "tokenizer": tiktoken.encoding_for_model("gpt-4"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), }, + # api_2d - "gpt-3.5-turbo-api2d": { + "api2d-gpt-3.5-turbo": { + "fn_with_ui": chatgpt_ui, + "fn_without_ui": chatgpt_noui, + "endpoint": "https://openai.api2d.net/v1/chat/completions", "max_token": 4096, "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), }, - "gpt-4-api2d": { + "api2d-gpt-4": { + "fn_with_ui": chatgpt_ui, + "fn_without_ui": chatgpt_noui, + "endpoint": "https://openai.api2d.net/v1/chat/completions", "max_token": 4096, "tokenizer": tiktoken.encoding_for_model("gpt-4"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), @@ -60,18 +64,20 @@ model_info = { # chatglm "chatglm": { + "fn_with_ui": chatglm_ui, + "fn_without_ui": chatglm_noui, + "endpoint": None, "max_token": 1024, "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), }, - } def LLM_CATCH_EXCEPTION(f): """ - 装饰器函数,将错误显示出来 + 装饰器函数,将错误显示出来 """ def decorated(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience): try: @@ -85,21 +91,20 @@ def LLM_CATCH_EXCEPTION(f): return tb_str return decorated -colors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044'] def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience=False): """ - 发送至LLM,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 - inputs: - 是本次问询的输入 - sys_prompt: - 系统静默prompt - llm_kwargs: - LLM的内部调优参数 - history: - 是之前的对话列表 - observe_window = None: - 用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗 + 发送至LLM,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 + inputs: + 是本次问询的输入 + sys_prompt: + 系统静默prompt + llm_kwargs: + LLM的内部调优参数 + history: + 是之前的对话列表 + observe_window = None: + 用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗 """ import threading, time, copy @@ -109,12 +114,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser assert not model.startswith("tgui"), "TGUI不支持函数插件的实现" # 如果只询问1个大语言模型: - if model.startswith('gpt'): - method = methods['openai-no-ui'] - elif model == 'chatglm': - method = methods['chatglm-no-ui'] - elif model.startswith('tgui'): - method = methods['tgui-no-ui'] + method = model_info[model]["fn_without_ui"] return method(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience) else: # 如果同时询问多个大语言模型: @@ -129,12 +129,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser futures = [] for i in range(n_model): model = models[i] - if model.startswith('gpt'): - method = methods['openai-no-ui'] - elif model == 'chatglm': - method = methods['chatglm-no-ui'] - elif model.startswith('tgui'): - method = methods['tgui-no-ui'] + method = model_info[model]["fn_without_ui"] llm_kwargs_feedin = copy.deepcopy(llm_kwargs) llm_kwargs_feedin['llm_model'] = model future = executor.submit(LLM_CATCH_EXCEPTION(method), inputs, llm_kwargs_feedin, history, sys_prompt, window_mutex[i], console_slience) @@ -176,20 +171,15 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser def predict(inputs, llm_kwargs, *args, **kwargs): """ - 发送至LLM,流式获取输出。 - 用于基础的对话功能。 - inputs 是本次问询的输入 - top_p, temperature是LLM的内部调优参数 - history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误) - chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 - additional_fn代表点击的哪个按钮,按钮见functional.py + 发送至LLM,流式获取输出。 + 用于基础的对话功能。 + inputs 是本次问询的输入 + top_p, temperature是LLM的内部调优参数 + history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误) + chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 + additional_fn代表点击的哪个按钮,按钮见functional.py """ - if llm_kwargs['llm_model'].startswith('gpt'): - method = methods['openai-ui'] - elif llm_kwargs['llm_model'] == 'chatglm': - method = methods['chatglm-ui'] - elif llm_kwargs['llm_model'].startswith('tgui'): - method = methods['tgui-ui'] + method = model_info[llm_kwargs['llm_model']]["fn_with_ui"] yield from method(inputs, llm_kwargs, *args, **kwargs) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index f37abe8..e395b66 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -21,9 +21,9 @@ import importlib # config_private.py放自己的秘密如API和代理网址 # 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件 -from toolbox import get_conf, update_ui -proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \ - get_conf('proxies', 'API_URL', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY') +from toolbox import get_conf, update_ui, is_any_api_key, select_api_key +proxies, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \ + get_conf('proxies', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY') timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \ '网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。' @@ -60,7 +60,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", while True: try: # make a POST request to the API endpoint, stream=False - response = requests.post(API_URL, headers=headers, proxies=proxies, + response = requests.post(llm_kwargs['endpoint'], headers=headers, proxies=proxies, json=payload, stream=True, timeout=TIMEOUT_SECONDS); break except requests.exceptions.ReadTimeout as e: retry += 1 @@ -113,14 +113,14 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 additional_fn代表点击的哪个按钮,按钮见functional.py """ - if inputs.startswith('sk-') and len(inputs) == 51: + if is_any_api_key(inputs): chatbot._cookies['api_key'] = inputs chatbot.append(("输入已识别为openai的api_key", "api_key已导入")) yield from update_ui(chatbot=chatbot, history=history, msg="api_key已导入") # 刷新界面 return - elif len(chatbot._cookies['api_key']) != 51: + elif not is_any_api_key(chatbot._cookies['api_key']): chatbot.append((inputs, "缺少api_key。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。")) - yield from update_ui(chatbot=chatbot, history=history, msg="api_key已导入") # 刷新界面 + yield from update_ui(chatbot=chatbot, history=history, msg="缺少api_key") # 刷新界面 return if additional_fn is not None: @@ -143,7 +143,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp while True: try: # make a POST request to the API endpoint, stream=True - response = requests.post(API_URL, headers=headers, proxies=proxies, + response = requests.post(llm_kwargs['endpoint'], headers=headers, proxies=proxies, json=payload, stream=True, timeout=TIMEOUT_SECONDS);break except: retry += 1 @@ -202,12 +202,14 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream): """ 整合所有信息,选择LLM模型,生成http请求,为发送请求做准备 """ - if len(llm_kwargs['api_key']) != 51: + if not is_any_api_key(llm_kwargs['api_key']): raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。") + api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model']) + headers = { "Content-Type": "application/json", - "Authorization": f"Bearer {llm_kwargs['api_key']}" + "Authorization": f"Bearer {api_key}" } conversation_cnt = len(history) // 2 @@ -235,7 +237,7 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream): messages.append(what_i_ask_now) payload = { - "model": llm_kwargs['llm_model'], + "model": llm_kwargs['llm_model'].strip('api2d-'), "messages": messages, "temperature": llm_kwargs['temperature'], # 1.0, "top_p": llm_kwargs['top_p'], # 1.0, diff --git a/toolbox.py b/toolbox.py index 0dd035a..865e92e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -1,13 +1,10 @@ import markdown -import mdtex2html -import threading import importlib import traceback import inspect import re from latex2mathml.converter import convert as tex2mathml from functools import wraps, lru_cache - ############################### 插件输入输出接驳区 ####################################### class ChatBotWithCookies(list): def __init__(self, cookie): @@ -25,9 +22,10 @@ class ChatBotWithCookies(list): def ArgsGeneralWrapper(f): """ - 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。 + 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。 """ def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args): + from request_llm.bridge_all import model_info txt_passon = txt if txt == "" and txt2 != "": txt_passon = txt2 # 引入一个有cookie的chatbot @@ -38,6 +36,7 @@ def ArgsGeneralWrapper(f): llm_kwargs = { 'api_key': cookies['api_key'], 'llm_model': llm_model, + 'endpoint': model_info[llm_model]['endpoint'], 'top_p':top_p, 'max_length': max_length, 'temperature':temperature, @@ -56,69 +55,10 @@ def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面 """ assert isinstance(chatbot, ChatBotWithCookies), "在传递chatbot的过程中不要将其丢弃。必要时,可用clear将其清空,然后用for+append循环重新赋值。" yield chatbot.get_cookies(), chatbot, history, msg -############################### ################## ####################################### -########################################################################################## - -def get_reduce_token_percent(text): - """ - * 此函数未来将被弃用 - """ - try: - # text = "maximum context length is 4097 tokens. However, your messages resulted in 4870 tokens" - pattern = r"(\d+)\s+tokens\b" - match = re.findall(pattern, text) - EXCEED_ALLO = 500 # 稍微留一点余地,否则在回复时会因余量太少出问题 - max_limit = float(match[0]) - EXCEED_ALLO - current_tokens = float(match[1]) - ratio = max_limit/current_tokens - assert ratio > 0 and ratio < 1 - return ratio, str(int(current_tokens-max_limit)) - except: - return 0.5, '不详' - - - -def write_results_to_file(history, file_name=None): - """ - 将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。 - """ - import os - import time - if file_name is None: - # file_name = time.strftime("chatGPT分析报告%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md' - file_name = 'chatGPT分析报告' + \ - time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md' - os.makedirs('./gpt_log/', exist_ok=True) - with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f: - f.write('# chatGPT 分析报告\n') - for i, content in enumerate(history): - try: # 这个bug没找到触发条件,暂时先这样顶一下 - if type(content) != str: - content = str(content) - except: - continue - if i % 2 == 0: - f.write('## ') - f.write(content) - f.write('\n\n') - res = '以上材料已经被写入' + os.path.abspath(f'./gpt_log/{file_name}') - print(res) - return res - - -def regular_txt_to_markdown(text): - """ - 将普通文本转换为Markdown格式的文本。 - """ - text = text.replace('\n', '\n\n') - text = text.replace('\n\n\n', '\n\n') - text = text.replace('\n\n\n', '\n\n') - return text - def CatchException(f): """ - 装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。 + 装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。 """ @wraps(f) def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT): @@ -155,9 +95,70 @@ def HotReload(f): return decorated +####################################### 其他小工具 ##################################### + +def get_reduce_token_percent(text): + """ + * 此函数未来将被弃用 + """ + try: + # text = "maximum context length is 4097 tokens. However, your messages resulted in 4870 tokens" + pattern = r"(\d+)\s+tokens\b" + match = re.findall(pattern, text) + EXCEED_ALLO = 500 # 稍微留一点余地,否则在回复时会因余量太少出问题 + max_limit = float(match[0]) - EXCEED_ALLO + current_tokens = float(match[1]) + ratio = max_limit/current_tokens + assert ratio > 0 and ratio < 1 + return ratio, str(int(current_tokens-max_limit)) + except: + return 0.5, '不详' + + + +def write_results_to_file(history, file_name=None): + """ + 将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。 + """ + import os + import time + if file_name is None: + # file_name = time.strftime("chatGPT分析报告%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md' + file_name = 'chatGPT分析报告' + \ + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md' + os.makedirs('./gpt_log/', exist_ok=True) + with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f: + f.write('# chatGPT 分析报告\n') + for i, content in enumerate(history): + try: # 这个bug没找到触发条件,暂时先这样顶一下 + if type(content) != str: + content = str(content) + except: + continue + if i % 2 == 0: + f.write('## ') + f.write(content) + f.write('\n\n') + res = '以上材料已经被写入' + os.path.abspath(f'./gpt_log/{file_name}') + print(res) + return res + + +def regular_txt_to_markdown(text): + """ + 将普通文本转换为Markdown格式的文本。 + """ + text = text.replace('\n', '\n\n') + text = text.replace('\n\n\n', '\n\n') + text = text.replace('\n\n\n', '\n\n') + return text + + + + def report_execption(chatbot, history, a, b): """ - 向chatbot中添加错误信息 + 向chatbot中添加错误信息 """ chatbot.append((a, b)) history.append(a) @@ -166,7 +167,7 @@ def report_execption(chatbot, history, a, b): def text_divide_paragraph(text): """ - 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 + 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 """ if '```' in text: # careful input @@ -182,7 +183,7 @@ def text_divide_paragraph(text): def markdown_convertion(txt): """ - 将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。 + 将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。 """ pre = '
' suf = '
' @@ -274,7 +275,7 @@ def close_up_code_segment_during_stream(gpt_reply): def format_io(self, y): """ - 将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。 + 将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。 """ if y is None or y == []: return [] @@ -290,7 +291,7 @@ def format_io(self, y): def find_free_port(): """ - 返回当前系统中可用的未使用端口。 + 返回当前系统中可用的未使用端口。 """ import socket from contextlib import closing @@ -410,9 +411,43 @@ def on_report_generated(files, chatbot): return report_files, chatbot def is_openai_api_key(key): - # 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合 API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", key) - return API_MATCH + return bool(API_MATCH) + +def is_api2d_key(key): + if key.startswith('fk') and len(key) == 41: + return True + else: + return False + +def is_any_api_key(key): + if ',' in key: + keys = key.split(',') + for k in keys: + if is_any_api_key(k): return True + return False + else: + return is_openai_api_key(key) or is_api2d_key(key) + + +def select_api_key(keys, llm_model): + import random + avail_key_list = [] + key_list = keys.split(',') + + if llm_model.startswith('gpt-'): + for k in key_list: + if is_openai_api_key(k): avail_key_list.append(k) + + if llm_model.startswith('api2d-'): + for k in key_list: + if is_api2d_key(k): avail_key_list.append(k) + + if len(avail_key_list) == 0: + raise RuntimeError(f"您提供的api-key不满足要求,不包含任何可用于{llm_model}的api-key。") + + api_key = random.choice(avail_key_list) # 随机负载均衡 + return api_key @lru_cache(maxsize=128) def read_single_conf_with_lru_cache(arg): @@ -423,7 +458,7 @@ def read_single_conf_with_lru_cache(arg): r = getattr(importlib.import_module('config'), arg) # 在读取API_KEY时,检查一下是不是忘了改config if arg == 'API_KEY': - if is_openai_api_key(r): + if is_any_api_key(r): print亮绿(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功") else: print亮红( "[API_KEY] 正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \ From 5549e5880aa80b5db65f63e88efd02d6257c06f6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 21:31:51 +0800 Subject: [PATCH 11/50] =?UTF-8?q?=E6=8F=90=E7=A4=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- request_llm/bridge_chatgpt.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/config.py b/config.py index a6c3f5f..18c6b8f 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,5 @@ # [step 1]>> 例如: API_KEY = "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" (此key无效) -API_KEY = "sk-此处填API密钥" +API_KEY = "sk-此处填API密钥" # 可同时填写多个API-KEY,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey1,fkxxxx-api2dkey2" # [step 2]>> 改为True应用代理,如果直接在海外服务器部署,此处不修改 USE_PROXY = False diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index e395b66..b13b521 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -130,13 +130,18 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp if "PreProcess" in core_functional[additional_fn]: inputs = core_functional[additional_fn]["PreProcess"](inputs) # 获取预处理函数(如果有的话) inputs = core_functional[additional_fn]["Prefix"] + inputs + core_functional[additional_fn]["Suffix"] - if stream: - raw_input = inputs - logging.info(f'[raw_input] {raw_input}') - chatbot.append((inputs, "")) - yield from update_ui(chatbot=chatbot, history=history, msg="等待响应") # 刷新界面 + raw_input = inputs + logging.info(f'[raw_input] {raw_input}') + chatbot.append((inputs, "")) + yield from update_ui(chatbot=chatbot, history=history, msg="等待响应") # 刷新界面 - headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt, stream) + try: + headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt, stream) + except RuntimeError as e: + chatbot[-1] = (inputs, f"您提供的api-key不满足要求,不包含任何可用于{llm_kwargs['llm_model']}的api-key。") + yield from update_ui(chatbot=chatbot, history=history, msg="api-key不满足要求") # 刷新界面 + return + history.append(inputs); history.append(" ") retry = 0 From 3eef2d55a010e593cc66817b503d2159f66f10c3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 21:54:14 +0800 Subject: [PATCH 12/50] =?UTF-8?q?=E6=8F=90=E7=A4=BA=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatgpt.py | 38 +++++++++++++++++------------------ toolbox.py | 5 ++--- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index b13b521..05b0467 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -42,17 +42,17 @@ def get_full_error(chunk, stream_response): def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False): """ - 发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 - inputs: - 是本次问询的输入 - sys_prompt: - 系统静默prompt - llm_kwargs: - chatGPT的内部调优参数 - history: - 是之前的对话列表 - observe_window = None: - 用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗 + 发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 + inputs: + 是本次问询的输入 + sys_prompt: + 系统静默prompt + llm_kwargs: + chatGPT的内部调优参数 + history: + 是之前的对话列表 + observe_window = None: + 用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗 """ watch_dog_patience = 5 # 看门狗的耐心, 设置5秒即可 headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt=sys_prompt, stream=True) @@ -105,13 +105,13 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): """ - 发送至chatGPT,流式获取输出。 - 用于基础的对话功能。 - inputs 是本次问询的输入 - top_p, temperature是chatGPT的内部调优参数 - history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误) - chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 - additional_fn代表点击的哪个按钮,按钮见functional.py + 发送至chatGPT,流式获取输出。 + 用于基础的对话功能。 + inputs 是本次问询的输入 + top_p, temperature是chatGPT的内部调优参数 + history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误) + chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 + additional_fn代表点击的哪个按钮,按钮见functional.py """ if is_any_api_key(inputs): chatbot._cookies['api_key'] = inputs @@ -205,7 +205,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp def generate_payload(inputs, llm_kwargs, history, system_prompt, stream): """ - 整合所有信息,选择LLM模型,生成http请求,为发送请求做准备 + 整合所有信息,选择LLM模型,生成http请求,为发送请求做准备 """ if not is_any_api_key(llm_kwargs['api_key']): raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。") diff --git a/toolbox.py b/toolbox.py index 865e92e..42c7a85 100644 --- a/toolbox.py +++ b/toolbox.py @@ -461,11 +461,10 @@ def read_single_conf_with_lru_cache(arg): if is_any_api_key(r): print亮绿(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功") else: - print亮红( "[API_KEY] 正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \ - "(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)") + print亮红( "[API_KEY] 正确的 API_KEY 是'sk'开头的51位密钥(OpenAI),或者 'fk'开头的41位密钥,请在config文件中修改API密钥之后再运行。") if arg == 'proxies': if r is None: - print亮红('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问。建议:检查USE_PROXY选项是否修改。') + print亮红('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问OpenAI家族的模型。建议:检查USE_PROXY选项是否修改。') else: print亮绿('[PROXY] 网络代理状态:已配置。配置信息如下:', r) assert isinstance(r, dict), 'proxies格式错误,请注意proxies选项的格式,不要遗漏括号。' From 2dd3530e82792a4411a0ce180fd6ded174a430de Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 21:58:28 +0800 Subject: [PATCH 13/50] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/config.py b/config.py index 18c6b8f..1476f86 100644 --- a/config.py +++ b/config.py @@ -19,10 +19,8 @@ if USE_PROXY: else: proxies = None -# [step 3]>> 多线程函数插件中,默认允许多少路线程同时访问OpenAI。 -# Free trial users的限制是每分钟3次,Pay-as-you-go users的限制是每分钟3500次。提高限制请查询: -# https://platform.openai.com/docs/guides/rate-limits/overview -# 一言以蔽之:免费用户填3,OpenAI绑了信用卡的用户可以填 16 或者更高 +# [step 3]>> 多线程函数插件中,默认允许多少路线程同时访问OpenAI。Free trial users的限制是每分钟3次,Pay-as-you-go users的限制是每分钟3500次 +# 一言以蔽之:免费用户填3,OpenAI绑了信用卡的用户可以填 16 或者更高。提高限制请查询:https://platform.openai.com/docs/guides/rate-limits/overview DEFAULT_WORKER_NUM = 3 From 45c81cdaffc2f74e93f94a37df783079a0618695 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 22:00:26 +0800 Subject: [PATCH 14/50] gpt-4 token +8192 --- request_llm/bridge_all.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 9af07a2..b5ad5c1 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -38,7 +38,7 @@ model_info = { "fn_with_ui": chatgpt_ui, "fn_without_ui": chatgpt_noui, "endpoint": "https://api.openai.com/v1/chat/completions", - "max_token": 4096, + "max_token": 8192, "tokenizer": tiktoken.encoding_for_model("gpt-4"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), }, @@ -57,7 +57,7 @@ model_info = { "fn_with_ui": chatgpt_ui, "fn_without_ui": chatgpt_noui, "endpoint": "https://openai.api2d.net/v1/chat/completions", - "max_token": 4096, + "max_token": 8192, "tokenizer": tiktoken.encoding_for_model("gpt-4"), "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), }, From d84c96cfa38b1492db91b54d439733c7ea29737f Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 22:26:46 +0800 Subject: [PATCH 15/50] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=87=E9=80=89?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=8C=BA+=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- toolbox.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 9264f01..5baf655 100644 --- a/main.py +++ b/main.py @@ -143,7 +143,7 @@ def main(): click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo) cancel_handles.append(click_handle) # 文件上传区,接收文件后与chatbot的互动 - file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) + file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes], [chatbot, txt, txt2]) # 函数插件-固定按钮区 for k in crazy_fns: if not crazy_fns[k].get("AsButton", True): continue diff --git a/toolbox.py b/toolbox.py index 42c7a85..3688a33 100644 --- a/toolbox.py +++ b/toolbox.py @@ -370,7 +370,7 @@ def find_recent_files(directory): return recent_files -def on_file_uploaded(files, chatbot, txt): +def on_file_uploaded(files, chatbot, txt, txt2, checkboxes): if len(files) == 0: return chatbot, txt import shutil @@ -392,13 +392,18 @@ def on_file_uploaded(files, chatbot, txt): dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract') moved_files = [fp for fp in glob.glob( 'private_upload/**/*', recursive=True)] - txt = f'private_upload/{time_tag}' + if "底部输入区" in checkboxes: + txt = "" + txt2 = f'private_upload/{time_tag}' + else: + txt = f'private_upload/{time_tag}' + txt2 = "" moved_files_str = '\t\n\n'.join(moved_files) chatbot.append(['我上传了文件,请查收', f'[Local Message] 收到以下文件: \n\n{moved_files_str}' + f'\n\n调用路径参数已自动修正到: \n\n{txt}' + f'\n\n现在您点击任意实验功能时,以上文件将被作为输入参数'+err_msg]) - return chatbot, txt + return chatbot, txt, txt2 def on_report_generated(files, chatbot): From 5316b5c3738105055498d08da031775a533c4653 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 22:33:46 +0800 Subject: [PATCH 16/50] =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=83=85=E5=86=B5?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatgpt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index 05b0467..bd90769 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -196,6 +196,8 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key. OpenAI以提供了不正确的API_KEY为由,拒绝服务.") elif "exceeded your current quota" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] You exceeded your current quota. OpenAI以账户额度不足为由,拒绝服务.") + elif "bad forward key" in error_msg: + chatbot[-1] = (chatbot[-1][0], "[Local Message] Bad forward key. API2D账户额度不足.") else: from toolbox import regular_txt_to_markdown tb_str = '```\n' + traceback.format_exc() + '```' From deb8e5e1378815cf7863fb5ec8bd741306362746 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 22:51:23 +0800 Subject: [PATCH 17/50] =?UTF-8?q?=E5=8A=A8=E6=80=81endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/询问多个大语言模型.py | 2 +- request_llm/bridge_chatgpt.py | 8 ++++++-- toolbox.py | 1 - 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crazy_functions/询问多个大语言模型.py b/crazy_functions/询问多个大语言模型.py index 55bfb41..4457e56 100644 --- a/crazy_functions/询问多个大语言模型.py +++ b/crazy_functions/询问多个大语言模型.py @@ -16,7 +16,7 @@ def 同时问询(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt chatbot.append((txt, "正在同时咨询ChatGPT和ChatGLM……")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新 - llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo' + llm_kwargs['llm_model'] = 'gpt-3.5-turbo&api2d-gpt-3.5-turbo' gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=txt, inputs_show_user=txt, llm_kwargs=llm_kwargs, chatbot=chatbot, history=history, diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index bd90769..719a14f 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -60,7 +60,9 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", while True: try: # make a POST request to the API endpoint, stream=False - response = requests.post(llm_kwargs['endpoint'], headers=headers, proxies=proxies, + from .bridge_all import model_info + endpoint = model_info[llm_kwargs['llm_model']]['endpoint'] + response = requests.post(endpoint, headers=headers, proxies=proxies, json=payload, stream=True, timeout=TIMEOUT_SECONDS); break except requests.exceptions.ReadTimeout as e: retry += 1 @@ -148,7 +150,9 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp while True: try: # make a POST request to the API endpoint, stream=True - response = requests.post(llm_kwargs['endpoint'], headers=headers, proxies=proxies, + from .bridge_all import model_info + endpoint = model_info[llm_kwargs['llm_model']]['endpoint'] + response = requests.post(endpoint, headers=headers, proxies=proxies, json=payload, stream=True, timeout=TIMEOUT_SECONDS);break except: retry += 1 diff --git a/toolbox.py b/toolbox.py index 3688a33..dfd3d1d 100644 --- a/toolbox.py +++ b/toolbox.py @@ -36,7 +36,6 @@ def ArgsGeneralWrapper(f): llm_kwargs = { 'api_key': cookies['api_key'], 'llm_model': llm_model, - 'endpoint': model_info[llm_model]['endpoint'], 'top_p':top_p, 'max_length': max_length, 'temperature':temperature, From 7c050d66c8b72e92ef2255c4c2a9b02f7e63ab1d Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 22:52:20 +0800 Subject: [PATCH 18/50] reverse --- crazy_functions/询问多个大语言模型.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crazy_functions/询问多个大语言模型.py b/crazy_functions/询问多个大语言模型.py index 4457e56..95712ae 100644 --- a/crazy_functions/询问多个大语言模型.py +++ b/crazy_functions/询问多个大语言模型.py @@ -16,7 +16,7 @@ def 同时问询(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt chatbot.append((txt, "正在同时咨询ChatGPT和ChatGLM……")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新 - llm_kwargs['llm_model'] = 'gpt-3.5-turbo&api2d-gpt-3.5-turbo' + llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo' # 'chatglm&gpt-3.5-turbo&api2d-gpt-3.5-turbo' gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=txt, inputs_show_user=txt, llm_kwargs=llm_kwargs, chatbot=chatbot, history=history, From c5d4e75a7a23d2edcdfe4f53bb2b787418ad6ebf Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 22:54:28 +0800 Subject: [PATCH 19/50] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/询问多个大语言模型.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crazy_functions/询问多个大语言模型.py b/crazy_functions/询问多个大语言模型.py index 95712ae..fb78145 100644 --- a/crazy_functions/询问多个大语言模型.py +++ b/crazy_functions/询问多个大语言模型.py @@ -16,7 +16,8 @@ def 同时问询(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt chatbot.append((txt, "正在同时咨询ChatGPT和ChatGLM……")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新 - llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo' # 'chatglm&gpt-3.5-turbo&api2d-gpt-3.5-turbo' + # llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo&api2d-gpt-3.5-turbo' # 支持任意数量的llm接口,用&符号分隔 + llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo' # 支持任意数量的llm接口,用&符号分隔 gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=txt, inputs_show_user=txt, llm_kwargs=llm_kwargs, chatbot=chatbot, history=history, From 9b0e20c96d9f70d7e9e61ef32a38077ac1a1be9e Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 23:09:38 +0800 Subject: [PATCH 20/50] update --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 213f3e7..810cc1b 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,4 @@ private_upload other_llms cradle* debug* +private* From 48cf5c0c9c7f5122129af6be2ad63c88706f8177 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 23:16:12 +0800 Subject: [PATCH 21/50] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=E6=8E=AA?= =?UTF-8?q?=E6=96=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatgpt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index 719a14f..9c4c3d2 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -169,7 +169,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp while True: chunk = next(stream_response) # print(chunk.decode()[6:]) - if is_head_of_the_stream: + if is_head_of_the_stream and (r'"object":"error"' not in chunk.decode()): # 数据流的第一帧不携带content is_head_of_the_stream = False; continue From b5c4cd2f1035b7954f5e94579ca48fb59a752811 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 23:21:12 +0800 Subject: [PATCH 22/50] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E8=B6=85?= =?UTF-8?q?=E9=A2=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/crazy_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index babad27..68c8072 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -105,7 +105,7 @@ def request_gpt_model_in_new_thread_with_ui_alive( if retry_op > 0: retry_op -= 1 mutable[0] += f"[Local Message] 重试中,请稍等 {retry_times_at_unknown_error-retry_op}/{retry_times_at_unknown_error}:\n\n" - if "Rate limit reached" in tb_str: + if ("Rate limit reached" in tb_str) or ("Too Many Requests" in tb_str): time.sleep(30) time.sleep(5) continue # 返回重试 @@ -234,7 +234,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( if retry_op > 0: retry_op -= 1 wait = random.randint(5, 20) - if "Rate limit reached" in tb_str: + if ("Rate limit reached" in tb_str) or ("Too Many Requests" in tb_str): wait = wait * 3 fail_info = "OpenAI绑定信用卡可解除频率限制 " else: From 05c74e66e7c99461878aea2ee5d203ccd7459ec2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Apr 2023 23:28:31 +0800 Subject: [PATCH 23/50] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E9=99=90?= =?UTF-8?q?=E5=88=B6=E6=9B=B4=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/crazy_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 68c8072..29c58b4 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -175,7 +175,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( except: max_workers = 8 if max_workers <= 0 or max_workers >= 20: max_workers = 8 # 屏蔽掉 chatglm的多线程,可能会导致严重卡顿 - if not llm_kwargs['llm_model'].startswith('gpt-'): + if not (llm_kwargs['llm_model'].startswith('gpt-') or llm_kwargs['llm_model'].startswith('api2d-')): max_workers = 1 executor = ThreadPoolExecutor(max_workers=max_workers) From bf44dd1d41f0d17235334a3595cc8d13f1052e88 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 18 Apr 2023 21:04:29 +0800 Subject: [PATCH 24/50] logo gen --- README.md | 10 +++++++--- docs/logo.png | Bin 0 -> 11335 bytes main.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 docs/logo.png diff --git a/README.md b/README.md index e0d6fd8..4d2f79d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# ChatGPT 学术优化 + + +# ChatGPT 学术优化 **如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发issue或者pull requests** @@ -68,11 +70,13 @@ huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/ -- 多种大语言模型混合调用([v3.1分支](https://github.com/binary-husky/chatgpt_academic/tree/v3.1)测试中) +- 多种大语言模型混合调用(ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4, [v3.1分支](https://github.com/binary-husky/chatgpt_academic/tree/v3.1)测试中)
- +
+v3.1的[huggingface测试版](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta)(huggingface版不支持chatglm) + ## 直接运行 (Windows, Linux or MacOS) diff --git a/docs/logo.png b/docs/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..567dee1a957833b9a925cf709c6ebedce482bd43 GIT binary patch literal 11335 zcmX|nbwE_#6E7{X(k;2f5(0}Lxspq#NUL;7DBUcuv1;J0h5t{Cu#z**VI z4FiLp?fwVT`-8M61_ld;3S3s#J9{sO$eD3A;MaIliyyN$S+Oz_hk%ToBNEJ#NbH0v zwvH}MG}L6zLYbySq)cg2E;!j1qZWkMR2*6~VJ`N5>ncL}z3Si}_O#QUJoDMc`b7~= zsKi~J0`|nhN@Xnao(D487)QAra*fd9vf^cx7SXOx-u0Hr@oiZz>i0`A>Y->i?@hEaBA0ON< zOR{k;f-+@OkQ-1t%yS=?A?x6cGzDgI0Y+Y=axxe#e67^@%SiyY{zJ)-3rv6;-Cr{V z5$n2V^y=(LGB~{CP%_k;Ad01%!56wzRep0t_sS1GFTl`sK)H&`g~S)y?7E@1{@pKk zlPP#&4p~2!OWHyJd52{B8v2DJR+U~uH)uC;Pg(tIBzEfO3#Z~@F9qleg@WI_hWwQ(pkgN!=z zX`lCyN`lDfD7!+*3(HNlmuZ>6A>ecwxlDi!AcL}toh$)hVisYJlqFNgARE2H#q^}* z1SV)HLjEH#LCTQDunGct;ATPyFchGQ>?}Jplyl~^mfAR$GFWv8*&xPnL0koBx7myU zsWV8xCLdps5?D{12@%9pfLg%0cls;Tt1cBy-wtnRzSW3R<0IQydy$f0s+l4nCHN}R zmjPE2#6}!t29H#q5kOmaG&NYxj+RYIN=$yXw#hT0R@sXEeEfNwDK8*S-JtL>J0SeI zHjcbzaxEfIVybC}!r<$kh8z3Z`oX`yKUc^9p`}exwMRjg-efom&|)~xPTETCzw3|I z*LbM}vrTPNa9M_y-)Twl?-OaQ$z)I1$ZBoA+bq90+Z)$}txlg}WAq=&;YXPTO@v&&9x8gbUW*_N^l>^r z9{0JG2u#WITP+Z^U90~ZzM(Q8Lk0>G`+H>+0MciJ#=qvum3W;PHEU&e=B00a^;Ll8 zi(%|U>%kW4f#tE`BEBbWIN*xR&sX`c3xDm{dQe?#|2{6udnR#PoEsEy5-@y*a1S`6 zuG{HxemXahTx%M+p(2xye8($TDAw1Gp#!vBzQA^Cx09w+!IH*^?q=UVHE!tW+7TeYR)p4Z|#{#8p}A$Jlg z$b}^C67p}jj3qWdr>GzD-|%Xe6CsOx#91gda`l5u?x|Ka`aLTbQUs+oGtM0-s!uOC zbml>*>+HHkQooZ0Cgh&i+i28)Q)-bukMV6nKFWXTO7tu}P{oL@&}S8kQZ{;-u6?G_ zaK%49of=Vj^9{Bx=>_{hal63$>mE0=U%@WE815li@8)+D>^a#K%)9;E;vFs5 zPYiBLDxAX0EY#%eWHEyC+FsKCE222jXR0YH)|XT(orWQns?ifXTu4Ii@G3lCBk5pQ zrFTZ2@~K7H^Bi%n9-Focn2Ko;HXL^|Z|H1QomuPua`CWfNO1t&tq$#|-L;G3`gCYh zIvr^Akuso#fJ7B+B<=P|1_v?0wR1il{OSNAYBOux1}+CJhUdRs_1w~a~G6#K5OVFVr%_eY?$7CKJ)O{WO=-*Rg))i zb6YOaJwvzG5?)I6*xxg|i&*|4IX6;e{OV2j3ATQ()`zla2fwqvF-+e0dd-W~GB?}Y zRzsU}T8PR)qUvd=W3D^F&!xr1FP5KPZXOJ}I80f}tvDuqt`$DL`SAU-v|oug&#Eo! zPB3HmLv~dh!JQJ!zgMy?(K=4nX$!N(EgcPQ0YW+s)gm=C^uJwGtA zgr!(0jyT)}!#!#HbndBSpY7L%G+aEzuB@5fj3$g+TJcg{GiBnBYZ8$ac5cR+T2<>W zVUG4`a(He;wapw$DrCWdOh26r`smuRkhahg_}a$IOnhmcOS7a(*B0Sk=|5klG#+0` z-JE6mBD%}=W1J5w0&Y~$-q^C^t3R8h)!}#77Gcq>G&i8DBLT8x1DH946BhH{wnIGK z7ac{?B#p{b4*UWTJ<B==NS=N}lx_W!7x9mWX!d;|;M>`#& zSNN;{ok_}k_bL%-J0Oup(hotkh>5Yn z^VzXcRG88de{8G)`%$jPR@vVka^qa1gNM0gGwudR?b6QUhM*nEOOc;`85BhN#&6WW z;(8;XD~#dWnA$Cci{`Y(WcLQ5u0=@dA2J#T68}34y#AdbI?Y8mvcA3NSL68tlkI#~ zvkXK;sQ20a^vR)e`Sq#t2a!bgQW8nfAbl?kWfft*2`8;X(9emXMf+cj9&CNpq8S&p zJ<9)c5IW3FC@Dj!WN_&Exqfs^rhhXaw!Zq1C>Zj23PYvjx*Dzh>m%~ETRC^|OR zV;H>X-WxDbH>oOF{o;3^rI13!4gJ=sD25}q!uQNxp%wXJ}QiJ31_b!s=gL5MfytyQ0TR{>icVQ~4# zp2*CNCdn8eRc8)7t+g;&9~wzVEIlI4bVzNu6Sw-ipG*Bm4~?xaer}pJNrg4X8>(a- znRqk1@?9)?cUfJ}G#TL*;5&^v-O^)ubvd{_qSep)qH+X?kPoHo+X`Wwl*kuHObq;S z4gm}Z0@`PU%z0B@E!Hw1+`BIvjNyaqW$6$<^bd1dWkY(uTVdzGws0X4<{ERF8yk{;_xa={MM9m_SUL{xa7XP)>j#R6!fnvRIj&A5Z$F4Z$ zBF3r5ccKbpny_CkTrGNDJS~)5c&xp@fZb%$Rs)-Xa`|(;w?Cz2!r-9rjwmPr&yjk~ z@61QTALjYq8YV{?WA+?w5&a$>eH~=N*gf?)l;tAO_jToXRryM=zq#%^=5Hd-7`nH@ zt#73FpY`DUFyBN0mx&hkBO_^j+7xPGw^i5QLv9q)ym>$08?C{$>7$<;RbgZ$s4)>* zY!OhL6p}p$lPeK;oHSR){S(F-Nh-bI1WckH!Rkh)yXzd&{Mbx-r=Q$8n031VLTw!1 zVD{Kh@IgISbaCoNYpjW$1H4pKXVhLLbdfqqAlr*=%;f1$;h>E{aC;_X1WxLAn_1%( zUiD{P=Z&+JC~poTiR$y!*EHTbMZcGapDx4hM(nF%@v1Q7f8QmAeN$9g*k7yr6INHD zi{ffmfs>+p*3=>j9R7<`{yKy?+VSaDvV~rvqHRxDK#xP(iF&)qq21lBQ16MNpO@n4 zzr%CWCf$syZ-&-UTTT6ETm#o(h2Dbsvm_L`p0pCwvLjb-I=Oae^jVO{6|wWvZ_lqe zqZ@vB9fz%w@av^YogF13_O{hG&P8n)zYfVXURTa;C_3gCfweWAa*kYjZDX5#68{Rq z3ezFEF)Q=`@mrRi5ttGKNhe(y=3OKd)wvay{tNNac+mgsOI^{k;TOY-iWu<~cgPJC zq1#DM?SH33Qin)jNmG>%ESP2!F~{{j85XKj1l}%J-sb+>AB*$kHN|6Ak3Q5cdkASo zYySq95zD{$&j?c1x8M+&rZnk#$4~Om%1>9_-{zZ4)%EziE*a;3q%PEnIN6!q*cH34 zbhL`+fiPbcM=^#IHSR;FZVP&WbcbnmD@Vd1uEWMVg5zAOTQ*N7v;0{tr8EQI^ROrB zL`9C+la_zA8LfTtfTj*Hij#Fp#F328u3#_>whK0mH*l104|X}GN*bS^>e`ywgkbDK zU-h^|tdIlRnF_HEUxfuXUsSLpKkl(M`!}{bW>d-(Ypcqxrc<9aRm`4^Sswet9ADE_ z50+|nVzr`7>n!T<6S>GhCb3~W*n2-=Yt-N^r%bY8zQi`yy!c>gG%14Cyc;1JJDFKj96`^ za(&WN@p&XX@exPxvz_{Cy#B-WJfl+#uAhRf$?oHwNoG%Z&3o1hJ;L98$2?klBdwA^ zWuqiWsbL`ieQ2RDMSmIJ*7p(L*;nxuPiM8#;5U!ZB8T5f=j(tAbVvL)-5vaBN`?+l zoq9)bO!7g;{3QSTUdm&cn`IdO+zZ*GGWj<&D_eM%f|IM_!m(@beEl7gVpT|Uzb|bK zr$;S2rr1RbaO{PKoXhZaM5OD8YAw^iZptB9j8RFg1}Ys>fx4M)NECs>>>9s(==WUG zMGdh}RfYP)@u?!0Yh`sAewCw{9|_vsT-y&1!)lK{yTU$5FU)^yZx5;ai+>xar1s&# z7_}N6>n{E01nyoKf#<&`cEHVJU4YgWeayHpZ#Ht(fVFDi<YV!Nu)X|tv-cFOK+b3a`l)X{QYQz zqYdl+6H;Ur!2^*xMEGHH`*Bz0SWK^vDQ&@ZU>Z+KAVbvv<~CoG?l$(Y&)8!Ir{B${ z5`v~s0-Ll*E)Dh{u+K^B#At5o-Tma8`ISh#Pr#jsRGb-qs7XlDx=}G`f4ZM^f-C=r z9$w8EOMzk!AQn|&4CliIeKEEIi>eyLw}1*S`0}$(-J+)3I`gZmP4O+nihAr*BUve{ z5rs;dza|D7p5~=tRXi=KTW$51dg4y}Z&R@I0k`-$yOVNP~!i zHzWmRHg6O_=V5)Y&jv3a4v~?-FEa;LZ8J~muwi$1r)Dq5Dt)?;vz$CBEhafx5zpJI zNWT0|T^L}JOIyk7Fyh+YqmgK`5vnOYjcWICRi8f>8j%qaw`4!8X(>LxdV|@4FIKy# zve+A`RY4vqJ^v%MuJ)e+H3#=-%>pDD_prym;OHAQQ@?bbU{XyTGr{cTVpf>N8e(Nu z#JcV+eZ`{$5?g5rKTgj0f#`e`oo-+J*SB3kIqY?S5NQ^YU&W-eA(e&e|J#@A*2jin7|A_rbvU3@tc;eEgw%-eF%&~({1O=w14W=9p=?qNfUQ1c-4Ne-dgt1N0e7vzw;=cEy^1l!FA|x{Ofz!a0Fbug;?w0v2_x#i{v%Q=@9Eh07D(k!WJGj=%Ah!`3b8-81@QO8PacnSpET zHv{L2YAWK`p&xu!FEBXDa5$00mttB##<5`Cc|d%4`*OkmC0~eGz{cio_LbdcD#ce1 zfytJh<;fQYt==RyMTkSDcL6>&Gh=LbYaQRdy{#XL`TW9BF<7^(AE=S$M(3&KUprRU zGfKejuEd`&itAPGbLx@dzMtDXUE{u8lo@jc5$KB^j6|;XT4*4fuiG`_>MZH)dQA%l z8W{V^WahBzTlz2e>UzI>d3kQ$`OzDW32~Q2(l&)*=a+lYHb=FHu|mlhBw)lyF9@sE zxkMb>j_aawyH49HZiCrGV?LBL^o!pui>rk~Pdz8QRq|XE)q@5uRr29y5>%u%ZEfmv zLiK+7HB|Z;>2zkuxi-#4NaMDzA}U9GeeSvR!vi#@rFVODUep0t%-x$|4AmA(>Z)|o zkB5T@$G;gtsWxOK^8~;X{h7x|a3b0e@0*0K8PIJlrR=hm2g}|cEK+etT$6oSRl`&- z^iwpMPa%X?VEppU)zl7AKF#LlCN6CBL#!o#49dyIXbOLdF1(|b(UBuY$_sAr;S0!6 zN||yKp#|;uQX#+ZZKp0Y+E&JUKFSC4-==xqxH|qny?4fk7S@C5naMXgrxy`jhyN{A>1(^6N|LFMW2sfs4cG<+LwDdeW zsup10&kA>)d|h7G_3xfkrx`CD{FD*4YYnI9^^5trfRa^Wz+erv-Q3 zJQNGfmd5_~*6_8PHIJT2#&Aqb)ar$MO|6vchRU=3?%fNeI=z1RwKLIk^LSEy6~*{y zO>O^G*K|_062)ox##Z+lux-cwK)gy;Q$gJP9;& z{doN8NFa`KWx!r;(-k~)9d;y!F_a0^RQ?1WFaePz%({MfB&k2{7m%fn7+~3d0vLP= zndrwI6dRNTo6CKs@}wKTK-eF>Reg@0Y`-G$az8`ZlqSSs7_Txv-X-X+$?LHQCs+J4 z@`W&0PjgyGmWAOppQC1QRrI{2bR_+V z<)bm;myd0O2$CcCM0N+JMw2{zAIOAJlXeIP%EmRoK1dE`LW5hF& z5{tLt^Pvx(hmsN}M-)bu@avUI2T@t+C9()yzU1}(Wv?;I%MzrC@BtLi)L}aa`vD7v z)CdPMQL{6(Vcqz(E0M`>Y@jOU%_RZx8K0)VTe5^(j9zC3_Ss$-_wK5L<{~>T!`{5? zNEo%T&v`XTnfsl_%fr*t`mY9Q!zKNqLn@n}%}M*?_t7lN4jXsv4i7D8iCbx;pUL1b zn_K4^Qp&460|paE`Fh3W9K@uj#a(td-idRxG3debm64p zE>j{763a+Y@=}TIMojqE*m)R6bZu4 zY%e6%FG)N-GBsiiOIvH_lrBP=n+Ae&;&uPJD2M ztV416HYDk${)ONam8<~J&2lR=-1o4~g^pb{^==C$jQ$B1#sXt#2-}pgrp9+mw|uTc zJ01SeT*{*}!7uO+6c`_=ZT-kep27C?O*imGWwh9<#V`3(%NRZpUvBz-M1>q8Ihe9- zJp7eRduAN4S`qW^cKwyu1E(h?ws0ReM&!V(z#pHo2(lWk=NuxL=@t1*J!7lJkHaP= zsq&dl2b0dzvmh%p9X9;D*I}unNw{RXtNhi+3;UC!cu|hI>9H%*MTFJImabf1X{uqu zuSoRE=!+x*N~+0iyO)zC=IZ<%8+9#OAJ|3E*8w(iq3IX|7=7HEaX*Au2HV~u;gc`| zm6>rKljiKlS;ZB*=854*Y~F)GqJDb)Ft2w*Hsw((NOSaDn`f}w3H#ORY|IudQ{-}v z`QSA!YSG-V@-0|*`;>R5AC&w3&%5p2!`1B9`n-JU6}Lmcn^Vzv!Z_bsa&lhcC&SZb zC(i<%#m7&*ocZ_`S*D;NAU1Cj$x%#SEmY(&qTsT~$JvEO8Y--{-oH}(j8Jq^Q{^qT zQstI%!mpy=_P|a3REeFty3Rfa!zEtn%h#_plya%77GG#C!hjE5-`8O_=WLTZZl z*&V!S`(EB;a&wh+mpuCCj}j#JdQnUlOpdTr-9FgZ+~^}o;Xp|ll-i7?^r{#4yf68C zEm16A|-PlT)V_up&!?Ox2>AzzKMm?j5S7^zS2J<`Yqxx8X2SGda=c2 ziyf=o`_3rpZN6@xyYm;$4+!IOD_dTRV*9(j@b`~g{8Fo#!b^sNgEHEfw6;($Q@5DV z&DUXDbOL=l77sq?1+V?uqPdv;9*SqZ9E#h7o`61n2IMmlYirsp?1?JP1dnG7^A_xg z(O2XSiz{**N)%hs$_ndwe0s7V^iuV!6!u8_&OjD9=B@R{jpnOwausJIFTix)#Kop)Dh*(ZPrF;9&jYrrXq?P{4T#3HLvTc^*SFt)qDr-Vy zKrg{Uz^9e+YtVJ_ZMSw7gazRs?y3h4c zyo+A{`|8y(_HPgd2GQO<7T`w(&9_maAJno`y(;aZjvE9$uSMLf)I>N$CH#mZS17_% z`RzAQOSF4`GG;U~)>#6wCLc<28!x1;L`zP#?ifxcbLG%geoKgg7q*Yv`vxUhlIaqt z_3B&B2t>|4fLtEQi5q;LN%V>-#2slSlpVPHiMo_KVdJyH`R6;)*lxlfiJ>I<8A{oq z*a^${lrV_~`$!^9jfP8Ro2X^Uo##tUfR;Js3Hu{%$UKQUn4c$>gJjm-m7CY&B`UUG zo2Np}?L!+~d-BIAp%9g1<&{L`F?f&4m!F(yIF25r;7vXA25K7Ubm#kg9}2;4dYrx9 zcbpk4{3SHVgRXrOXiiYLo`k+mp(dh)T$)skP(XmTx_7DJV4+W8r*~h5xkrH2L{!U# zMvY?Mc;vdS@(9I&s-972bNaN#jDRwAwv{0rCqrt=*+^z3TTo{S&$a88+IGW~_7kmFz#C)<_|xi_!lh`S7Z3LU#Xr?wD33iX73XwsjBL5UOotfQe_xQ29Q=4{ z=y|Asqn8|#%$Ef*0df)6D8-)&(Fx{@4-_Y1bMXT%+($DYQ~a`vr)x<*-MTlr{#3HH zcDaiMkp-LxVErn*iRXr}?=aCm_wc`3S>MoQ8|!2KQ@r?9e#G;Hff4b9YzxO+!?+f) zE-y0OZ`)9i1kmq)z6Z5Zu?&8*X02qSa1=IJN_*WVt@^>qsGU^r$N?{VeNykZS?#3f z#nB4_bsW7;*qf4diXENr@4`8DXmG^B)3p7Eu5iQ9g0OxU8VC*#GM*m@! ztWx|nXPW|w{fDs33A;E>0su~Uw1KKxEg?_Gm-4j&o#7m?g+8W>V&?mnV#`~s#Lj-> zp~pl%NG#t&Za#2@8!Q{aT6h17Oe3psvyXLRaNx#6z9IusDlP7x%_3q=*oMME1c1lk zcdF=1fW~pT5j^dvDr<8pg)&2PO*QgR)u;Nw}(%%ENGJB}SdHsF8 zX)O7#!S(syY@nM5qmhODcX)2^Qj`x;jCuyBI)u&x{kLei&H%DBRST zowfnu4~!%|UIS=jq$0#e6efs{95(#JD!t3#Gn_wRUBu}3WOe1tM2~^a+{sYsVL#$0 zn-G~}OpCB&yGec~q)4y!$(BW=r8g$UNqHVcZF2JUq*bfI`)tj)&Z48G-KEKs#M$$0 zW1L&+4jUQJ63;giVXX-m0kYWYw!B;BCR3a^eh}Dg>;Ck!7IIl$;QGUWTv{cZar}g_ z&Y5o#sf=E2b&-M3j;sm=*fj8yynshl#v1X0)MZt*elQpqvKARR`E)70mYL6B^z1D{^|p>Tv$z@);KG5 zm9+{^5s=5T`MxvVtLFj&zTfe+Z!+l&&WQ3n-N@w0{<8f?07|nPZvpl^bP$4zI)K&_ z0t=l z9YPJW|NIB2iB$H;0WP9d9<%c8paZy(S=ULrX}yk!cA@1-^~CZcS9}~njnT{_yk&B) zPjKMS+A#sOYP54gqZcjEIZi?3#`LP+WCjOxAJ}C;i2RV|#4^_eKqZ&F<-WSmKw1b~ zr3?ny>tV!bYOf2xo4h!j-f7>nry#mlgRgvj;BN!nmOrwmDkF?ozAZAwK6#PqZw}r= zPudxv&&7IgJ{KTU5_5nbk7C^uqVDx-AT21*%Nr=`nu{N;0eaaq1ixvSfVGj%GvjKh zQ*QHo+RFaf;buUExZ5K4{v-tluWA(QvL-ZA^oKLLRIu-9O3^?QO%3&CW}GivfhbN* zFG<3>5Eqbq)PR1krYAj}nyYm5k{#1Xy2a2w!~A(w{00i^e%c)bRMPp_p*yzWzZ1l} zFY-TV3?a-5M=6j%)f@U$OQ+t`K|dX!z2D;;xipnP++wRN89G*7fI$#2AejMUIN&>& zb%;zcsm`Cn=vfyK(A-NiD?`86dE*u?fDl8c`L_*{h8Rg}FaUYh7OXr0GrE^8$cRP* zhYNI*L+77o=SJm6fy2)Wd;q~b#v6lC!Uo7jq9{{FpyAMc!(d?w*H&;zlm)#z-cyh# z1jvJl(Senb-MPZ1eT{1Wty3u<8yOh81I8$3fg~BKh@t0YR(6Gjx#GVLY*SY672nJwPOh6$K`OZjM6t07#cW>ZbD2N!a!KwpL$V~uV z0{$oE4hh&3Yw)Uxl^P(m^al1a@e;aIqLL4g_Nd|zWJ1(BiRHiCZ%^Cmc9B$)Oc%&p z3cxZ(pg+bw#kj=PLDCn>v-2l>E-D`|C_iA4v(EB7&Vmtu>)HS3O26atq(+b}w%~$~ zbh|w75jh)@$1ZmYfKNC9s>-p~BHk)zUXk z6lkds)r?;?VJb{;-gAoikCO`gKTgDeJtWLGfkkcXS9x9pfC2*nXYhUQzuiN3{$O(!?Sr6hM?+k$|HY06|X%pbj~?G>@_EaxaK&ZovSA zaHLZZwggb90~FkLx!!m-w-$K7FyJsB0hPn^@i{SiVZnn*l1X zN3K|Nf$8hR5*i;dK^^&eORkii%#3YzdrIWy${0qHK$>Hq)$ literal 0 HcmV?d00001 diff --git a/main.py b/main.py index 5baf655..723d5d2 100644 --- a/main.py +++ b/main.py @@ -182,7 +182,7 @@ def main(): threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() auto_opentab_delay() - demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION) + demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png") if __name__ == "__main__": main() \ No newline at end of file From 40d91e9e1ac72ab3c8105a6c72b2e0a548a8b7fd Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 18 Apr 2023 21:45:56 +0800 Subject: [PATCH 25/50] improve dockerfile --- docs/Dockerfile+ChatGLM | 3 +++ request_llm/bridge_all.py | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM index 197ca1a..4a11f06 100644 --- a/docs/Dockerfile+ChatGLM +++ b/docs/Dockerfile+ChatGLM @@ -36,6 +36,9 @@ from transformers import AutoModel, AutoTokenizer \n\ chatglm_tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) \n\ chatglm_model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float() ' >> warm_up_chatglm.py RUN python3 -u warm_up_chatglm.py + +# 禁用缓存,确保更新代码 +ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache RUN $useProxyNetwork git pull # 为chatgpt-academic配置代理和API-KEY (非必要 可选步骤) diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index b5ad5c1..ecc6b1e 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -23,6 +23,9 @@ from .bridge_tgui import predict as tgui_ui colors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044'] +get_token_num_gpt35 = lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())) +get_token_num_gpt4 = lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())) + model_info = { # openai "gpt-3.5-turbo": { @@ -31,7 +34,7 @@ model_info = { "endpoint": "https://api.openai.com/v1/chat/completions", "max_token": 4096, "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), - "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), + "token_cnt": get_token_num_gpt35, }, "gpt-4": { @@ -40,7 +43,7 @@ model_info = { "endpoint": "https://api.openai.com/v1/chat/completions", "max_token": 8192, "tokenizer": tiktoken.encoding_for_model("gpt-4"), - "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), + "token_cnt": get_token_num_gpt4, }, # api_2d @@ -50,7 +53,7 @@ model_info = { "endpoint": "https://openai.api2d.net/v1/chat/completions", "max_token": 4096, "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), - "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), + "token_cnt": get_token_num_gpt35, }, "api2d-gpt-4": { @@ -59,7 +62,7 @@ model_info = { "endpoint": "https://openai.api2d.net/v1/chat/completions", "max_token": 8192, "tokenizer": tiktoken.encoding_for_model("gpt-4"), - "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())), + "token_cnt": get_token_num_gpt4, }, # chatglm @@ -69,7 +72,7 @@ model_info = { "endpoint": None, "max_token": 1024, "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), - "token_cnt": lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())), + "token_cnt": get_token_num_gpt35, }, } From e2a62ec4092dbe55c4e24b7a7bd2cbc192495a31 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 11:09:35 +0800 Subject: [PATCH 26/50] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + crazy_functions/crazy_functions_test.py | 62 ++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 810cc1b..987f054 100644 --- a/.gitignore +++ b/.gitignore @@ -144,3 +144,4 @@ other_llms cradle* debug* private* +crazy_functions/test_project/pdf_and_word diff --git a/crazy_functions/crazy_functions_test.py b/crazy_functions/crazy_functions_test.py index 4df55a8..a958bd3 100644 --- a/crazy_functions/crazy_functions_test.py +++ b/crazy_functions/crazy_functions_test.py @@ -31,12 +31,72 @@ system_prompt = "Serve me as a writing and programming assistant." web_port = 1024 +def test_解析一个Python项目(): + from crazy_functions.解析项目源代码 import 解析一个Python项目 + txt = "crazy_functions/test_project/python/dqn" + for _ in 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_解析一个Python项目(): + from crazy_functions.解析项目源代码 import 解析一个Python项目 + txt = "crazy_functions/test_project/python/dqn" + for _ in 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_Latex英文润色(): + from crazy_functions.Latex全文润色 import Latex英文润色 + txt = "crazy_functions/test_project/latex/attention" + for _ in Latex英文润色(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_Latex英文润色(): + from crazy_functions.Latex全文润色 import Latex英文润色 + txt = "crazy_functions/test_project/latex/attention" + for _ in Latex英文润色(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_Markdown中译英(): + from crazy_functions.批量Markdown翻译 import Markdown中译英 + txt = "README.md" + for _ in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_Markdown中译英(): + from crazy_functions.批量Markdown翻译 import Markdown中译英 + txt = "README.md" + for _ in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_Markdown中译英(): + from crazy_functions.批量Markdown翻译 import Markdown中译英 + txt = "README.md" + for _ in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_批量翻译PDF文档(): + from crazy_functions.批量翻译PDF文档_多线程 import 批量翻译PDF文档 + txt = "crazy_functions/test_project/pdf_and_word" + for _ in 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + +def test_谷歌检索小助手(): + from crazy_functions.谷歌检索小助手 import 谷歌检索小助手 + txt = "https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=auto+reinforcement+learning&btnG=" + for _ in 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + def test_总结word文档(): from crazy_functions.总结word文档 import 总结word文档 - txt = "F:/AMD" + txt = "crazy_functions/test_project/pdf_and_word" for _ in 总结word文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): pass +def test_下载arxiv论文并翻译摘要(): + from crazy_functions.下载arxiv论文翻译摘要 import 下载arxiv论文并翻译摘要 + txt = "1812.10695" + for _ in 下载arxiv论文并翻译摘要(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + pass + test_总结word文档() input("程序完成,回车退出。") From 57b8ae32753e23fef29531aaf40000e4a277706d Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 11:41:37 +0800 Subject: [PATCH 27/50] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/crazy_functions_test.py | 58 +++++++++---------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/crazy_functions/crazy_functions_test.py b/crazy_functions/crazy_functions_test.py index a958bd3..1c50724 100644 --- a/crazy_functions/crazy_functions_test.py +++ b/crazy_functions/crazy_functions_test.py @@ -34,70 +34,52 @@ web_port = 1024 def test_解析一个Python项目(): from crazy_functions.解析项目源代码 import 解析一个Python项目 txt = "crazy_functions/test_project/python/dqn" - for _ in 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass - -def test_解析一个Python项目(): - from crazy_functions.解析项目源代码 import 解析一个Python项目 - txt = "crazy_functions/test_project/python/dqn" - for _ in 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) def test_Latex英文润色(): from crazy_functions.Latex全文润色 import Latex英文润色 txt = "crazy_functions/test_project/latex/attention" - for _ in Latex英文润色(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass - -def test_Latex英文润色(): - from crazy_functions.Latex全文润色 import Latex英文润色 - txt = "crazy_functions/test_project/latex/attention" - for _ in Latex英文润色(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in Latex英文润色(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) def test_Markdown中译英(): from crazy_functions.批量Markdown翻译 import Markdown中译英 txt = "README.md" - for _ in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass - -def test_Markdown中译英(): - from crazy_functions.批量Markdown翻译 import Markdown中译英 - txt = "README.md" - for _ in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass - -def test_Markdown中译英(): - from crazy_functions.批量Markdown翻译 import Markdown中译英 - txt = "README.md" - for _ in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) def test_批量翻译PDF文档(): from crazy_functions.批量翻译PDF文档_多线程 import 批量翻译PDF文档 txt = "crazy_functions/test_project/pdf_and_word" - for _ in 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) def test_谷歌检索小助手(): from crazy_functions.谷歌检索小助手 import 谷歌检索小助手 txt = "https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=auto+reinforcement+learning&btnG=" - for _ in 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) def test_总结word文档(): from crazy_functions.总结word文档 import 总结word文档 txt = "crazy_functions/test_project/pdf_and_word" - for _ in 总结word文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in 总结word文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) def test_下载arxiv论文并翻译摘要(): from crazy_functions.下载arxiv论文翻译摘要 import 下载arxiv论文并翻译摘要 txt = "1812.10695" - for _ in 下载arxiv论文并翻译摘要(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): - pass + for cookies, cb, hist, msg in 下载arxiv论文并翻译摘要(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) +test_解析一个Python项目() +test_Latex英文润色() +test_Markdown中译英() +test_批量翻译PDF文档() +test_谷歌检索小助手() test_总结word文档() +test_下载arxiv论文并翻译摘要() input("程序完成,回车退出。") print("退出。") \ No newline at end of file From bb94ad387f2213bb094329e74fd9c07afd95c5d4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 11:45:34 +0800 Subject: [PATCH 28/50] =?UTF-8?q?=E4=BB=8B=E7=BB=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index 620acb6..8522aad 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.1, "show_feature": true, - "new_feature": "支持ChatGLM <-> 支持多LLM模型同时对话" + "new_feature": "添加支持ChatGLM和GPT-4 <-> 添加支持API2D(国内不需要代理) <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加输入区文本清除按键" } From da1b42803016715089d84073b2b3f64511b142e5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 11:51:16 +0800 Subject: [PATCH 29/50] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functional.py | 23 ++++++++++++----------- version | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/crazy_functional.py b/crazy_functional.py index a1a4bc2..6f4d37e 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -3,7 +3,6 @@ from toolbox import HotReload # HotReload 的意思是热更新,修改函数 def get_crazy_functions(): ###################### 第一组插件 ########################### - # [第一组插件]: 最早期编写的项目插件和一些demo from crazy_functions.读文章写摘要 import 读文章写摘要 from crazy_functions.生成函数注释 import 批量生成函数注释 from crazy_functions.解析项目源代码 import 解析项目本身 @@ -19,15 +18,17 @@ def get_crazy_functions(): from crazy_functions.询问多个大语言模型 import 同时问询 from crazy_functions.解析项目源代码 import 解析一个Lua项目 from crazy_functions.解析项目源代码 import 解析一个CSharp项目 + from crazy_functions.总结word文档 import 总结word文档 function_plugins = { - "询问多个GPT模型": { - "Color": "stop", # 按钮颜色 - "Function": HotReload(同时问询) - }, + "解析整个Python项目": { "Color": "stop", # 按钮颜色 "Function": HotReload(解析一个Python项目) }, + "批量总结Word文档": { + "Color": "stop", + "Function": HotReload(总结word文档) + }, "解析整个C++项目头文件": { "Color": "stop", # 按钮颜色 "AsButton": False, # 加入下拉菜单中 @@ -86,10 +87,9 @@ def get_crazy_functions(): } ###################### 第二组插件 ########################### - # [第二组插件]: 经过充分测试,但功能上距离达到完美状态还差一点点 + # [第二组插件]: 经过充分测试 from crazy_functions.批量总结PDF文档 import 批量总结PDF文档 from crazy_functions.批量总结PDF文档pdfminer import 批量总结PDF文档pdfminer - from crazy_functions.总结word文档 import 总结word文档 from crazy_functions.批量翻译PDF文档_多线程 import 批量翻译PDF文档 from crazy_functions.谷歌检索小助手 import 谷歌检索小助手 from crazy_functions.理解PDF文档内容 import 理解PDF文档内容标准文件输入 @@ -105,6 +105,10 @@ def get_crazy_functions(): "AsButton": True, # 加入下拉菜单中 "Function": HotReload(批量翻译PDF文档) }, + "询问多个GPT模型": { + "Color": "stop", # 按钮颜色 + "Function": HotReload(同时问询) + }, "[测试功能] 批量总结PDF文档": { "Color": "stop", "AsButton": False, # 加入下拉菜单中 @@ -121,10 +125,7 @@ def get_crazy_functions(): "AsButton": False, # 加入下拉菜单中 "Function": HotReload(谷歌检索小助手) }, - "批量总结Word文档": { - "Color": "stop", - "Function": HotReload(总结word文档) - }, + "理解PDF文档内容 (模仿ChatPDF)": { # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效 "Color": "stop", diff --git a/version b/version index 8522aad..ff76f27 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.1, "show_feature": true, - "new_feature": "添加支持ChatGLM和GPT-4 <-> 添加支持API2D(国内不需要代理) <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加输入区文本清除按键" + "new_feature": "添加支持ChatGLM和GPT-4 <-> 添加支持API2D(国内不需要代理) <-> 改进架构,支持与多个LLM模型同时对话 <-> 支持多API-KEY负载均衡(并列填写,逗号分割) <-> 添加输入区文本清除按键" } From acaf8cdbf4718f30e6fd874de436701ce515122a Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 11:52:13 +0800 Subject: [PATCH 30/50] =?UTF-8?q?=E7=89=88=E6=9C=AC=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index ff76f27..0c32f66 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.1, "show_feature": true, - "new_feature": "添加支持ChatGLM和GPT-4 <-> 添加支持API2D(国内不需要代理) <-> 改进架构,支持与多个LLM模型同时对话 <-> 支持多API-KEY负载均衡(并列填写,逗号分割) <-> 添加输入区文本清除按键" + "new_feature": "添加支持清华ChatGLM和GPT-4 <-> 添加支持API2D(国内不需要代理) <-> 改进架构,支持与多个LLM模型同时对话 <-> 支持多API-KEY负载均衡(并列填写,逗号分割) <-> 添加输入区文本清除按键" } From 23c5a77f82dcbee1018a16b16e78640cb8b0acf5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 12:29:33 +0800 Subject: [PATCH 31/50] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- main.py | 4 ++-- request_llm/bridge_chatgpt.py | 12 +++++++----- version | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/config.py b/config.py index 1476f86..1cc057d 100644 --- a/config.py +++ b/config.py @@ -45,7 +45,7 @@ MAX_RETRY = 2 # OpenAI模型选择是(gpt4现在只对申请成功的人开放) LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm" -AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "chatglm", "gpt-4", "api2d-gpt-4", "api2d-gpt-3.5-turbo"] +AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm"] # 本地LLM模型如ChatGLM的执行方式 CPU/GPU LOCAL_MODEL_DEVICE = "cpu" # 可选 "cuda" diff --git a/main.py b/main.py index 723d5d2..c028e4a 100644 --- a/main.py +++ b/main.py @@ -95,13 +95,13 @@ def main(): with gr.Row(): with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up: file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple") - with gr.Accordion("展开SysPrompt & 交互界面布局 & Github地址", open=(LAYOUT == "TOP-DOWN")): + with gr.Accordion("更换模型 & SysPrompt & 交互界面布局", open=(LAYOUT == "TOP-DOWN")): system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) 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=4096, value=512, step=1, interactive=True, label="MaxLength",) checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") - md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="").style(container=False) + md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False) gr.Markdown(description) with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary: diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index 9c4c3d2..4990c1a 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -175,15 +175,17 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp if chunk: try: - if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0: + chunk_decoded = chunk.decode() + # 前者API2D的 + if ('data: [DONE]' in chunk_decoded) or (len(json.loads(chunk_decoded[6:])['choices'][0]["delta"]) == 0): # 判定为数据流的结束,gpt_replying_buffer也写完了 logging.info(f'[response] {gpt_replying_buffer}') break # 处理数据流的主体 - chunkjson = json.loads(chunk.decode()[6:]) + chunkjson = json.loads(chunk_decoded[6:]) status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}" # 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出 - gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"] + gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk_decoded[6:])['choices'][0]["delta"]["content"] history[-1] = gpt_replying_buffer chatbot[-1] = (history[-2], history[-1]) yield from update_ui(chatbot=chatbot, history=history, msg=status_text) # 刷新界面 @@ -192,7 +194,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp traceback.print_exc() yield from update_ui(chatbot=chatbot, history=history, msg="Json解析不合常规") # 刷新界面 chunk = get_full_error(chunk, stream_response) - error_msg = chunk.decode() + error_msg = chunk_decoded if "reduce the length" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长,或历史数据过长. 历史缓存数据现已释放,您可以请再次尝试.") history = [] # 清除历史 @@ -205,7 +207,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp else: from toolbox import regular_txt_to_markdown tb_str = '```\n' + traceback.format_exc() + '```' - chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk.decode()[4:])}") + chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk_decoded[4:])}") yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg) # 刷新界面 return diff --git a/version b/version index 0c32f66..bb462e2 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.1, "show_feature": true, - "new_feature": "添加支持清华ChatGLM和GPT-4 <-> 添加支持API2D(国内不需要代理) <-> 改进架构,支持与多个LLM模型同时对话 <-> 支持多API-KEY负载均衡(并列填写,逗号分割) <-> 添加输入区文本清除按键" + "new_feature": "添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)<-> 支持多API-KEY负载均衡(并列填写,逗号分割) <-> 添加输入区文本清除按键" } From 2125ea437fc06889b565c64cc59e3576a524fb6c Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 13:14:11 +0800 Subject: [PATCH 32/50] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 118 +++++++++++++++++++++------------------- docs/Dockerfile+ChatGLM | 15 +++-- 2 files changed, 71 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 4d2f79d..a97026e 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ If you like this project, please give it a Star. If you've come up with more use 一键代码解释 | 可以正确显示代码、解释代码 [自定义快捷键](https://www.bilibili.com/video/BV14s4y1E7jN) | 支持自定义快捷键 [配置代理服务器](https://www.bilibili.com/video/BV1rc411W7Dr) | 支持配置代理服务器 -模块化设计 | 支持自定义高阶的实验性功能与[函数插件],插件支持[热更新](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97) +模块化设计 | 支持自定义高阶的函数插件与[函数插件],插件支持[热更新](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97) [自我程序剖析](https://www.bilibili.com/video/BV1cj411A7VW) | [函数插件] [一键读懂](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)本项目的源代码 [程序剖析](https://www.bilibili.com/video/BV1cj411A7VW) | [函数插件] 一键可以剖析其他Python/C/C++/Java/Lua/...项目树 读论文 | [函数插件] 一键解读latex论文全文并生成摘要 @@ -78,16 +78,17 @@ huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/ v3.1的[huggingface测试版](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta)(huggingface版不支持chatglm) +--- -## 直接运行 (Windows, Linux or MacOS) +## 安装-方法1:直接运行 (Windows, Linux or MacOS) -### 1. 下载项目 +1. 下载项目 ```sh git clone https://github.com/binary-husky/chatgpt_academic.git cd chatgpt_academic ``` -### 2. 配置API_KEY和代理设置 +2. 配置API_KEY和代理设置 在`config.py`中,配置 海外Proxy 和 OpenAI API KEY,说明如下 ``` @@ -98,7 +99,7 @@ cd chatgpt_academic (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。) -### 3. 安装依赖 +3. 安装依赖 ```sh # (选择一)推荐 python -m pip install -r requirements.txt @@ -112,27 +113,30 @@ python -m pip install -r requirements.txt # python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ``` -### 4. 运行 +如果需要支持清华ChatGLM,需要额外安装更多依赖(不熟悉python者、电脑配置不佳者,建议不要尝试): +```sh +python -m pip install -r request_llm/requirements_chatglm.txt +``` + +4. 运行 ```sh python main.py ``` -### 5. 测试实验性功能 +5. 测试函数插件 ``` -- 测试C++项目头文件分析 - input区域 输入 `./crazy_functions/test_project/cpp/libJPG` , 然后点击 "[实验] 解析整个C++项目(input输入项目根路径)" -- 测试给Latex项目写摘要 - input区域 输入 `./crazy_functions/test_project/latex/attention` , 然后点击 "[实验] 读tex论文写摘要(input输入项目根路径)" - 测试Python项目分析 - input区域 输入 `./crazy_functions/test_project/python/dqn` , 然后点击 "[实验] 解析整个py项目(input输入项目根路径)" + input区域 输入 `./crazy_functions/test_project/python/dqn` , 然后点击 "解析整个Python项目" - 测试自我代码解读 - 点击 "[实验] 请解析并解构此项目本身" + 点击 "[多线程Demo] 解析此项目本身(源码自译解)" - 测试实验功能模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能 - 点击 "[实验] 实验功能函数模板" + 点击 "[函数插件模板Demo] 历史上的今天" +- 函数插件区下拉菜单中有更多功能可供选择 ``` -## 使用docker (Linux) +## 安装-方法2:使用docker (Linux) +1. 仅ChatGPT(推荐大多数人选择) ``` sh # 下载项目 git clone https://github.com/binary-husky/chatgpt_academic.git @@ -144,67 +148,70 @@ docker build -t gpt-academic . # 运行 docker run --rm -it --net=host gpt-academic -# 测试实验性功能 -## 测试自我代码解读 -点击 "[实验] 请解析并解构此项目本身" -## 测试实验功能模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能 -点击 "[实验] 实验功能函数模板" -##(请注意在docker中运行时,需要额外注意程序的文件访问权限问题) -## 测试C++项目头文件分析 -input区域 输入 ./crazy_functions/test_project/cpp/libJPG , 然后点击 "[实验] 解析整个C++项目(input输入项目根路径)" +# 测试函数插件 +## 测试函数插件模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能 +点击 "[函数插件模板Demo] 历史上的今天" ## 测试给Latex项目写摘要 -input区域 输入 ./crazy_functions/test_project/latex/attention , 然后点击 "[实验] 读tex论文写摘要(input输入项目根路径)" +input区域 输入 ./crazy_functions/test_project/latex/attention , 然后点击 "读Tex论文写摘要" ## 测试Python项目分析 -input区域 输入 ./crazy_functions/test_project/python/dqn , 然后点击 "[实验] 解析整个py项目(input输入项目根路径)" +input区域 输入 ./crazy_functions/test_project/python/dqn , 然后点击 "解析整个Python项目" +函数插件区下拉菜单中有更多功能可供选择 ``` -## 其他部署方式 +2. ChatGPT+ChatGLM(需要对docker非常熟悉 + 电脑配置足够强) -- 远程云服务器部署 -请访问[部署wiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) +``` sh +# 修改dockerfile +cd docs && nano Dockerfile+ChatGLM +# How to build | 如何构建 (Dockerfile+ChatGLM在docs路径下,请先cd docs) +docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . +# How to run | 如何运行 (1) 直接运行: +docker run --rm -it --net=host --gpus=all gpt-academic +# How to run | 如何运行 (2) 我想运行之前进容器做一些调整: +docker run --rm -it --net=host --gpus=all gpt-academic bash +``` -- 使用WSL2(Windows Subsystem for Linux 子系统) -请访问[部署wiki-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) + +## 安装-方法3:其他部署方式 + +1. 远程云服务器部署 +请访问[部署wiki-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) + +2. 使用WSL2(Windows Subsystem for Linux 子系统) +请访问[部署wiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) + + +## 安装-代理配置 +### 方法一:常规方法 +[配置代理](https://github.com/binary-husky/chatgpt_academic/issues/1) + +### 方法二:纯新手教程 +[纯新手教程](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) + + +--- ## 自定义新的便捷按钮(学术快捷键自定义) 任意文本编辑器打开`core_functional.py`,添加条目如下,然后重启程序即可。(如果按钮已经添加成功并可见,那么前缀、后缀都支持热修改,无需重启程序即可生效。) 例如 ``` "超级英译中": { - # 前缀,会被加在你的输入之前。例如,用来描述你的要求,例如翻译、解释代码、润色等等 "Prefix": "请翻译把下面一段内容成中文,然后用一个markdown表格逐一解释文中出现的专有名词:\n\n", # 后缀,会被加在你的输入之后。例如,配合前缀可以把你的输入内容用引号圈起来。 "Suffix": "", - }, ```
+--- -如果你发明了更好用的快捷键,欢迎发issue或者pull requests! -## 配置代理 -### 方法一:常规方法 -在```config.py```中修改端口与代理软件对应 - -
- - -
- -配置完成后,你可以用以下命令测试代理是否工作,如果一切正常,下面的代码将输出你的代理服务器所在地: -``` -python check_proxy.py -``` -### 方法二:纯新手教程 -[纯新手教程](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) - -## 功能测试 +## 部分功能展示 ### 图片显示: @@ -258,8 +265,9 @@ python check_proxy.py ## Todo 与 版本规划: - -- version 3.0 (Todo): 优化对chatglm和其他小型llm的支持 +- version 3.2+ (todo): 函数插件支持更多参数接口 +- version 3.1: 支持同时问询多个gpt模型!支持api2d,支持多个apikey负载均衡 +- version 3.0: 对chatglm和其他小型llm的支持 - version 2.6: 重构了插件结构,提高了交互性,加入更多插件 - version 2.5: 自更新,解决总结大工程源代码时文本过长、token溢出的问题 - version 2.4: (1)新增PDF全文翻译功能; (2)新增输入区切换位置的功能; (3)新增垂直布局选项; (4)多线程函数插件优化。 @@ -271,14 +279,12 @@ python check_proxy.py ## 参考与学习 - ``` 代码中参考了很多其他优秀项目中的设计,主要包括: -# 借鉴项目1:借鉴了ChuanhuChatGPT中读取OpenAI json的方法、记录历史问询记录的方法以及gradio queue的使用技巧 +# 借鉴项目1:借鉴了ChuanhuChatGPT中诸多技巧 https://github.com/GaiZhenbiao/ChuanhuChatGPT -# 借鉴项目2: +# 借鉴项目2:清华ChatGLM-6B: https://github.com/THUDM/ChatGLM-6B - ``` diff --git a/docs/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM index 4a11f06..8e60524 100644 --- a/docs/Dockerfile+ChatGLM +++ b/docs/Dockerfile+ChatGLM @@ -11,11 +11,11 @@ RUN apt-get install -y git python python3 python-dev python3-dev --fix-missing # 配置代理网络(构建Docker镜像时使用) # # comment out below if you do not need proxy network | 如果不需要翻墙 - 从此行向下删除 -RUN $useProxyNetwork curl cip.cc -RUN sed -i '$ d' /etc/proxychains.conf -RUN sed -i '$ d' /etc/proxychains.conf -RUN echo "socks5 127.0.0.1 10880" >> /etc/proxychains.conf -ARG useProxyNetwork=proxychains +# RUN $useProxyNetwork curl cip.cc +# RUN sed -i '$ d' /etc/proxychains.conf +# RUN sed -i '$ d' /etc/proxychains.conf +# RUN echo "socks5 127.0.0.1 10880" >> /etc/proxychains.conf +# ARG useProxyNetwork=proxychains # # comment out above if you do not need proxy network | 如果不需要翻墙 - 从此行向上删除 @@ -42,11 +42,14 @@ ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache RUN $useProxyNetwork git pull # 为chatgpt-academic配置代理和API-KEY (非必要 可选步骤) +# 可同时填写多个API-KEY,支持openai的key和api2d的key共存,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey1,fkxxxx-api2dkey2" +# LLM_MODEL 是选择初始的模型 +# LOCAL_MODEL_DEVICE 是选择chatglm等本地模型运行的设备,可选 cpu 和 cuda RUN echo ' \n\ API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \n\ USE_PROXY = True \n\ LLM_MODEL = "chatglm" \n\ -LOCAL_MODEL_DEVICE = "cuda" \n\ +LOCAL_MODEL_DEVICE = "cpu" \n\ proxies = { "http": "socks5h://localhost:10880", "https": "socks5h://localhost:10880", } ' >> config_private.py # 启动 From 28aa6d1dc0826608d9349bdd5b1d35dd5cbfd1de Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 13:17:56 +0800 Subject: [PATCH 33/50] =?UTF-8?q?=E6=9B=B4=E6=96=B0dockerfile=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E4=B9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index 757a188..ed3526c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,6 @@ +# 此Dockerfile适用于“无本地模型”的环境构建,如果需要使用chatglm等本地模型,请参考 docs/Dockerfile+ChatGLM +# 如何构建 docker build -t gpt-academic . +# 如何运行 docker run --rm -it --net=host gpt-academic FROM python:3.11 RUN echo '[global]' > /etc/pip.conf && \ From 73d39b5470e2c7a36418642394bed2028113da2d Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 13:23:49 +0800 Subject: [PATCH 34/50] =?UTF-8?q?=E4=BF=AE=E6=AD=A3dockerfile=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index ed3526c..5b6f4b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # 此Dockerfile适用于“无本地模型”的环境构建,如果需要使用chatglm等本地模型,请参考 docs/Dockerfile+ChatGLM -# 如何构建 docker build -t gpt-academic . -# 如何运行 docker run --rm -it --net=host gpt-academic +# 如何构建: 先修改 `config.py`, 然后 docker build -t gpt-academic . +# 如何运行: docker run --rm -it --net=host gpt-academic FROM python:3.11 RUN echo '[global]' > /etc/pip.conf && \ From b0409b929b2814decd78e7d74650956d848ff546 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 14:27:34 +0800 Subject: [PATCH 35/50] =?UTF-8?q?tiktoken=E5=81=9Alazyload=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- check_proxy.py | 9 ++++- crazy_functions/Latex全文润色.py | 5 ++- crazy_functions/Latex全文翻译.py | 5 ++- crazy_functions/crazy_utils.py | 4 +-- crazy_functions/代码重写为全英文_多线程.py | 5 ++- crazy_functions/批量Markdown翻译.py | 5 ++- crazy_functions/批量翻译PDF文档_多线程.py | 4 +-- crazy_functions/理解PDF文档内容.py | 4 +-- main.py | 3 +- request_llm/bridge_all.py | 38 ++++++++++++++++------ request_llm/bridge_chatglm.py | 35 +++++++++++++++++--- toolbox.py | 1 - 12 files changed, 83 insertions(+), 35 deletions(-) diff --git a/check_proxy.py b/check_proxy.py index 7fdd2b0..28711a8 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -103,7 +103,7 @@ def auto_update(): import json proxies, = get_conf('proxies') response = requests.get( - "https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=1) + "https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=5) remote_json_data = json.loads(response.text) remote_version = remote_json_data['version'] if remote_json_data["show_feature"]: @@ -133,6 +133,13 @@ def auto_update(): except: print('自动更新程序:已禁用') +def warm_up_modules(): + print('正在执行一些模块的预热...') + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] + enc.encode("模块预热", disallowed_special=()) + enc = model_info["gpt-4"]['tokenizer'] + enc.encode("模块预热", disallowed_special=()) if __name__ == '__main__': import os diff --git a/crazy_functions/Latex全文润色.py b/crazy_functions/Latex全文润色.py index da03686..c299e59 100644 --- a/crazy_functions/Latex全文润色.py +++ b/crazy_functions/Latex全文润色.py @@ -11,9 +11,8 @@ class PaperFileGroup(): self.sp_file_tag = [] # count_token - import tiktoken - from toolbox import get_conf - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) self.get_token_num = get_token_num diff --git a/crazy_functions/Latex全文翻译.py b/crazy_functions/Latex全文翻译.py index de25e62..efada61 100644 --- a/crazy_functions/Latex全文翻译.py +++ b/crazy_functions/Latex全文翻译.py @@ -11,9 +11,8 @@ class PaperFileGroup(): self.sp_file_tag = [] # count_token - import tiktoken - from toolbox import get_conf - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) self.get_token_num = get_token_num diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 6085049..4e0eba4 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -2,9 +2,9 @@ import traceback from toolbox import update_ui, get_conf def input_clipping(inputs, history, max_token_limit): - import tiktoken import numpy as np - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) mode = 'input-and-history' diff --git a/crazy_functions/代码重写为全英文_多线程.py b/crazy_functions/代码重写为全英文_多线程.py index f8ecec9..e57f80f 100644 --- a/crazy_functions/代码重写为全英文_多线程.py +++ b/crazy_functions/代码重写为全英文_多线程.py @@ -59,9 +59,8 @@ def 全项目切换英文(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_ # 第5步:Token限制下的截断与处理 MAX_TOKEN = 3000 - import tiktoken - from toolbox import get_conf - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_fn(txt): return len(enc.encode(txt, disallowed_special=())) diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index 823ca3f..68d1b50 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -11,9 +11,8 @@ class PaperFileGroup(): self.sp_file_tag = [] # count_token - import tiktoken - from toolbox import get_conf - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) self.get_token_num = get_token_num diff --git a/crazy_functions/批量翻译PDF文档_多线程.py b/crazy_functions/批量翻译PDF文档_多线程.py index 351be0e..4adb9a4 100644 --- a/crazy_functions/批量翻译PDF文档_多线程.py +++ b/crazy_functions/批量翻译PDF文档_多线程.py @@ -68,8 +68,8 @@ def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, # 递归地切割PDF文件 from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf - from toolbox import get_conf - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf( txt=file_content, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT) diff --git a/crazy_functions/理解PDF文档内容.py b/crazy_functions/理解PDF文档内容.py index 05fbd49..5050864 100644 --- a/crazy_functions/理解PDF文档内容.py +++ b/crazy_functions/理解PDF文档内容.py @@ -17,8 +17,8 @@ def 解析PDF(file_name, llm_kwargs, plugin_kwargs, chatbot, history, system_pro TOKEN_LIMIT_PER_FRAGMENT = 2500 from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf - from toolbox import get_conf - enc = tiktoken.encoding_for_model("gpt-3.5-turbo") + from request_llm.bridge_all import model_info + enc = model_info["gpt-3.5-turbo"]['tokenizer'] def get_token_num(txt): return len(enc.encode(txt, disallowed_special=())) paper_fragments = breakdown_txt_to_satisfy_token_limit_for_pdf( txt=file_content, get_token_fn=get_token_num, limit=TOKEN_LIMIT_PER_FRAGMENT) diff --git a/main.py b/main.py index c028e4a..fafd38c 100644 --- a/main.py +++ b/main.py @@ -40,7 +40,7 @@ def main(): set_theme = adjust_theme() # 代理与自动更新 - from check_proxy import check_proxy, auto_update + from check_proxy import check_proxy, auto_update, warm_up_modules proxy_info = check_proxy(proxies) gr_L1 = lambda: gr.Row().style() @@ -180,6 +180,7 @@ def main(): webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") threading.Thread(target=open, name="open-browser", daemon=True).start() threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() + threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start() auto_opentab_delay() demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png") diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index ecc6b1e..f416ad6 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -9,7 +9,7 @@ 2. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程 """ import tiktoken - +from functools import wraps, lru_cache from concurrent.futures import ThreadPoolExecutor from .bridge_chatgpt import predict_no_ui_long_connection as chatgpt_noui @@ -18,13 +18,31 @@ from .bridge_chatgpt import predict as chatgpt_ui from .bridge_chatglm import predict_no_ui_long_connection as chatglm_noui from .bridge_chatglm import predict as chatglm_ui -from .bridge_tgui import predict_no_ui_long_connection as tgui_noui -from .bridge_tgui import predict as tgui_ui +# from .bridge_tgui import predict_no_ui_long_connection as tgui_noui +# from .bridge_tgui import predict as tgui_ui colors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044'] -get_token_num_gpt35 = lambda txt: len(tiktoken.encoding_for_model("gpt-3.5-turbo").encode(txt, disallowed_special=())) -get_token_num_gpt4 = lambda txt: len(tiktoken.encoding_for_model("gpt-4").encode(txt, disallowed_special=())) +class LazyloadTiktoken(object): + def __init__(self, model): + self.model = model + + @staticmethod + @lru_cache(maxsize=128) + def get_encoder(model): + print('正在加载tokenizer,如果是第一次运行,可能需要一点时间下载参数') + tmp = tiktoken.encoding_for_model(model) + print('加载tokenizer完毕') + return tmp + + def encode(self, *args, **kwargs): + encoder = self.get_encoder(self.model) + return encoder.encode(*args, **kwargs) + +tokenizer_gpt35 = LazyloadTiktoken("gpt-3.5-turbo") +tokenizer_gpt4 = LazyloadTiktoken("gpt-4") +get_token_num_gpt35 = lambda txt: len(tokenizer_gpt35.encode(txt, disallowed_special=())) +get_token_num_gpt4 = lambda txt: len(tokenizer_gpt4.encode(txt, disallowed_special=())) model_info = { # openai @@ -33,7 +51,7 @@ model_info = { "fn_without_ui": chatgpt_noui, "endpoint": "https://api.openai.com/v1/chat/completions", "max_token": 4096, - "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), + "tokenizer": tokenizer_gpt35, "token_cnt": get_token_num_gpt35, }, @@ -42,7 +60,7 @@ model_info = { "fn_without_ui": chatgpt_noui, "endpoint": "https://api.openai.com/v1/chat/completions", "max_token": 8192, - "tokenizer": tiktoken.encoding_for_model("gpt-4"), + "tokenizer": tokenizer_gpt4, "token_cnt": get_token_num_gpt4, }, @@ -52,7 +70,7 @@ model_info = { "fn_without_ui": chatgpt_noui, "endpoint": "https://openai.api2d.net/v1/chat/completions", "max_token": 4096, - "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), + "tokenizer": tokenizer_gpt35, "token_cnt": get_token_num_gpt35, }, @@ -61,7 +79,7 @@ model_info = { "fn_without_ui": chatgpt_noui, "endpoint": "https://openai.api2d.net/v1/chat/completions", "max_token": 8192, - "tokenizer": tiktoken.encoding_for_model("gpt-4"), + "tokenizer": tokenizer_gpt4, "token_cnt": get_token_num_gpt4, }, @@ -71,7 +89,7 @@ model_info = { "fn_without_ui": chatglm_noui, "endpoint": None, "max_token": 1024, - "tokenizer": tiktoken.encoding_for_model("gpt-3.5-turbo"), + "tokenizer": tokenizer_gpt35, "token_cnt": get_token_num_gpt35, }, diff --git a/request_llm/bridge_chatglm.py b/request_llm/bridge_chatglm.py index 819519b..7af2835 100644 --- a/request_llm/bridge_chatglm.py +++ b/request_llm/bridge_chatglm.py @@ -5,6 +5,8 @@ import importlib from toolbox import update_ui, get_conf from multiprocessing import Process, Pipe +load_message = "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……" + ################################################################################# class GetGLMHandle(Process): def __init__(self): @@ -12,13 +14,26 @@ class GetGLMHandle(Process): self.parent, self.child = Pipe() self.chatglm_model = None self.chatglm_tokenizer = None + self.info = "" + self.success = True + self.check_dependency() self.start() - print('初始化') + def check_dependency(self): + try: + import sentencepiece + self.info = "依赖检测通过" + self.success = True + except: + self.info = "缺少ChatGLM的依赖,如果要使用ChatGLM,除了基础的pip依赖以外,您还需要运行`pip install -r request_llm/requirements_chatglm.txt`安装ChatGLM的依赖。" + self.success = False + def ready(self): return self.chatglm_model is not None def run(self): + # 第一次运行,加载参数 + retry = 0 while True: try: if self.chatglm_model is None: @@ -33,7 +48,12 @@ class GetGLMHandle(Process): else: break except: - pass + retry += 1 + if retry > 3: + self.child.send('[Local Message] Call ChatGLM fail 不能正常加载ChatGLM的参数。') + raise RuntimeError("不能正常加载ChatGLM的参数!") + + # 进入任务等待状态 while True: kwargs = self.child.recv() try: @@ -64,7 +84,11 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", global glm_handle if glm_handle is None: glm_handle = GetGLMHandle() - observe_window[0] = "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……" + observe_window[0] = load_message + "\n\n" + glm_handle.info + if not glm_handle.success: + error = glm_handle.info + glm_handle = None + raise RuntimeError(error) # chatglm 没有 sys_prompt 接口,因此把prompt加入 history history_feedin = [] @@ -93,8 +117,11 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp global glm_handle if glm_handle is None: glm_handle = GetGLMHandle() - chatbot[-1] = (inputs, "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……") + chatbot[-1] = (inputs, load_message + "\n\n" + glm_handle.info) yield from update_ui(chatbot=chatbot, history=[]) + if not glm_handle.success: + glm_handle = None + return if additional_fn is not None: import core_functional diff --git a/toolbox.py b/toolbox.py index 05fd368..038d7be 100644 --- a/toolbox.py +++ b/toolbox.py @@ -25,7 +25,6 @@ def ArgsGeneralWrapper(f): 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。 """ def decorated(cookies, max_length, llm_model, txt, txt2, top_p, temperature, chatbot, history, system_prompt, *args): - from request_llm.bridge_all import model_info txt_passon = txt if txt == "" and txt2 != "": txt_passon = txt2 # 引入一个有cookie的chatbot From 966af4d4c55941775a9c19927814d3e21495e4d9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 14:40:22 +0800 Subject: [PATCH 36/50] warm up module in dockerfile --- Dockerfile | 3 +++ docs/Dockerfile+ChatGLM | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Dockerfile b/Dockerfile index 5b6f4b9..da5053d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,7 @@ RUN pip3 install -r requirements.txt COPY . . +# 可选步骤,用于预热模块 +RUN python3 -c 'from check_proxy import warm_up_modules; warm_up_modules()' + CMD ["python3", "-u", "main.py"] diff --git a/docs/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM index 8e60524..52f5f33 100644 --- a/docs/Dockerfile+ChatGLM +++ b/docs/Dockerfile+ChatGLM @@ -41,6 +41,9 @@ RUN python3 -u warm_up_chatglm.py ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache RUN $useProxyNetwork git pull +# 预热Tiktoken模块 +RUN python3 -c 'from check_proxy import warm_up_modules; warm_up_modules()' + # 为chatgpt-academic配置代理和API-KEY (非必要 可选步骤) # 可同时填写多个API-KEY,支持openai的key和api2d的key共存,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey1,fkxxxx-api2dkey2" # LLM_MODEL 是选择初始的模型 From 6bf89dfa2d9431fe233cfcd4f1c4c325f8db6f17 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 14:49:34 +0800 Subject: [PATCH 37/50] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- crazy_functions/crazy_functions_test.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index 1cc057d..8c01531 100644 --- a/config.py +++ b/config.py @@ -44,7 +44,7 @@ WEB_PORT = -1 MAX_RETRY = 2 # OpenAI模型选择是(gpt4现在只对申请成功的人开放) -LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm" +LLM_MODEL = "gpt-3.5-turbo" # 可选 ↓↓↓ AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm"] # 本地LLM模型如ChatGLM的执行方式 CPU/GPU diff --git a/crazy_functions/crazy_functions_test.py b/crazy_functions/crazy_functions_test.py index 1c50724..2838e54 100644 --- a/crazy_functions/crazy_functions_test.py +++ b/crazy_functions/crazy_functions_test.py @@ -37,6 +37,12 @@ def test_解析一个Python项目(): for cookies, cb, hist, msg in 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): print(cb) +def test_解析一个Cpp项目(): + from crazy_functions.解析项目源代码 import 解析一个C项目 + txt = "crazy_functions/test_project/cpp/cppipc" + for cookies, cb, hist, msg in 解析一个C项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + print(cb) + def test_Latex英文润色(): from crazy_functions.Latex全文润色 import Latex英文润色 txt = "crazy_functions/test_project/latex/attention" @@ -80,6 +86,7 @@ test_批量翻译PDF文档() test_谷歌检索小助手() test_总结word文档() test_下载arxiv论文并翻译摘要() +test_解析一个Cpp项目() input("程序完成,回车退出。") print("退出。") \ No newline at end of file From a15489d6e6fee7d2418095296f6efed19bb8de74 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 14:54:58 +0800 Subject: [PATCH 38/50] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatgpt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index 4990c1a..f10fc20 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -85,6 +85,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", raise ConnectionAbortedError("OpenAI拒绝了请求:" + error_msg) else: raise RuntimeError("OpenAI拒绝了请求:" + error_msg) + if ('data: [DONE]' in chunk): break # api2d 正常完成 json_data = json.loads(chunk.lstrip('data:'))['choices'][0] delta = json_data["delta"] if len(delta) == 0: break From c37c49dd5141fdc37fe5222d4d0252a706daf9dd Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 14:58:39 +0800 Subject: [PATCH 39/50] =?UTF-8?q?lazy=20load=20+decode=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_all.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index f416ad6..f1f4ee1 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -38,7 +38,11 @@ class LazyloadTiktoken(object): def encode(self, *args, **kwargs): encoder = self.get_encoder(self.model) return encoder.encode(*args, **kwargs) - + + def decode(self, *args, **kwargs): + encoder = self.get_encoder(self.model) + return encoder.decode(*args, **kwargs) + tokenizer_gpt35 = LazyloadTiktoken("gpt-3.5-turbo") tokenizer_gpt4 = LazyloadTiktoken("gpt-4") get_token_num_gpt35 = lambda txt: len(tokenizer_gpt35.encode(txt, disallowed_special=())) From a9e9e79ed31022bfa023238c63db60c12607f5f6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 15:28:44 +0800 Subject: [PATCH 40/50] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index fafd38c..2ee64a9 100644 --- a/main.py +++ b/main.py @@ -99,7 +99,7 @@ def main(): system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt) 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=4096, value=512, step=1, interactive=True, label="MaxLength",) + max_length_sl = gr.Slider(minimum=256, maximum=4096, value=512, step=1, interactive=True, label="Local LLM MaxLength",) checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区") md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False) From 02e3e1d19baa37f0e4b3ade5633dc5b029a1c47b Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 15:36:28 +0800 Subject: [PATCH 41/50] fix dockerfile --- docs/Dockerfile+ChatGLM | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM index 52f5f33..dafcee7 100644 --- a/docs/Dockerfile+ChatGLM +++ b/docs/Dockerfile+ChatGLM @@ -1,6 +1,6 @@ # How to build | 如何构建: docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . -# How to run | 如何运行 (1) 直接运行: docker run --rm -it --net=host --gpus=all gpt-academic -# How to run | 如何运行 (2) 我想运行之前进容器做一些调整: docker run --rm -it --net=host --gpus=all gpt-academic bash +# How to run | 如何运行 (1) 直接运行(选择0号GPU): docker run --rm -it --net=host --gpus="0" gpt-academic +# How to run | 如何运行 (2) 我想运行之前进容器做一些调整: docker run --rm -it --net=host --gpus="0" gpt-academic bash # 从NVIDIA源,从而支持显卡运损(检查宿主的nvidia-smi中的cuda版本必须>=11.3) FROM nvidia/cuda:11.3.1-runtime-ubuntu20.04 @@ -11,11 +11,11 @@ RUN apt-get install -y git python python3 python-dev python3-dev --fix-missing # 配置代理网络(构建Docker镜像时使用) # # comment out below if you do not need proxy network | 如果不需要翻墙 - 从此行向下删除 -# RUN $useProxyNetwork curl cip.cc -# RUN sed -i '$ d' /etc/proxychains.conf -# RUN sed -i '$ d' /etc/proxychains.conf -# RUN echo "socks5 127.0.0.1 10880" >> /etc/proxychains.conf -# ARG useProxyNetwork=proxychains +RUN $useProxyNetwork curl cip.cc +RUN sed -i '$ d' /etc/proxychains.conf +RUN sed -i '$ d' /etc/proxychains.conf +RUN echo "socks5 127.0.0.1 10880" >> /etc/proxychains.conf +ARG useProxyNetwork=proxychains # # comment out above if you do not need proxy network | 如果不需要翻墙 - 从此行向上删除 @@ -24,7 +24,7 @@ RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.8 # 下载分支 WORKDIR /gpt -RUN $useProxyNetwork git clone https://github.com/binary-husky/chatgpt_academic.git -b v3.1 +RUN $useProxyNetwork git clone https://github.com/binary-husky/chatgpt_academic.git WORKDIR /gpt/chatgpt_academic RUN $useProxyNetwork python3 -m pip install -r requirements.txt RUN $useProxyNetwork python3 -m pip install -r request_llm/requirements_chatglm.txt @@ -45,14 +45,14 @@ RUN $useProxyNetwork git pull RUN python3 -c 'from check_proxy import warm_up_modules; warm_up_modules()' # 为chatgpt-academic配置代理和API-KEY (非必要 可选步骤) -# 可同时填写多个API-KEY,支持openai的key和api2d的key共存,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey1,fkxxxx-api2dkey2" +# 可同时填写多个API-KEY,支持openai的key和api2d的key共存,用英文逗号分割,例如API_KEY = "sk-openaikey1,fkxxxx-api2dkey2,........" # LLM_MODEL 是选择初始的模型 # LOCAL_MODEL_DEVICE 是选择chatglm等本地模型运行的设备,可选 cpu 和 cuda RUN echo ' \n\ -API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \n\ +API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,fkxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \n\ USE_PROXY = True \n\ LLM_MODEL = "chatglm" \n\ -LOCAL_MODEL_DEVICE = "cpu" \n\ +LOCAL_MODEL_DEVICE = "cuda" \n\ proxies = { "http": "socks5h://localhost:10880", "https": "socks5h://localhost:10880", } ' >> config_private.py # 启动 From f956dcd91dfd86ef6686e9ec33a7600089ad7264 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 15:39:57 +0800 Subject: [PATCH 42/50] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 8c01531..e24c68b 100644 --- a/config.py +++ b/config.py @@ -43,7 +43,7 @@ WEB_PORT = -1 # 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制 MAX_RETRY = 2 -# OpenAI模型选择是(gpt4现在只对申请成功的人开放) +# OpenAI模型选择是(gpt4现在只对申请成功的人开放,体验gpt-4可以试试api2d) LLM_MODEL = "gpt-3.5-turbo" # 可选 ↓↓↓ AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm"] From 9d2bc1f3e0102fa2445f1de222362b2cf150aed9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 15:52:44 +0800 Subject: [PATCH 43/50] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatgpt.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index f10fc20..8c915c2 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -195,10 +195,13 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp traceback.print_exc() yield from update_ui(chatbot=chatbot, history=history, msg="Json解析不合常规") # 刷新界面 chunk = get_full_error(chunk, stream_response) + chunk_decoded = chunk.decode() error_msg = chunk_decoded if "reduce the length" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长,或历史数据过长. 历史缓存数据现已释放,您可以请再次尝试.") history = [] # 清除历史 + elif "does not exist" in error_msg: + chatbot[-1] = (chatbot[-1][0], f"[Local Message] Model {llm_kwargs['llm_model']} does not exist. 模型不存在,或者您没有获得体验资格.") elif "Incorrect API key" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key. OpenAI以提供了不正确的API_KEY为由,拒绝服务.") elif "exceeded your current quota" in error_msg: From fea5f018744aa6c4020c63e68acee382e7162dbe Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 15:56:20 +0800 Subject: [PATCH 44/50] Update README --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a97026e..bfd2b34 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,7 @@ chat分析报告生成 | [函数插件] 运行后自动生成总结汇报 公式/图片/表格显示 | 可以同时显示公式的tex形式和渲染形式,支持公式、代码高亮 多线程函数插件支持 | 支持多线调用chatgpt,一键处理海量文本或程序 启动暗色gradio[主题](https://github.com/binary-husky/chatgpt_academic/issues/173) | 在浏览器url后面添加```/?__dark-theme=true```可以切换dark主题 -[多LLM模型](https://www.bilibili.com/video/BV1EM411K7VH/)支持([v3.1分支](https://github.com/binary-husky/chatgpt_academic/tree/v3.1)) | 同时被ChatGPT和[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)伺候的感觉一定会很不错吧? -兼容[TGUI](https://github.com/oobabooga/text-generation-webui)接入更多样的语言模型 | 接入opt-1.3b, galactica-1.3b等模型([v3.1分支](https://github.com/binary-husky/chatgpt_academic/tree/v3.0)测试中) +[多LLM模型](https://www.bilibili.com/video/BV1EM411K7VH/)支持 | 同时被ChatGPT和[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)伺候的感觉一定会很不错吧? huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/gpt-academic) | 登陆huggingface后复制[此空间](https://huggingface.co/spaces/qingxu98/gpt-academic) …… | …… @@ -70,12 +69,12 @@ huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/ -- 多种大语言模型混合调用(ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4, [v3.1分支](https://github.com/binary-husky/chatgpt_academic/tree/v3.1)测试中) +- 多种大语言模型混合调用(ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4)
-v3.1的[huggingface测试版](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta)(huggingface版不支持chatglm) +多种大语言模型混合调用[huggingface测试版](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta)(huggingface版不支持chatglm) --- From 85e433910adc81476d67351648d8c278cb33f0a5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 15:58:33 +0800 Subject: [PATCH 45/50] UP --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bfd2b34..1cca472 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ chat分析报告生成 | [函数插件] 运行后自动生成总结汇报 公式/图片/表格显示 | 可以同时显示公式的tex形式和渲染形式,支持公式、代码高亮 多线程函数插件支持 | 支持多线调用chatgpt,一键处理海量文本或程序 启动暗色gradio[主题](https://github.com/binary-husky/chatgpt_academic/issues/173) | 在浏览器url后面添加```/?__dark-theme=true```可以切换dark主题 -[多LLM模型](https://www.bilibili.com/video/BV1EM411K7VH/)支持 | 同时被ChatGPT和[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)伺候的感觉一定会很不错吧? +[多LLM模型](https://www.bilibili.com/video/BV1wT411p7yf)支持,[API2D](https://api2d.com/)接口支持 | 同时被GPT3.5、GPT4和[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)伺候的感觉一定会很不错吧? huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/gpt-academic) | 登陆huggingface后复制[此空间](https://huggingface.co/spaces/qingxu98/gpt-academic) …… | …… From 0aed1609f6fa300402c8f435223765410d358698 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Wed, 19 Apr 2023 16:00:42 +0800 Subject: [PATCH 46/50] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1cca472..75ee585 100644 --- a/README.md +++ b/README.md @@ -182,10 +182,10 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash ## 安装-代理配置 -### 方法一:常规方法 +1. 常规方法 [配置代理](https://github.com/binary-husky/chatgpt_academic/issues/1) -### 方法二:纯新手教程 +2. 纯新手教程 [纯新手教程](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) From 5d23420553a10fd52fd497e24bdf3729b552c38e Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 16:49:47 +0800 Subject: [PATCH 47/50] =?UTF-8?q?=E5=A4=9A=E8=AF=AD=E8=A8=80README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- crazy_functions/批量Markdown翻译.py | 2 +- docs/README_EN.md | 303 ++++++++++++++-------------- docs/README_FR.md | 296 +++++++++++++++++++++++++++ docs/README_JP.md | 294 +++++++++++++++++++++++++++ docs/README_RS.md | 291 ++++++++++++++++++++++++++ 6 files changed, 1033 insertions(+), 155 deletions(-) create mode 100644 docs/README_FR.md create mode 100644 docs/README_JP.md create mode 100644 docs/README_RS.md diff --git a/README.md b/README.md index 1cca472..423f97a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发issue或者pull requests** -If you like this project, please give it a Star. If you've come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request. We also have a [README in English](img/README_EN.md) translated by this project itself. +If you like this project, please give it a Star. If you've come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request. We also have a README in [English|](docs/README_EN.md)[Japanese|](docs/README_JP.md)[Russian|](docs/README_RS.md)[French](docs/README_FR.md) translated by this project itself. > **Note** > diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index 68d1b50..ee6a1a4 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -50,7 +50,7 @@ def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch pfg.file_contents.append(file_content) # <-------- 拆分过长的Markdown文件 ----------> - pfg.run_file_split(max_token_limit=2048) + pfg.run_file_split(max_token_limit=1500) n_split = len(pfg.sp_file_contents) # <-------- 多线程润色开始 ----------> diff --git a/docs/README_EN.md b/docs/README_EN.md index 537b430..db214f5 100644 --- a/docs/README_EN.md +++ b/docs/README_EN.md @@ -1,223 +1,227 @@ -# ChatGPT Academic Optimization > **Note** > -> This English readme is automatically generated by the markdown translation plugin in this project, and may not be 100% correct. +> This English README is automatically generated by the markdown translation plugin in this project, and may not be 100% correct. > +# ChatGPT Academic Optimization -**If you like this project, please give it a star. If you have come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request (to the `dev` branch).** +**If you like this project, please give it a Star. If you've come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request. We also have a [README in English](docs/README_EN.md) translated by this project itself.** > **Note** > -> 1. Please note that only function plugins (buttons) marked in **red** support reading files, and some plugins are located in the **dropdown menu** in the plugin area. Additionally, we welcome and process PRs for any new plugins with the **highest priority**! +> 1. Please note that only **functions with red color** supports reading files, some functions are located in the **dropdown menu** of plugins. Additionally, we welcome and prioritize any new plugin PRs with **highest priority**! > -> 2. The functions of each file in this project are detailed in the self-translation report [self_analysis.md](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A). With the version iteration, you can click on a relevant function plugin at any time to call GPT to regenerate the self-analysis report for the project. Commonly asked questions are summarized in the [`wiki`](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98). -> -> 3. If you are not used to the function, comments or interface with some Chinese names, you can click on the relevant function plugin at any time to call ChatGPT to generate the source code of the project in English. +> 2. The functionality of each file in this project is detailed in the self-translation report [`self_analysis.md`](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A) of the project. With the iteration of the version, you can also click on the relevant function plugins at any time to call GPT to regenerate the self-analysis report of the project. The FAQ summary is in the [`wiki`](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) section. +> +
Function | Description --- | --- -One-click refinement | Supports one-click refinement, one-click searching for grammatical errors in papers. -One-click translation between Chinese and English | One-click translation between Chinese and English. -One-click code interpretation | Can correctly display and interpret the code. -[Custom shortcuts](https://www.bilibili.com/video/BV14s4y1E7jN) | Supports custom shortcuts. -[Configure proxy server](https://www.bilibili.com/video/BV1rc411W7Dr) | Supports configuring proxy server. -Modular design | Supports custom high-order experimental features and [function plug-ins], and plug-ins support [hot update](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97). -[Self-program analysis](https://www.bilibili.com/video/BV1cj411A7VW) | [Function Plug-in] [One-Key Understanding](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A) the source code of this project. -[Program analysis](https://www.bilibili.com/video/BV1cj411A7VW) | [Function Plug-in] One-click can analyze other Python/C/C++/Java/Golang/Lua/Rect project trees. -Read papers | [Function Plug-in] One-click reads the full text of a latex paper and generates an abstract. -Latex full-text translation/refinement | [Function Plug-in] One-click translates or refines a latex paper. -Batch annotation generation | [Function Plug-in] One-click generates function annotations in batches. -Chat analysis report generation | [Function Plug-in] Automatically generate summary reports after running. -[Arxiv assistant](https://www.bilibili.com/video/BV1LM4y1279X) | [Function Plug-in] Enter the arxiv paper url and you can translate the abstract and download the PDF with one click. -[PDF paper full-text translation function](https://www.bilibili.com/video/BV1KT411x7Wn) | [Function Plug-in] Extract title and abstract of PDF papers + translate full text (multi-threaded). -[Google Scholar integration assistant](https://www.bilibili.com/video/BV19L411U7ia) (Version>=2.45) | [Function Plug-in] Given any Google Scholar search page URL, let GPT help you choose interesting articles. -Formula display | Can simultaneously display the tex form and rendering form of formulas. -Image display | Can display images in Markdown. -Multithreaded function plug-in support | Supports multi-threaded calling of chatgpt, one-click processing of massive texts or programs. -Support for markdown tables output by GPT | Can output markdown tables that support GPT. -Start dark gradio theme [theme](https://github.com/binary-husky/chatgpt_academic/issues/173) | Add ```/?__dark-theme=true``` to the browser URL to switch to the dark theme. -Huggingface free scientific online experience](https://huggingface.co/spaces/qingxu98/gpt-academic) | After logging in to Huggingface, copy [this space](https://huggingface.co/spaces/qingxu98/gpt-academic). -[Mixed support for multiple LLM models](https://www.bilibili.com/video/BV1EM411K7VH/) ([v3.0 branch](https://github.com/binary-husky/chatgpt_academic/tree/v3.0) in testing) | It must feel great to be served by both ChatGPT and [Tsinghua ChatGLM](https://github.com/THUDM/ChatGLM-6B)! -Compatible with [TGUI](https://github.com/oobabooga/text-generation-webui) to access more language models | Access to opt-1.3b, galactica-1.3b and other models ([v3.0 branch](https://github.com/binary-husky/chatgpt_academic/tree/v3.0) under testing). -… | ... +One-Click Polish | Supports one-click polishing and finding grammar errors in academic papers. +One-Key Translation Between Chinese and English | One-click translation between Chinese and English. +One-Key Code Interpretation | Can correctly display and interpret code. +[Custom Shortcut Keys](https://www.bilibili.com/video/BV14s4y1E7jN) | Supports custom shortcut keys. +[Configure Proxy Server](https://www.bilibili.com/video/BV1rc411W7Dr) | Supports configuring proxy servers. +Modular Design | Supports custom high-order function plugins and [function plugins], and plugins support [hot updates](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97). +[Self-programming Analysis](https://www.bilibili.com/video/BV1cj411A7VW) | [Function Plugin] [One-Key Read] (https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A) The source code of this project is analyzed. +[Program Analysis](https://www.bilibili.com/video/BV1cj411A7VW) | [Function Plugin] One-click can analyze the project tree of other Python/C/C++/Java/Lua/... projects +Read the Paper | [Function Plugin] One-click interpretation of the full text of latex paper and generation of abstracts +Latex Full Text Translation, Proofreading | [Function Plugin] One-click translation or proofreading of latex papers. +Batch Comment Generation | [Function Plugin] One-click batch generation of function comments +Chat Analysis Report Generation | [Function Plugin] After running, an automatic summary report will be generated +[Arxiv Assistant](https://www.bilibili.com/video/BV1LM4y1279X) | [Function Plugin] Enter the arxiv article url to translate the abstract and download the PDF with one click +[Full-text Translation Function of PDF Paper](https://www.bilibili.com/video/BV1KT411x7Wn) | [Function Plugin] Extract the title & abstract of the PDF paper + translate the full text (multithreading) +[Google Scholar Integration Assistant](https://www.bilibili.com/video/BV19L411U7ia) | [Function Plugin] Given any Google Scholar search page URL, let gpt help you choose interesting articles. +Formula / Picture / Table Display | Can display both the tex form and the rendering form of formulas at the same time, support formula and code highlighting +Multithreaded Function Plugin Support | Supports multi-threaded calling chatgpt, one-click processing of massive text or programs +Start Dark Gradio [Theme](https://github.com/binary-husky/chatgpt_academic/issues/173) | Add ```/?__dark-theme=true``` at the end of the browser url to switch to dark theme +[Multiple LLM Models](https://www.bilibili.com/video/BV1wT411p7yf) support, [API2D](https://api2d.com/) interface support | It must feel nice to be served by both GPT3.5, GPT4, and [Tsinghua ChatGLM](https://github.com/THUDM/ChatGLM-6B)! +Huggingface non-Science Net [Online Experience](https://huggingface.co/spaces/qingxu98/gpt-academic) | After logging in to huggingface, copy [this space](https://huggingface.co/spaces/qingxu98/gpt-academic) +... | ...
- -- New interface (modify the `LAYOUT` option in `config.py` to switch between "left and right layout" and "up and down layout"). + +- New interface (switch between "left-right layout" and "up-down layout" by modifying the LAYOUT option in config.py)
-- All buttons are dynamically generated by reading `functional.py`, and custom functions can be added freely, freeing up the clipboard. + +- All buttons are dynamically generated by reading functional.py and can add custom functionality at will, freeing up clipboard
-- Refinement/Correction +- Proofreading / correcting
-- Supports markdown tables output by GPT. +- If the output contains formulas, it will be displayed in both the tex form and the rendering form at the same time, which is convenient for copying and reading
-- If the output contains formulas, both the tex form and the rendering form are displayed simultaneously for easy copying and reading. -
- -
- -- Don't want to read project code? Let chatgpt boast about the whole project. +- Don't want to read the project code? Just take the whole project to chatgpt
-- Multiple large language models mixed calling. ([v3.0 branch](https://github.com/binary-husky/chatgpt_academic/tree/v3.0) in testing) +- Multiple major language model mixing calls (ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4) +
+ +
+ +Multiple major language model mixing call [huggingface beta version](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta) (the huggingface version does not support chatglm) -## Running Directly (Windows, Linux or MacOS) +--- -### 1. Download the Project +## Installation-Method 1: Run directly (Windows, Linux or MacOS) + +1. Download project ```sh git clone https://github.com/binary-husky/chatgpt_academic.git cd chatgpt_academic ``` -### 2. Configure API_KEY and Proxy Settings +2. Configure API_KEY and proxy settings -In `config.py`, configure the overseas Proxy and OpenAI API KEY, as follows: -``` -1. If you are in China, you need to set an overseas proxy to use the OpenAI API smoothly. Please read the instructions in config.py carefully (1. Modify the USE_PROXY to True; 2. Modify the proxies according to the instructions). -2. Configure OpenAI API KEY. You need to register on the OpenAI official website and obtain an API KEY. Once you get the API KEY, configure it in the config.py file. -3. Issues related to proxy network (network timeout, proxy not working) are summarized to https://github.com/binary-husky/chatgpt_academic/issues/1 -``` -(Note: When the program is running, it will first check whether there is a private configuration file named `config_private.py`, and use the configuration in it to overwrite the same name configuration in `config.py`. Therefore, if you can understand our configuration reading logic, we strongly recommend that you create a new configuration file next to `config.py` named `config_private.py` and transfer (copy) the configuration in `config.py` to `config_private.py`. `config_private.py` is not managed by Git, which can make your privacy information more secure.) -### 3. Install Dependencies +In `config.py`, configure the overseas Proxy and OpenAI API KEY as follows: +``` +1. If you are in China, you need to set up an overseas proxy to use the OpenAI API smoothly. Please read config.py carefully for setup details (1. Modify USE_PROXY to True; 2. Modify proxies according to the instructions). +2. Configure the OpenAI API KEY. You need to register and obtain an API KEY on the OpenAI website. Once you get the API KEY, you can configure it in the config.py file. +3. Issues related to proxy networks (network timeouts, proxy failures) are summarized at https://github.com/binary-husky/chatgpt_academic/issues/1 +``` +(P.S. When the program runs, it will first check whether there is a private configuration file named `config_private.py` and use the same-name configuration in `config.py` to overwrite it. Therefore, if you can understand our configuration reading logic, we strongly recommend that you create a new configuration file named `config_private.py` next to `config.py` and transfer (copy) the configuration in `config.py` to` config_private.py`. `config_private.py` is not controlled by git and can make your privacy information more secure.)) + + +3. Install dependencies ```sh -# (Option 1) Recommended +# (Option One) Recommended python -m pip install -r requirements.txt -# (Option 2) If you use anaconda, the steps are also similar: -# (Option 2.1) conda create -n gptac_venv python=3.11 -# (Option 2.2) conda activate gptac_venv -# (Option 2.3) python -m pip install -r requirements.txt +# (Option Two) If you use anaconda, the steps are similar: +# (Option Two.1) conda create -n gptac_venv python=3.11 +# (Option Two.2) conda activate gptac_venv +# (Option Two.3) python -m pip install -r requirements.txt -# Note: Use the official pip source or the Ali pip source. Other pip sources (such as some university pips) may have problems. Temporary substitution method: +# Note: Use official pip source or Ali pip source. Other pip sources (such as some university pips) may have problems, and temporary replacement methods are as follows: # python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ``` -### 4. Run +If you need to support Tsinghua ChatGLM, you need to install more dependencies (if you are not familiar with python or your computer configuration is not good, we recommend not to try): +```sh +python -m pip install -r request_llm/requirements_chatglm.txt +``` + +4. Run ```sh python main.py ``` -### 5. Test Experimental Features +5. Test function plugins ``` -- Test C++ Project Header Analysis - In the input area, enter `./crazy_functions/test_project/cpp/libJPG` , and then click "[Experiment] Parse the entire C++ project (input inputs the root path of the project)" -- Test Writing Abstracts for Latex Projects - In the input area, enter `./crazy_functions/test_project/latex/attention` , and then click "[Experiment] Read the tex paper and write an abstract (input inputs the root path of the project)" -- Test Python Project Analysis - In the input area, enter `./crazy_functions/test_project/python/dqn` , and then click "[Experiment] Parse the entire py project (input inputs the root path of the project)" -- Test Self-code Interpretation - Click "[Experiment] Please analyze and deconstruct this project itself" -- Test Experimental Function Template (asking GPT what happened in history today), you can implement more complex functions based on this template function - Click "[Experiment] Experimental function template" +- Test Python project analysis + In the input area, enter `./crazy_functions/test_project/python/dqn`, and then click "Analyze the entire Python project" +- Test self-code interpretation + Click "[Multithreading Demo] Interpretation of This Project Itself (Source Code Interpretation)" +- Test experimental function template function (requires gpt to answer what happened today in history). You can use this function as a template to implement more complex functions. + Click "[Function Plugin Template Demo] Today in History" +- There are more functions to choose from in the function plugin area drop-down menu. ``` -## Use Docker (Linux) +## Installation-Method 2: Use Docker (Linux) +1. ChatGPT only (recommended for most people) ``` sh -# Download Project +# download project git clone https://github.com/binary-husky/chatgpt_academic.git cd chatgpt_academic -# Configure Overseas Proxy and OpenAI API KEY -Configure config.py with any text editor -# Installation +# configure overseas Proxy and OpenAI API KEY +Edit config.py with any text editor +# Install docker build -t gpt-academic . # Run docker run --rm -it --net=host gpt-academic -# Test Experimental Features -## Test Self-code Interpretation -Click "[Experiment] Please analyze and deconstruct this project itself" -## Test Experimental Function Template (asking GPT what happened in history today), you can implement more complex functions based on this template function -Click "[Experiment] Experimental function template" -## (Please note that when running in docker, you need to pay extra attention to file access rights issues of the program.) -## Test C++ Project Header Analysis -In the input area, enter ./crazy_functions/test_project/cpp/libJPG , and then click "[Experiment] Parse the entire C++ project (input inputs the root path of the project)" -## Test Writing Abstracts for Latex Projects -In the input area, enter ./crazy_functions/test_project/latex/attention , and then click "[Experiment] Read the tex paper and write an abstract (input inputs the root path of the project)" +# Test function plug-in +## Test function plugin template function (requires gpt to answer what happened today in history). You can use this function as a template to implement more complex functions. +Click "[Function Plugin Template Demo] Today in History" +## Test Abstract Writing for Latex Projects +Enter ./crazy_functions/test_project/latex/attention in the input area, and then click "Read Tex Paper and Write Abstract" ## Test Python Project Analysis -In the input area, enter ./crazy_functions/test_project/python/dqn , and then click "[Experiment] Parse the entire py project (input inputs the root path of the project)" +Enter ./crazy_functions/test_project/python/dqn in the input area and click "Analyze the entire Python project." +More functions are available in the function plugin area drop-down menu. ``` -## Other Deployment Methods -- Use WSL2 (Windows Subsystem for Linux subsystem) -Please visit [Deploy Wiki-1] (https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) +2. ChatGPT+ChatGLM (requires strong familiarity with docker + strong computer configuration) -- nginx remote deployment -Please visit [Deploy Wiki-2] (https://github.com/binary-husky/chatgpt_academic/wiki/%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E7%9A%84%E6%8C%87%E5%AF%BC) - - -## Customizing New Convenient Buttons (Academic Shortcut Key Customization) -Open functional.py and add the entry as follows, and then restart the program. (If the button has been successfully added and is visible, both the prefix and suffix support hot modification and take effect without restarting the program.) - -For example, +``` sh +# Modify dockerfile +cd docs && nano Dockerfile+ChatGLM +# How to build | 如何构建 (Dockerfile+ChatGLM在docs路径下,请先cd docs) +docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . +# How to run | 如何运行 (1) 直接运行: +docker run --rm -it --net=host --gpus=all gpt-academic +# How to run | 如何运行 (2) 我想运行之前进容器做一些调整: +docker run --rm -it --net=host --gpus=all gpt-academic bash ``` -"Super English to Chinese Translation": { - # Prefix, which will be added before your input. For example, it is used to describe your requirements, such as translation, code interpretation, polishing, etc. - "Prefix": "Please translate the following content into Chinese, and then use a markdown table to explain each proprietary term in the text:\n\n", - - # Suffix, which will be added after your input. For example, in conjunction with the prefix, you can bracket your input in quotes. + +## Installation-Method 3: Other Deployment Methods + +1. Remote Cloud Server Deployment +Please visit [Deployment Wiki-1] (https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) + +2. Use WSL2 (Windows Subsystem for Linux) +Please visit [Deployment Wiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) + + +## Installation-Proxy Configuration +### Method 1: Conventional method +[Configure Proxy](https://github.com/binary-husky/chatgpt_academic/issues/1) + +### Method Two: Step-by-step tutorial for newcomers +[Step-by-step tutorial for newcomers](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) + +--- + +## Customizing Convenient Buttons (Customizing Academic Shortcuts) +Open `core_functional.py` with any text editor and add an item as follows, then restart the program (if the button has been successfully added and visible, both the prefix and suffix support hot modification without the need to restart the program to take effect). For example: +``` +"Super English to Chinese translation": { + # Prefix, which will be added before your input. For example, to describe your requirements, such as translation, code interpretation, polishing, etc. + "Prefix": "Please translate the following content into Chinese and use a markdown table to interpret the proprietary terms in the text one by one:\n\n", + + # Suffix, which will be added after your input. For example, combined with the prefix, you can put your input content in quotes. "Suffix": "", - }, ```
- -If you invent a more user-friendly academic shortcut key, welcome to post an issue or pull request! - -## Configure Proxy -### Method 1: General Method -Modify the port and proxy software corresponding in ```config.py``` - -
- - -
+--- -After configuring, you can use the following command to test whether the proxy works. If everything is normal, the code below will output the location of your proxy server: - -``` -python check_proxy.py -``` - -### Method Two: Pure Beginner Tutorial -[Pure Beginner Tutorial](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) - -## Compatibility Testing +## Some Function Displays ### Image Display: + +You are a professional academic paper translator. +
- -### If the program can read and analyze itself: +### If a program can understand and analyze itself:
@@ -227,7 +231,7 @@ python check_proxy.py
-### Any other Python/Cpp project analysis: +### Analysis of any Python/Cpp project:
@@ -236,59 +240,52 @@ python check_proxy.py -### Latex paper reading comprehension and abstract generation with one click +### One-click reading comprehension and summary generation of Latex papers
-### Automatic Report Generation +### Automatic report generation
-### Modular Function Design +### Modular functional design
- -### Translating source code to English +### Source code translation to English
-## Todo and Version Planning: - -- version 3 (Todo): -- - Support for gpt4 and other llm -- version 2.4+ (Todo): -- - Summary of long text and token overflow problems in large project source code -- - Implementation of project packaging and deployment -- - Function plugin parameter interface optimization -- - Self-updating -- version 2.4: (1) Added PDF full-text translation function; (2) Added input area switching function; (3) Added vertical layout option; (4) Optimized multi-threaded function plugin. +## Todo and version planning: +- version 3.2+ (todo): Function plugin supports more parameter interfaces +- version 3.1: Support for inquiring multiple GPT models at the same time! Support for api2d, support for multiple apikeys load balancing +- version 3.0: Support for chatglm and other small llms +- version 2.6: Refactored the plugin structure, improved interactivity, added more plugins +- version 2.5: Self-updating, solves the problem of text being too long and token overflowing when summarizing large project source code +- version 2.4: (1) Added PDF full text translation function; (2) Added function to switch input area position; (3) Added vertical layout option; (4) Multi-threaded function plugin optimization. - version 2.3: Enhanced multi-threaded interactivity -- version 2.2: Function plug-in supports hot reloading -- version 2.1: Collapsible layout +- version 2.2: Function plugin supports hot reloading +- version 2.1: Foldable layout - version 2.0: Introduction of modular function plugins - version 1.0: Basic functions -## References and Learning - +## Reference and learning ``` -The code refers to the design of many other excellent projects, mainly including: +The code design of this project has referenced many other excellent projects, including: -# Reference Project 1: Referenced the method of reading OpenAI json, recording historical inquiry records, and using gradio queue in ChuanhuChatGPT +# Reference project 1: Borrowed many tips from ChuanhuChatGPT https://github.com/GaiZhenbiao/ChuanhuChatGPT -# Reference Project 2: +# Reference project 2: Tsinghua ChatGLM-6B: https://github.com/THUDM/ChatGLM-6B - ``` - diff --git a/docs/README_FR.md b/docs/README_FR.md new file mode 100644 index 0000000..f21e900 --- /dev/null +++ b/docs/README_FR.md @@ -0,0 +1,296 @@ +> **Note** +> +> Ce fichier README est généré automatiquement par le plugin de traduction markdown de ce projet et n'est peut - être pas correct à 100%. +> + +# ChatGPT Optimisation Académique + +**Si vous aimez ce projet, donnez-lui une étoile; si vous avez inventé des raccourcis académiques plus utiles ou des plugins fonctionnels, n'hésitez pas à ouvrir une demande ou une demande de traction. Nous avons également un fichier README en [anglais|](docs/README_EN.md)[japonais|](docs/README_JP.md)[russe|](docs/README_RS.md)[français](docs/README_FR.md) traduit par ce projet lui-même.** + +> **Note** +> +> 1. Veuillez noter que seuls les plugins de fonction signalés en **rouge** sont capables de lire les fichiers, certains plugins se trouvent dans le **menu déroulant** de la section plugin. Nous sommes également les bienvenus avec la plus haute priorité pour traiter et accepter tout nouveau PR de plugin! +> +> 2. Chaque fichier dans ce projet est expliqué en détail dans l'auto-analyse [self_analysis.md](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A). Avec l'itération des versions, vous pouvez également cliquer sur les plugins fonctionnels pertinents pour appeler GPT et générer un rapport d'auto-analyse projet mis à jour. Les questions fréquemment posées sont résumées dans le [wiki](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98). +> + +
+ +Fonctionnalité | Description +--- | --- +Polissage en un clic | Prend en charge la correction en un clic et la recherche d'erreurs de syntaxe dans les documents de recherche. +Traduction Chinois-Anglais en un clic | Une touche pour traduire la partie chinoise en anglais ou celle anglaise en chinois. +Explication de code en un clic | Affiche et explique correctement le code. +[Raccourcis clavier personnalisables](https://www.bilibili.com/video/BV14s4y1E7jN) | Prend en charge les raccourcis clavier personnalisables. +[Configuration du serveur proxy](https://www.bilibili.com/video/BV1rc411W7Dr) | Prend en charge la configuration du serveur proxy. +Conception modulaire | Prend en charge la personnalisation des plugins de fonctions et des [plugins] de fonctions hiérarchiques personnalisés, et les plugins prennent en charge [la mise à jour à chaud](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97). +[Auto-analyse du programme](https://www.bilibili.com/video/BV1cj411A7VW) | [Plugins] [Lire en un clic](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A) le code source de ce projet. +[Analyse de programme](https://www.bilibili.com/video/BV1cj411A7VW) | [Plugins] En un clic, les projets Python/C/C++/Java/Lua/... peuvent être analysés. +Lire le document de recherche | [Plugins] Lisez le résumé de l'article en latex et générer un résumé. +Traduction et polissage de l'article complet en LaTeX | [Plugins] Une touche pour traduire ou corriger en LaTeX +Génération Commentaire de fonction en vrac | [Plugins] Lisez en un clic les fonctions et générez des commentaires de fonction. +Rapport d'analyse automatique des chats générés | [Plugins] Génère un rapport de synthèse après l'exécution. +[Assistant arxiv](https://www.bilibili.com/video/BV1LM4y1279X) | [Plugins] Entrez l'url de l'article arxiv pour traduire le résumé + télécharger le PDF en un clic +[Traduction complète des articles PDF](https://www.bilibili.com/video/BV1KT411x7Wn) | [Plugins] Extraire le titre et le résumé de l'article PDF + Traduire le texte entier (multithread) +[Aide à la recherche Google Academ](https://www.bilibili.com/video/BV19L411U7ia) | [Plugins] Donnez à GPT l'URL de n'importe quelle page de recherche Google Academ pour vous aider à sélectionner des articles intéressants +Affichage de formules/images/tableaux | Afficher la forme traduite et rendue d'une formule en même temps, plusieurs formules et surlignage du code prend en charge +Prise en charge des plugins multithread | Prise en charge de l'appel multithread de chatgpt, traitement en masse de texte ou de programmes en un clic +Activer le thème Gradio sombre [theme](https://github.com/binary-husky/chatgpt_academic/issues/173) au démarrage | Ajoutez ```/?__dark-theme=true``` à l'URL du navigateur pour basculer vers le thème sombre +[Prise en charge de plusieurs modèles LLM](https://www.bilibili.com/video/BV1wT411p7yf), [prise en charge de l'interface API2D](https://api2d.com/) | Comment cela serait-il de se faire servir par GPT3.5, GPT4 et la [ChatGLM de Tsinghua](https://github.com/THUDM/ChatGLM-6B) en même temps? +Expérience en ligne d'huggingface sans science | Après vous être connecté à huggingface, copiez [cet espace](https://huggingface.co/spaces/qingxu98/gpt-academic) +... | ... + +
+ + +Vous êtes un traducteur professionnel d'articles universitaires en français. + +Ceci est un fichier Markdown, veuillez le traduire en français sans modifier les commandes Markdown existantes : + +- Nouvelle interface (modifiable en modifiant l'option de mise en page dans config.py pour basculer entre les mises en page gauche-droite et haut-bas) +
+ +
+ + +- Tous les boutons sont générés dynamiquement en lisant functional.py, les utilisateurs peuvent ajouter librement des fonctions personnalisées pour libérer le presse-papiers. +
+ +
+ +- Correction/amélioration +
+ +
+ +- Si la sortie contient des formules, elles seront affichées simultanément sous forme de de texte brut et de forme rendue pour faciliter la copie et la lecture. +
+ +
+ +- Pas envie de lire le code du projet ? Faites votre propre démo avec ChatGPT. +
+ +
+ +- Utilisation combinée de plusieurs modèles de langage sophistiqués (ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4) +
+ +
+ +Utilisation combinée de plusieurs modèles de langage sophistiqués en version de test [huggingface](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta) (la version huggingface ne prend pas en charge Chatglm). + + +--- + +## Installation - Méthode 1 : Exécution directe (Windows, Linux or MacOS) + +1. Téléchargez le projet +```sh +git clone https://github.com/binary-husky/chatgpt_academic.git +cd chatgpt_academic +``` + +2. Configuration de l'API_KEY et des paramètres de proxy + +Dans `config.py`, configurez les paramètres de proxy et de clé d'API OpenAI, comme indiqué ci-dessous +``` +1. Si vous êtes en Chine, vous devez configurer un proxy étranger pour utiliser l'API OpenAI en toute transparence. Pour ce faire, veuillez lire attentivement le fichier config.py (1. Modifiez l'option USE_PROXY ; 2. Modifiez les paramètres de proxies comme indiqué dans les instructions). +2. Configurez votre clé API OpenAI. Vous devez vous inscrire sur le site web d'OpenAI pour obtenir une clé API. Une fois que vous avez votre clé API, vous pouvez la configurer dans le fichier config.py. +3. Tous les problèmes liés aux réseaux de proxy (temps d'attente, non-fonctionnement des proxies) sont résumés dans https://github.com/binary-husky/chatgpt_academic/issues/1. +``` +(Remarque : le programme vérifie d'abord s'il existe un fichier de configuration privé nommé `config_private.py`, et utilise les configurations de celui-ci à la place de celles du fichier `config.py`. Par conséquent, si vous comprenez notre logique de lecture de configuration, nous vous recommandons fortement de créer un nouveau fichier de configuration nommé `config_private.py` à côté de `config.py` et de transférer (copier) les configurations de celui-ci dans `config_private.py`. `config_private.py` n'est pas contrôlé par git et rend vos informations personnelles plus sûres.) + +3. Installation des dépendances +```sh +# (Option 1) Recommandé +python -m pip install -r requirements.txt + +# (Option 2) Si vous utilisez anaconda, les étapes sont similaires : +# (Option 2.1) conda create -n gptac_venv python=3.11 +# (Option 2.2) conda activate gptac_venv +# (Option 2.3) python -m pip install -r requirements.txt + +# note : Utilisez la source pip officielle ou la source pip Alibaba. D'autres sources (comme celles des universités) pourraient poser problème. Pour utiliser temporairement une autre source, utilisez : +# python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ +``` + +Si vous avez besoin de soutenir ChatGLM de Tsinghua, vous devez installer plus de dépendances (si vous n'êtes pas familier avec Python ou que votre ordinateur n'est pas assez performant, nous vous recommandons de ne pas essayer) : +```sh +python -m pip install -r request_llm/requirements_chatglm.txt +``` + +4. Exécution +```sh +python main.py +``` + +5. Tester les plugins de fonctions +``` +- Test Python Project Analysis + Dans la zone de saisie, entrez `./crazy_functions/test_project/python/dqn`, puis cliquez sur "Parse Entire Python Project" +- Test d'auto-lecture du code + Cliquez sur "[Démo multi-thread] Parser ce projet lui-même (auto-traduction de la source)" +- Test du modèle de fonctionnalité expérimentale (exige une réponse de l'IA à ce qui est arrivé aujourd'hui dans l'histoire). Vous pouvez utiliser cette fonctionnalité comme modèle pour des fonctions plus complexes. + Cliquez sur "[Démo modèle de plugin de fonction] Histoire du Jour" +- Le menu déroulant de la zone de plugin de fonctionnalité contient plus de fonctionnalités à sélectionner. +``` + +## Installation - Méthode 2 : Utilisation de docker (Linux) + + +Vous êtes un traducteur professionnel d'articles académiques en français. + +1. ChatGPT seul (recommandé pour la plupart des gens) +``` sh +# Télécharger le projet +git clone https://github.com/binary-husky/chatgpt_academic.git +cd chatgpt_academic +# Configurer le proxy outre-mer et la clé API OpenAI +Modifier le fichier config.py avec n'importe quel éditeur de texte +# Installer +docker build -t gpt-academic . +# Exécuter +docker run --rm -it --net=host gpt-academic + +# Tester les modules de fonction +## Tester la fonction modèle des modules (requiert la réponse de GPT à "qu'est-ce qui s'est passé dans l'histoire aujourd'hui ?"), vous pouvez utiliser cette fonction en tant que modèle pour implémenter des fonctions plus complexes. +Cliquez sur "[Exemple de modèle de module] Histoire d'aujourd'hui" +## Tester le résumé écrit pour le projet LaTeX +Dans la zone de saisie, tapez ./crazy_functions/test_project/latex/attention, puis cliquez sur "Lire le résumé de l'article de recherche LaTeX" +## Tester l'analyse du projet Python +Dans la zone de saisie, tapez ./crazy_functions/test_project/python/dqn, puis cliquez sur "Analyser l'ensemble du projet Python" + +D'autres fonctions sont disponibles dans la liste déroulante des modules de fonction. +``` + +2. ChatGPT+ChatGLM (nécessite une grande connaissance de docker et une configuration informatique suffisamment puissante) +``` sh +# Modifier le dockerfile +cd docs && nano Dockerfile+ChatGLM +# Comment construire | 如何构建 (Dockerfile+ChatGLM在docs路径下,请先cd docs) +docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . +# Comment exécuter | 如何运行 (1) Directement exécuter : +docker run --rm -it --net=host --gpus=all gpt-academic +# Comment exécuter | 如何运行 (2) Je veux effectuer quelques ajustements dans le conteneur avant de lancer : +docker run --rm -it --net=host --gpus=all gpt-academic bash +``` + +## Installation - Méthode 3 : Autres méthodes de déploiement + +1. Déploiement sur un cloud serveur distant +Veuillez consulter le [wiki de déploiement-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) + +2. Utilisation de WSL2 (Windows Subsystem for Linux) +Veuillez consulter le [wiki de déploiement-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) + + +## Configuration de la procuration de l'installation +### Méthode 1 : Méthode conventionnelle +[Configuration de la procuration](https://github.com/binary-husky/chatgpt_academic/issues/1) + +### Méthode 2 : Tutoriel pour débutant pur +[Tutoriel pour débutant pur](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) + + +--- + +## Personnalisation des nouveaux boutons pratiques (personnalisation des raccourcis académiques) +Ouvrez le fichier `core_functional.py` avec n'importe quel éditeur de texte, ajoutez les éléments suivants, puis redémarrez le programme. (Si le bouton a déjà été ajouté avec succès et est visible, le préfixe et le suffixe pris en charge peuvent être modifiés à chaud sans avoir besoin de redémarrer le programme.) +Par exemple: +``` +"Traduction Français-Chinois": { + # Préfixe, qui sera ajouté avant votre saisie. Par exemple, pour décrire votre demande, telle que la traduction, le débogage de code, l'amélioration, etc. + "Prefix": "Veuillez traduire le contenu ci-dessous en chinois, puis expliquer chaque terme propre mentionné dans un tableau Markdown :\n\n", + + # Suffixe, qui sera ajouté après votre saisie. Par exemple, en combinaison avec un préfixe, vous pouvez mettre le contenu de votre saisie entre guillemets. + "Suffix": "", +}, +``` + +
+ +
+ +--- + + +## Présentation de certaines fonctionnalités + +### Affichage des images: + +
+ +
+ + +### Si un programme peut comprendre et décomposer lui-même : + +
+ +
+ +
+ +
+ + +### Analyse de tout projet Python/Cpp quelconque : +
+ +
+ +
+ +
+ +### Lecture et résumé générés automatiquement pour les articles en Latex +
+ +
+ +### Génération de rapports automatique +
+ + + +
+ +### Conception de fonctionnalités modulaires +
+ + +
+ + +### Traduction de code source en anglais + +
+ +
+ +## À faire et planification de version : +- version 3.2+ (à faire) : Prise en charge de plus de paramètres d'interface de plugin de fonction +- version 3.1 : Prise en charge de l'interrogation simultanée de plusieurs modèles GPT ! Prise en charge de l'API2d, prise en charge de la répartition de charge de plusieurs clés API +- version 3.0 : Prise en charge de chatglm et d'autres petits llm +- version 2.6 : Réorganisation de la structure du plugin, amélioration de l'interactivité, ajout de plus de plugins +- version 2.5 : Mise à jour automatique, résolution du problème de dépassement de jeton et de texte trop long lors de la compilation du code source complet +- version 2.4 : (1) Ajout de la fonctionnalité de traduction intégrale de PDF ; (2) Ajout d'une fonctionnalité de changement de position de zone de saisie ; (3) Ajout d'une option de disposition verticale ; (4) Optimisation du plugin de fonction multi-thread. +- version 2.3 : Amélioration de l'interactivité multi-thread +- version 2.2 : Prise en charge du rechargement à chaud du plugin de fonction +- version 2.1 : Mise en page pliable +- version 2.0 : Introduction du plugin de fonction modulaire +- version 1.0 : Fonctionnalité de base + +## Références et apprentissage + +``` +De nombreux designs d'autres projets exceptionnels ont été utilisés pour référence dans le code, notamment : + +# Projet 1 : De nombreuses astuces ont été empruntées à ChuanhuChatGPT +https://github.com/GaiZhenbiao/ChuanhuChatGPT + +# Projet 2 : ChatGLM-6B de Tsinghua : +https://github.com/THUDM/ChatGLM-6B +``` + diff --git a/docs/README_JP.md b/docs/README_JP.md new file mode 100644 index 0000000..c08c27b --- /dev/null +++ b/docs/README_JP.md @@ -0,0 +1,294 @@ +> **Note** +> +> このReadmeファイルは、このプロジェクトのmarkdown翻訳プラグインによって自動的に生成されたもので、100%正確ではない可能性があります。 +> + +# ChatGPT 学術最適化 + +**このプロジェクトが気に入ったらスターを付けてください。また、より便利な学術用ショートカットまたは機能プラグインを考案された場合は、問題を開いたり、プルリクエストを投げたりすることができます。さらに、このプロジェクト自体によって翻訳された[README(英語)](docs/README_EN.md)もあります。** + +> **注意** +> +> 1. 取り込まれたファイルを読み込むことができるのは、**赤色**で表示されている関数プラグイン(ボタン)のみであり、一部のプラグインはプラグイン領域の**ドロップダウンメニュー**にあります。さらに、新しいプラグインに関するいかなるPRでも、最優先で歓迎・処理いたします。 +> +> 2. このプロジェクトの各ファイルの機能に関しては、[`self_analysis.md`](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)で詳しく説明されています。バージョンが進化するにつれて、関連する関数プラグインをクリックしてChatGPTを呼び出し、プロジェクトの自己解析レポートを再生成することもいつでもできます。一般的な問題は[`wiki`](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)にまとめられています。 + + +
+ +機能 | 説明 +--- | --- +一键修正 | 一键で論文の文法エラーを検索・修正できます +一键日英翻訳 | 一键で英日の相互翻訳ができます。 +コードの自動解釈 | コードが正しく表示され、解釈できます。 +[カスタムショートカット](https://www.bilibili.com/video/BV14s4y1E7jN) | カスタムショートカットがサポートされます。 +[プロキシサーバーの設定](https://www.bilibili.com/video/BV1rc411W7Dr) | プロキシサーバーの設定ができます。 +モジュール化されたデザイン | 任意の高次の関数プラグインと[関数プラグイン]がカスタムされ、プラグインは[ホットアップデート](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97)をサポートします。 +[自己解析プログラム](https://www.bilibili.com/video/BV1cj411A7VW) | [関数プラグイン][一键分析](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)はこのプロジェクトのソースコードを解析することができます。 +[プログラムの解析](https://www.bilibili.com/video/BV1cj411A7VW) | [関数プラグイン] 一件で他のPython/C/C++/Java/Lua/...プロジェクトツリーを解析できます。 +論文の読解 | [関数プラグイン] 一件でLaTeX論文全文を解読し、要旨を生成できます。 +LaTeX全文翻訳、修正 | [関数プラグイン] 一键でLaTeX論文を翻訳または修正できます。 +一括コードコメント生成 | [関数プラグイン] 一件で関数コメントを自動生成できます。 +chatレポートの自動生成 | [関数プラグイン] 実行後、自動的にサマリーレポートを生成します。 +[arxivアシスタント](https://www.bilibili.com/video/BV1LM4y1279X) | [関数プラグイン] arxiv記事URLを入力すると、要約を自動翻訳+PDFをダウンロードできます。 +[PDF論文全文翻訳機能](https://www.bilibili.com/video/BV1KT411x7Wn) | [関数プラグイン] PDF論文のタイトルと要旨を抽出しながら、全文を翻訳できます(マルチスレッド)。 +[Google Scholar統合アシスタント](https://www.bilibili.com/video/BV19L411U7ia) | [関数プラグイン] Google Scholarの検索ページURLを入力すると、gptが興味深い記事を選択します。 +数式、画像、表の表示 | 数式のtex形式とレンダリング形式を同時に表示し、公式、コードのハイライトがサポートされます。 +マルチスレッド関数プラグインサポート | chatgptのマルチスレッド呼び出しがサポートされ、大量のテキストまたはプログラムを一括で処理できます。 +Dark gradioスタイルの起動(https://github.com/binary-husky/chatgpt_academic/issues/173) | ブラウザのURLに```/?__dark-theme=true```を追加すると、ダークテーマに切り替えることができます。 +[多数のLLMモデル](https://www.bilibili.com/video/BV1wT411p7yf)のサポート、[API2D](https://api2d.com/)インターフェースのサポート | 同時に、GPT3.5、GPT4、[清華ChatGLM](https://github.com/THUDM/ChatGLM-6B)がサポートされている感覚は、きっと素晴らしいはずですね? +huggingfaceが提供する科学技術ウェブ[体験版](https://huggingface.co/spaces/qingxu98/gpt-academic) | huggingfaceにログインした後[このスペース](https://huggingface.co/spaces/qingxu98/gpt-academic)をコピーしてください。 +…… | …… + +
+ +- 新しいインターフェース(config.pyのLAYOUTオプションを変更すると、左右のレイアウトと上下のレイアウトを切り替えることができます)。 + +
+ +
+ + +- All buttons are dynamically generated by reading functional.py, and custom functions can be added, freeing up the clipboard. +
+ +
+ +- Polishing/Error Correction +
+ +
+ +- If the output contains formulas, it will be displayed in both tex form and rendering form at the same time for easy copying and reading. +
+ +
+ +- Tired of looking at the project code? Just show chatgpt's mouth the whole project +
+ +
+ +- Various large language models are mixed and called (ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4) +
+ +
+ +Multiple large language models mixed call [huggingface beta](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta) (huggingface version does not support chatglm) + + +--- + +## Installation-Method 1: Run Directly (Windows, Linux or MacOS) + +1. Download the project +```sh +git clone https://github.com/binary-husky/chatgpt_academic.git +cd chatgpt_academic +``` + +2. Configure API_KEY and proxy settings + +In `config.py`, configure the overseas proxy and OpenAI API KEY, as explained below. +``` +1. If you are in China, you need to set up overseas proxies to use the OpenAI API smoothly. Please read config.py carefully for the setting method (1. Change USE_PROXY to True; 2. Modify the proxies according to the instructions). +2. Configure OpenAI API KEY. You need to register and obtain the API KEY on the OpenAI official website. Once you have the API KEY, configure it in the config.py file. +3. Issues related to proxy networks (network timeout, proxies not working) are summarized at https://github.com/binary-husky/chatgpt_academic/issues/1 +``` +(P.S. When the program is running, it will first check whether there is a private configuration file named `config_private.py` and use the configuration therein to override the same-named configuration in `config.py`. Therefore, if you can understand our configuration reading logic, we strongly recommend that you create a new configuration file named `config_private.py` next to `config.py` and transfer (copy) the configuration in `config.py` to `config_private.py`. `config_private.py` is not controlled by git, which can make your privacy information more secure.) + +3. Install dependencies +```sh +# (Option 1) Recommended +python -m pip install -r requirements.txt + +# (Option 2) If you use anaconda, the steps are similar: +# (Option 2.1) conda create -n gptac_venv python=3.11 +# (Option 2.2) conda activate gptac_venv +# (Option 2.3) python -m pip install -r requirements.txt + +# Note: Use the official pip source or Ali pip source. Other pip sources (such as some universities' pip) may have problems. Temporary switching source method: +# python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ +``` + +If you need to support Tsinghua ChatGLM, you need to install more dependencies separately (if you are not familiar with Python or your computer configuration is not good, it is not recommended to try): +```sh +python -m pip install -r request_llm/requirements_chatglm.txt +``` + +4. Run +```sh +python main.py +``` + +5. Test function plugin +``` +- Test Python project analysis + In the input area, enter `./crazy_functions/test_project/python/dqn`, and then click "Analyze the Entire Python Project" +- Test self-code interpretation + Click "[Multi-threaded Demo] Analyze this Project Itself (Self-Translating Source Code)" +- Test experimental functional template function (requires gpt to answer what happened in history today), you can use this function as a template to implement more complex functions + Click "[Function Plugin Template Demo] In History Today" +- There are more features to choose from in the function plugin dropdown menu +``` + +## Installation-Method 2: Using Docker (Linux) + + +あなたは、プロの学術論文翻訳者です。 + +1. ChatGPTのみを使用(一般的にはこのオプションが推奨されます) +``` sh +# プロジェクトをダウンロードします +git clone https://github.com/binary-husky/chatgpt_academic.git +cd chatgpt_academic +# 海外ProxyとOpenAI API KEYを構成する +config.pyをテキストエディタで開いて編集します。 +# インストール +docker build -t gpt-academic . +# 実行 +docker run --rm -it --net=host gpt-academic + +# 関数プラグインのテスト +## 関数プラグインテンプレート関数をテストします(gptが「今日の歴史で何が起こったか」と答えるように要求します)。この関数をテンプレートとして使用して、より複雑な機能を実装できます。 +[関数プラグインテンプレートDemo]をクリックしてください。 +## LaTexプロジェクトの要約を書くテスト +入力エリアに./crazy_functions/test_project/latex/attentionを入力し、「LaTex論文を読んで要約を書く」をクリックします。 +## Pythonプロジェクトの解析をテストする +入力エリアに./crazy_functions/test_project/python/dqnを入力して、「Pythonプロジェクトを解析する」をクリックします。 + +より多くの機能が関数プラグインエリアのドロップダウンメニューで利用可能です。 +``` + +2. ChatGPT+ChatGLMを使用する(Dockerに非常に詳しい必要があります + コンピュータの構成が十分に強力な必要があります) + +``` sh +# dockerfileを変更する +cd docs && nano Dockerfile+ChatGLM +# How to build | 構築方法(Dockerfile+ChatGLMはdocsのパスにありますので、まずcd docsを実行してください) +docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . +# How to run | 実行方法 (1) 直接実行: +docker run --rm -it --net=host --gpus=all gpt-academic +# How to run | 実行方法 (2) コンテナに入って調整することを予め行いたい場合: +docker run --rm -it --net=host --gpus=all gpt-academic bash +``` + + +## Installation-Method 3: その他のデプロイ方法 + +1. クラウドサーバーにリモートでインストールする +[デプロイwiki-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) + +2. WSL2を使用する(Windows Subsystem for Linuxサブシステム) +[デプロイwiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) + + +## Installation-プロキシ構成 +### 方法1: 標準的な方法 +[プロキシの構成](https://github.com/binary-husky/chatgpt_academic/issues/1) + +### 方法2:初心者向けのチュートリアル +[初心者向けチュートリアル](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) + + +--- + +## カスタムコンビニエンスボタンの作成方法(学術用ショートカットキーをカスタマイズする) +任意のテキストエディタで`core_functional.py`を開き、以下のエントリを追加し、プログラムを再起動します。 (ボタンが正常に追加されて表示されている場合、プレフィックスとサフィックスは両方ともリアルタイムで変更できるため、プログラムを再起動する必要はありません。) +例えば +``` +"超级英译中": { + # プレフィックス、あなたの要求を説明するために使用されます。翻訳、コードの解析、精緻化など。 + "Prefix": "以下の内容を中国語に翻訳し、専門用語が含まれる場合はマークダウン表で1つずつ説明してください:\n\n", + + #サフィックス、プレフィックスと組み合わせて、入力内容を引用符で囲むことができます。 + "Suffix": "", +}, +``` +
+ +
+ +--- + + +## 一部の機能のデモ + +### 画像表示: + +
+ +
+ + +### プログラムが自分自身を読み取り、分解できる場合: + +
+ +
+ +
+ +
+ + +### 他のPython/Cppプロジェクトの分析: +
+ +
+ +
+ +
+ +### LaTeX論文の読解と要約の自動生成 +
+ +
+ +### 自動レポート生成 +
+ + + +
+ +### モジュール化された機能設計 +
+ + +
+ + +### ソースコードを英語に変換する + +
+ +
+ +## タスクとバージョン計画: +- version 3.2+ (todo): 関数プラグインがより多くのパラメーターインターフェースをサポート +- version 3.1: 複数のgptモデルを同時に問い合わせるサポート! api2dをサポートし、複数のapikeyの負荷分散をサポート +- version 3.0: chatglmおよびその他の小規模llmのサポート +- version 2.6: プラグイン構造を再構築し、対話性を強化し、さらに多くのプラグインを追加 +- version 2.5: 自動更新、長い文章、トークンのオーバーフローを解決する +- version 2.4: (1) PDF全文翻訳機能を追加。 (2)位置の切り替え機能を追加。 (3)垂直レイアウトオプションを追加。 (4)マルチスレッド関数プラグインの最適化。 +- version 2.3: マルチスレッドの相互作用の強化 +- version 2.2: 関数プラグインのホットリロードをサポート +- version 2.1: 折りたたみ式レイアウト +- version 2.0: モジュール化された関数プラグインを導入 +- version 1.0: 基本機能 + +## 参考と学習 + +``` +多くの優れたプロジェクトの設計を参考にしています: + +# 参考プロジェクト1:ChuanhuChatGPTから多くの技術を借りました +https://github.com/GaiZhenbiao/ChuanhuChatGPT + +# 参考プロジェクト2:清華ChatGLM-6B: +https://github.com/THUDM/ChatGLM-6B +``` + + diff --git a/docs/README_RS.md b/docs/README_RS.md new file mode 100644 index 0000000..f8d925a --- /dev/null +++ b/docs/README_RS.md @@ -0,0 +1,291 @@ +> **Note** +> +> Этот файл самовыражения автоматически генерируется модулем перевода markdown в этом проекте и может быть не на 100% правильным. +> + +# ChatGPT Academic Optimization + +**Если вам понравился этот проект, пожалуйста, поставьте ему звезду. Если вы придумали более полезные академические ярлыки или функциональные плагины, не стесняйтесь создавать запросы на изменение или пул-запросы. Мы также имеем [README на английском языке](docs/README_EN.md), переведенный этим же проектом. + +> **Примечание** +> +> 1. Пожалуйста, обратите внимание, что только функциonal plugins (buttons) с **красным цветом** могут читать файлы, некоторые из которых находятся в **выпадающем меню** плагинов. Кроме того, мы приветствуем и обрабатываем любые новые плагины с **наивысшим приоритетом**! +> +> 2. Функции каждого файла в этом проекте подробно описаны в собственном анализе [`self_analysis.md`](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A) . При повторных итерациях вы также можете вызывать обновленный отчет функций проекта, щелкнув соответствующий функциональный плагин GPT. Часто задаваемые вопросы собраны в [`wiki`](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) . + +
+ +Функция | Описание +--- | --- +Редактирование одним кликом | Поддержка редактирования одним кликом, поиск грамматических ошибок в академических статьях +Переключение языков "Английский-Китайский" одним кликом | Одним кликом переключайте языки "Английский-Китайский" +Разъяснение программного кода одним кликом | Вы можете правильно отобразить и объяснить программный код. +[Настраиваемые сочетания клавиш](https://www.bilibili.com/video/BV14s4y1E7jN) | Поддержка настраиваемых сочетаний клавиш +[Настройка сервера-прокси](https://www.bilibili.com/video/BV1rc411W7Dr) | Поддержка настройки сервера-прокси +Модульный дизайн | Поддержка настраиваемых функциональных плагинов высших порядков и функциональных плагинов, поддерживающих [горячее обновление](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97) +[Автоанализ программы](https://www.bilibili.com/video/BV1cj411A7VW) | [Функциональный плагин] [Прочтение в один клик](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A) кода программы проекта +[Анализ программы](https://www.bilibili.com/video/BV1cj411A7VW) | [Функциональный плагин] Один клик для проанализирования дерева других проектов Python/C/C++/Java/Lua/... +Чтение статей| [Функциональный плагин] Одним кликом прочитайте весь латех (LaTex) текст статьи и сгенерируйте краткое описание +Перевод и редактирование всех статей из LaTex | [Функциональный плагин] Перевод или редактирование LaTex-статьи всего одним нажатием кнопки +Генерация комментариев в пакетном режиме | [Функциональный плагин] Одним кликом сгенерируйте комментарии к функциям в пакетном режиме +Генерация отчетов пакета CHAT | [Функциональный плагин] Автоматически создавайте сводные отчеты после выполнения +[Помощник по arxiv](https://www.bilibili.com/video/BV1LM4y1279X) | [Функциональный плагин] Введите URL статьи arxiv, чтобы легко перевести резюме и загрузить PDF-файл +[Перевод полного текста статьи в формате PDF](https://www.bilibili.com/video/BV1KT411x7Wn) | [Функциональный плагин] Извлеките заголовок статьи, резюме и переведите весь текст статьи (многопоточно) +[Помощник интеграции Google Scholar](https://www.bilibili.com/video/BV19L411U7ia) | [Функциональный плагин] Дайте GPT выбрать для вас интересные статьи на любой странице поиска Google Scholar. +Отображение формул/изображений/таблиц | Одновременно отображается tex-форма и рендер-форма формул, поддержка формул, высокоскоростных кодов +Поддержка функциональных плагинов многопоточности | Поддержка многопоточной работы с плагинами, обрабатывайте огромные объемы текста или программы одним кликом +Запуск темной темы gradio[подробнее](https://github.com/binary-husky/chatgpt_academic/issues/173) | Добавьте / ?__dark-theme=true в конец URL браузера, чтобы переключиться на темную тему. +[Поддержка нескольких моделей LLM](https://www.bilibili.com/video/BV1wT411p7yf), поддержка API2D | Находиться между GPT3.5, GPT4 и [清华ChatGLM](https://github.com/THUDM/ChatGLM-6B) должно быть очень приятно, не так ли? +Альтернатива huggingface без использования научной сети [Онлайн-эксперимент](https://huggingface.co/spaces/qingxu98/gpt-academic) | Войдите в систему, скопируйте пространство [этот пространственный URL](https://huggingface.co/spaces/qingxu98/gpt-academic) +…… | …… + + +
+ +- Новый интерфейс (вы можете изменить настройку LAYOUT в config.py, чтобы переключаться между "горизонтальным расположением" и "вертикальным расположением") +
+ +
+ + +Вы профессиональный переводчик научных статей. + +- Все кнопки генерируются динамически путем чтения functional.py и могут быть легко настроены под пользовательские потребности, освобождая буфер обмена. +
+ +
+ +- Редактирование/корректирование +
+ +
+ +- Если вывод содержит формулы, они отображаются одновременно как в формате tex, так и в рендеринговом формате для удобства копирования и чтения. +
+ +
+ +- Лень смотреть код проекта? Просто покажите chatgpt. +
+ +
+ +- Несколько моделей больших языковых моделей смешиваются (ChatGLM + OpenAI-GPT3.5 + [API2D] (https://api2d.com/) -GPT4) +
+ +
+ +Несколько моделей больших языковых моделей смешиваются в [бета-версии huggingface] (https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta) (huggingface-версия не поддерживает chatglm). + + +--- + +## Установка - Метод 1: Запуск (Windows, Linux или MacOS) + +1. Скачайте проект +```sh +git clone https://github.com/binary-husky/chatgpt_academic.git +cd chatgpt_academic +``` + +2. Настройка API_KEY и настройки прокси + +В файле `config.py` настройте зарубежный прокси и OpenAI API KEY, пояснения ниже +``` +1. Если вы находитесь в Китае, вам нужно настроить зарубежный прокси, чтобы использовать OpenAI API. Пожалуйста, внимательно прочитайте config.py для получения инструкций (1. Измените USE_PROXY на True; 2. Измените прокси в соответствии с инструкциями). +2. Настройка API KEY OpenAI. Вам необходимо зарегистрироваться на сайте OpenAI и получить API KEY. После получения API KEY настройте его в файле config.py. +3. Вопросы, связанные с сетевыми проблемами (тайм-аут сети, прокси не работает), можно найти здесь: https://github.com/binary-husky/chatgpt_academic/issues/1 +``` +(Примечание: при запуске программы будет проверяться наличие конфиденциального файла конфигурации с именем `config_private.py` и использоваться в нем конфигурация параметров, которая перезаписывает параметры с такими же именами в `config.py`. Поэтому, если вы понимаете логику чтения нашей конфигурации, мы настоятельно рекомендуем вам создать новый файл конфигурации с именем `config_private.py` рядом с `config.py` и переместить (скопировать) настройки из `config.py` в `config_private.py`. `config_private.py` не подвергается контролю git, что делает конфиденциальную информацию более безопасной.) + + +3. Установить зависимости +```sh +# (Выбор 1) Рекомендуется +python -m pip install -r requirements.txt + +# (Выбор 2) Если вы используете anaconda, то шаги будут аналогичны: +# (Шаг 2.1) conda create -n gptac_venv python=3.11 +# (Шаг 2.2) conda activate gptac_venv +# (Шаг 2.3) python -m pip install -r requirements.txt + +# Примечание: используйте официальный источник pip или источник pip.aliyun.com. Другие источники pip могут вызывать проблемы. временный метод замены источника: +# python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ +``` + +Если требуется поддержка TUNA ChatGLM, необходимо установить дополнительные зависимости (если вы неудобны с python, необходимо иметь хорошую конфигурацию компьютера): +```sh +python -m pip install -r request_llm/requirements_chatglm.txt +``` + +4. Запустите +```sh +python main.py +``` + +5. Тестовые функции плагина +``` +- Тестирвоание анализа проекта Python + В основной области введите `./crazy_functions/test_project/python/dqn` , а затем нажмите "Анализировать весь проект Python" +- Тестирование самостоятельного чтения кода + Щелкните " [Демонстрационный режим многопоточности] Проанализируйте сам проект (расшифровка источника кода)" +- Тестирование функций шаблонного плагина (вы можете использовать эту функцию как шаблон для более сложных функций, требующих ответа от gpt в связи с тем, что произошло сегодня в истории) + Щелкните " [Функции шаблонного плагина] День в истории" +- На нижней панели дополнительные функции для выбора +``` + +## Установка - Метод 2: Использование docker (Linux) + + +1. Только ChatGPT (рекомендуется для большинства пользователей): +``` sh +# Скачать проект +git clone https://github.com/binary-husky/chatgpt_academic.git +cd chatgpt_academic +# Настроить прокси за границей и OpenAI API KEY +Отредактируйте файл config.py в любом текстовом редакторе. +# Установка +docker build -t gpt-academic . +# Запустить +docker run --rm -it --net=host gpt-academic + +# Проверка функциональности плагина +## Проверка шаблонной функции плагина (требуется, чтобы gpt ответил, что произошло "в истории на этот день"), вы можете использовать эту функцию в качестве шаблона для реализации более сложных функций. +Нажмите "[Шаблонный демонстрационный плагин] История на этот день". +## Тест абстрактного резюме для проекта на Latex +В области ввода введите ./crazy_functions/test_project/latex/attention, а затем нажмите "Чтение реферата о тезисах статьи на LaTeX". +## Тестовый анализ проекта на Python +Введите в область ввода ./crazy_functions/test_project/python/dqn, затем нажмите "Проанализировать весь проект на Python". + +Выбирайте больше функциональных плагинов в нижнем выпадающем меню. +``` + +2. ChatGPT + ChatGLM (требуется глубокое знание Docker и достаточно мощное компьютерное оборудование): + +``` sh +# Изменение Dockerfile +cd docs && nano Dockerfile+ChatGLM +# Как построить | Как запустить (Dockerfile+ChatGLM в пути docs, сначала перейдите в папку с помощью cd docs) +docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . +# Как запустить | Как запустить (2) я хочу войти в контейнер и сделать какие-то настройки до запуска: +docker run --rm -it --net=host --gpus=all gpt-academic bash +``` + + +## Установка-Метод 3: Другие способы развертывания + +1. Развертывание на удаленном облачном сервере +Пожалуйста, посетите [Deploy Wiki-1] (https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) + +2. Использование WSL2 (Windows Subsystem for Linux) +Пожалуйста, посетите [Deploy Wiki-2] (https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) + + +## Установка-Настройки прокси +### Метод 1: Обычный способ +[Конфигурация прокси] (https://github.com/binary-husky/chatgpt_academic/issues/1) + +### Метод 2: Руководство новичка +[Руководство новичка] (https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) + + +--- + +## Настройка новой удобной кнопки (настройка быстрой клавиши для научной работы) +Откройте `core_functional.py` любым текстовым редактором, добавьте элементы, как показано ниже, затем перезапустите программу. (Если кнопка уже успешно добавлена и видна, то префикс и суффикс поддерживают горячее изменение, чтобы они оказались в действии, не нужно перезапускать программу.) +например +``` +"Супер анг-рус": { + # Префикс, будет добавлен перед вашим вводом. Например, используется для описания ваших потребностей, таких как перевод, кодинг, редактирование и т. д. + "Prefix": "Пожалуйста, переведите этот фрагмент на русский язык, а затем создайте пошаговую таблицу в markdown, чтобы объяснить все специализированные термины, которые встречаются в тексте:\n\n", + + # Суффикс, будет добавлен после вашего ввода. Например, совместно с префиксом можно обрамить ваш ввод в кавычки. + "Suffix": "", +}, +``` +
+ +
+ +--- + + +## Демонстрация некоторых возможностей + +### Отображение изображений: + +
+ +
+ + +### Если программа может понимать и разбирать сама себя: + +
+ +
+ +
+ +
+ + +### Анализ других проектов на Python/Cpp: +
+ +
+ +
+ +
+ +### Генерация понимания и абстрактов с помощью Латех статей в один клик +
+ +
+ +### Автоматическое создание отчетов +
+ + + +
+ +### Модульный дизайн функций +
+ + +
+ + +### Трансляция исходного кода на английский язык + +
+ +
+ +## Todo и планирование версий: +- version 3.2+ (todo): функция плагины поддерживают более многочисленные интерфейсы параметров +- version 3.1: поддержка одновременного опроса нескольких моделей gpt! Поддержка api2d, поддержка балансировки нагрузки множества apikey. +- version 3.0: поддержка chatglm и других маленьких llm +- version 2.6: реструктурировал структуру плагинов, повысил интерактивность, добавил больше плагинов +- version 2.5: само обновление, решение проблемы слишком длинного текста и переполнения токена при переводе всего проекта исходного кода +- version 2.4: (1) добавлена функция перевода всего PDF-документа; (2) добавлена функция изменения положения входной области; (3) добавлена опция вертикального макета; (4) оптимизация функций многопоточности плагина. +- version 2.3: улучшение многопоточной интерактивности +- version 2.2: функция плагинов поддерживает горячую перезагрузку +- version 2.1: блочная раскладка +- version 2.0: модульный дизайн функций плагина +- version 1.0: основные функции + +## Ссылки на изучение и обучение + +``` +В коде использовано много хороших дизайнерских решений из других отличных проектов, в том числе: + +# Project1: использование многих приемов из ChuanhuChatGPT +https://github.com/GaiZhenbiao/ChuanhuChatGPT + +# Project2: ChatGLM-6B в Тхуде: +https://github.com/THUDM/ChatGLM-6B +``` + From 205a6952a2b0926f583d2fcf432301f193b414d1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Apr 2023 17:04:04 +0800 Subject: [PATCH 48/50] =?UTF-8?q?=E5=A4=9A=E8=AF=AD=E8=A8=80README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/README_JP.md | 242 ++++++++++++++++++++++++---------------------- 2 files changed, 126 insertions(+), 118 deletions(-) diff --git a/README.md b/README.md index 695c0bd..9fefdcb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发issue或者pull requests** -If you like this project, please give it a Star. If you've come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request. We also have a README in [English|](docs/README_EN.md)[Japanese|](docs/README_JP.md)[Russian|](docs/README_RS.md)[French](docs/README_FR.md) translated by this project itself. +If you like this project, please give it a Star. If you've come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request. We also have a README in [English|](docs/README_EN.md)[日本語|](docs/README_JP.md)[Русский|](docs/README_RS.md)[Français](docs/README_FR.md) translated by this project itself. > **Note** > diff --git a/docs/README_JP.md b/docs/README_JP.md index c08c27b..9fc6dbe 100644 --- a/docs/README_JP.md +++ b/docs/README_JP.md @@ -5,214 +5,217 @@ # ChatGPT 学術最適化 -**このプロジェクトが気に入ったらスターを付けてください。また、より便利な学術用ショートカットまたは機能プラグインを考案された場合は、問題を開いたり、プルリクエストを投げたりすることができます。さらに、このプロジェクト自体によって翻訳された[README(英語)](docs/README_EN.md)もあります。** +**このプロジェクトが好きだったら、スターをつけてください。もし、より使いやすい学術用のショートカットキーまたはファンクションプラグインを発明した場合は、issueを発行するかpull requestを作成してください。また、このプロジェクト自体によって翻訳されたREADMEは[英語説明書|](docs/README_EN.md)[日本語説明書|](docs/README_JP.md)[ロシア語説明書|](docs/README_RS.md)[フランス語説明書](docs/README_FR.md)もあります。** -> **注意** +> **注意事項** > -> 1. 取り込まれたファイルを読み込むことができるのは、**赤色**で表示されている関数プラグイン(ボタン)のみであり、一部のプラグインはプラグイン領域の**ドロップダウンメニュー**にあります。さらに、新しいプラグインに関するいかなるPRでも、最優先で歓迎・処理いたします。 +> 1. **赤色**のラベルが付いているファンクションプラグイン(ボタン)のみファイルを読み込めます。一部のプラグインはプラグインエリアのドロップダウンメニューにあります。新しいプラグインのPRを歓迎いたします! > -> 2. このプロジェクトの各ファイルの機能に関しては、[`self_analysis.md`](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)で詳しく説明されています。バージョンが進化するにつれて、関連する関数プラグインをクリックしてChatGPTを呼び出し、プロジェクトの自己解析レポートを再生成することもいつでもできます。一般的な問題は[`wiki`](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)にまとめられています。 +> 2. このプロジェクトの各ファイルの機能は`self_analysis.md`(自己解析レポート)で詳しく説明されています。バージョンが追加されると、関連するファンクションプラグインをクリックして、GPTを呼び出して自己解析レポートを再生成することができます。一般的な質問は`wiki`にまとめられています。(`https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98`)
機能 | 説明 --- | --- -一键修正 | 一键で論文の文法エラーを検索・修正できます -一键日英翻訳 | 一键で英日の相互翻訳ができます。 -コードの自動解釈 | コードが正しく表示され、解釈できます。 -[カスタムショートカット](https://www.bilibili.com/video/BV14s4y1E7jN) | カスタムショートカットがサポートされます。 -[プロキシサーバーの設定](https://www.bilibili.com/video/BV1rc411W7Dr) | プロキシサーバーの設定ができます。 -モジュール化されたデザイン | 任意の高次の関数プラグインと[関数プラグイン]がカスタムされ、プラグインは[ホットアップデート](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97)をサポートします。 -[自己解析プログラム](https://www.bilibili.com/video/BV1cj411A7VW) | [関数プラグイン][一键分析](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)はこのプロジェクトのソースコードを解析することができます。 -[プログラムの解析](https://www.bilibili.com/video/BV1cj411A7VW) | [関数プラグイン] 一件で他のPython/C/C++/Java/Lua/...プロジェクトツリーを解析できます。 -論文の読解 | [関数プラグイン] 一件でLaTeX論文全文を解読し、要旨を生成できます。 -LaTeX全文翻訳、修正 | [関数プラグイン] 一键でLaTeX論文を翻訳または修正できます。 -一括コードコメント生成 | [関数プラグイン] 一件で関数コメントを自動生成できます。 -chatレポートの自動生成 | [関数プラグイン] 実行後、自動的にサマリーレポートを生成します。 -[arxivアシスタント](https://www.bilibili.com/video/BV1LM4y1279X) | [関数プラグイン] arxiv記事URLを入力すると、要約を自動翻訳+PDFをダウンロードできます。 -[PDF論文全文翻訳機能](https://www.bilibili.com/video/BV1KT411x7Wn) | [関数プラグイン] PDF論文のタイトルと要旨を抽出しながら、全文を翻訳できます(マルチスレッド)。 -[Google Scholar統合アシスタント](https://www.bilibili.com/video/BV19L411U7ia) | [関数プラグイン] Google Scholarの検索ページURLを入力すると、gptが興味深い記事を選択します。 -数式、画像、表の表示 | 数式のtex形式とレンダリング形式を同時に表示し、公式、コードのハイライトがサポートされます。 -マルチスレッド関数プラグインサポート | chatgptのマルチスレッド呼び出しがサポートされ、大量のテキストまたはプログラムを一括で処理できます。 -Dark gradioスタイルの起動(https://github.com/binary-husky/chatgpt_academic/issues/173) | ブラウザのURLに```/?__dark-theme=true```を追加すると、ダークテーマに切り替えることができます。 -[多数のLLMモデル](https://www.bilibili.com/video/BV1wT411p7yf)のサポート、[API2D](https://api2d.com/)インターフェースのサポート | 同時に、GPT3.5、GPT4、[清華ChatGLM](https://github.com/THUDM/ChatGLM-6B)がサポートされている感覚は、きっと素晴らしいはずですね? -huggingfaceが提供する科学技術ウェブ[体験版](https://huggingface.co/spaces/qingxu98/gpt-academic) | huggingfaceにログインした後[このスペース](https://huggingface.co/spaces/qingxu98/gpt-academic)をコピーしてください。 -…… | …… +ワンクリック整形 | 論文の文法エラーを一括で正確に修正できます。 +ワンクリック日英翻訳 | 日英翻訳には、ワンクリックで対応できます。 +ワンクリックコード説明 | コードの正しい表示と説明が可能です。 +[カスタムショートカットキー](https://www.bilibili.com/video/BV14s4y1E7jN) | カスタムショートカットキーをサポートします。 +[プロキシサーバーの設定](https://www.bilibili.com/video/BV1rc411W7Dr) | プロキシサーバーの設定をサポートします。 +モジュラーデザイン | カスタム高階関数プラグインと[関数プラグイン]、プラグイン[ホット更新]のサポートが可能です。詳細は[こちら](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97) +[自己プログラム解析](https://www.bilibili.com/video/BV1cj411A7VW) | [関数プラグイン][ワンクリック理解](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)このプロジェクトのソースコード +[プログラム解析機能](https://www.bilibili.com/video/BV1cj411A7VW) | [関数プラグイン] ワンクリックで別のPython/C/C++/Java/Lua/...プロジェクトツリーを解析できます。 +論文読解 | [関数プラグイン] LaTeX論文の全文をワンクリックで解読し、要約を生成します。 +LaTeX全文翻訳、整形 | [関数プラグイン] ワンクリックでLaTeX論文を翻訳または整形できます。 +注釈生成 | [関数プラグイン] ワンクリックで関数の注釈を大量に生成できます。 +チャット分析レポート生成 | [関数プラグイン] 実行後、まとめレポートを自動生成します。 +[arxivヘルパー](https://www.bilibili.com/video/BV1LM4y1279X) | [関数プラグイン] 入力したarxivの記事URLで要約をワンクリック翻訳+PDFダウンロードができます。 +[PDF論文全文翻訳機能](https://www.bilibili.com/video/BV1KT411x7Wn) | [関数プラグイン] PDF論文タイトルと要約を抽出し、全文を翻訳します(マルチスレッド)。 +[Google Scholar Integratorヘルパー](https://www.bilibili.com/video/BV19L411U7ia) | [関数プラグイン] 任意のGoogle Scholar検索ページURLを指定すると、gptが興味深い記事を選択します。 +数式/画像/テーブル表示 | 数式のTex形式とレンダリング形式を同時に表示できます。数式、コードのハイライトをサポートしています。 +マルチスレッド関数プラグインサポート | ChatGPTをマルチスレッドで呼び出すことができ、大量のテキストやプログラムを簡単に処理できます。 +ダークグラジオ[テーマ](https://github.com/binary-husky/chatgpt_academic/issues/173)の起動 | 「/?__dark-theme=true」というURLをブラウザに追加することで、ダークテーマに切り替えることができます。 +[多数のLLMモデル](https://www.bilibili.com/video/BV1wT411p7yf)をサポート、[API2D](https://api2d.com/)インターフェースをサポート | GPT3.5、GPT4、[清華ChatGLM](https://github.com/THUDM/ChatGLM-6B)による同時サポートは、とても素晴らしいですね! +huggingface免科学上网[オンライン版](https://huggingface.co/spaces/qingxu98/gpt-academic) | huggingfaceにログイン後、[このスペース](https://huggingface.co/spaces/qingxu98/gpt-academic)をコピーしてください。 +...... | ...... +
-- 新しいインターフェース(config.pyのLAYOUTオプションを変更すると、左右のレイアウトと上下のレイアウトを切り替えることができます)。 +- 新しいインターフェース(config.pyのLAYOUTオプションを変更するだけで、「左右レイアウト」と「上下レイアウト」を切り替えることができます)
-- All buttons are dynamically generated by reading functional.py, and custom functions can be added, freeing up the clipboard. +- すべてのボタンは、functional.pyを読み込んで動的に生成されます。カスタム機能を自由に追加して、クリップボードを解放します
-- Polishing/Error Correction +- 色を修正/修正
-- If the output contains formulas, it will be displayed in both tex form and rendering form at the same time for easy copying and reading. +- 出力に数式が含まれている場合、TeX形式とレンダリング形式の両方が表示され、コピーと読み取りが容易になります
-- Tired of looking at the project code? Just show chatgpt's mouth the whole project +- プロジェクトのコードを見るのが面倒?chatgptに整備されたプロジェクトを直接与えましょう
-- Various large language models are mixed and called (ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4) +- 多数の大規模言語モデルの混合呼び出し(ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4)
-Multiple large language models mixed call [huggingface beta](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta) (huggingface version does not support chatglm) +多数の大規模言語モデルの混合呼び出し[huggingfaceテスト版](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta)(huggigface版はchatglmをサポートしていません) --- -## Installation-Method 1: Run Directly (Windows, Linux or MacOS) +## インストール-方法1:直接運転 (Windows、LinuxまたはMacOS) -1. Download the project +1. プロジェクトをダウンロードします。 ```sh git clone https://github.com/binary-husky/chatgpt_academic.git cd chatgpt_academic ``` -2. Configure API_KEY and proxy settings +2. API_KEYとプロキシ設定を構成する -In `config.py`, configure the overseas proxy and OpenAI API KEY, as explained below. +`config.py`で、海外のProxyとOpenAI API KEYを構成して説明します。 ``` -1. If you are in China, you need to set up overseas proxies to use the OpenAI API smoothly. Please read config.py carefully for the setting method (1. Change USE_PROXY to True; 2. Modify the proxies according to the instructions). -2. Configure OpenAI API KEY. You need to register and obtain the API KEY on the OpenAI official website. Once you have the API KEY, configure it in the config.py file. -3. Issues related to proxy networks (network timeout, proxies not working) are summarized at https://github.com/binary-husky/chatgpt_academic/issues/1 +1.あなたが中国にいる場合、OpenAI APIをスムーズに使用するには海外プロキシを設定する必要があります。構成の詳細については、config.py(1.その中のUSE_PROXYをTrueに変更し、2.手順に従ってプロキシを変更する)を詳細に読んでください。 +2. OpenAI API KEYを構成する。OpenAIのウェブサイトでAPI KEYを取得してください。一旦API KEYを手に入れると、config.pyファイルで設定するだけです。 +3.プロキシネットワークに関連する問題(ネットワークタイムアウト、プロキシが動作しない)をhttps://github.com/binary-husky/chatgpt_academic/issues/1にまとめました。 ``` -(P.S. When the program is running, it will first check whether there is a private configuration file named `config_private.py` and use the configuration therein to override the same-named configuration in `config.py`. Therefore, if you can understand our configuration reading logic, we strongly recommend that you create a new configuration file named `config_private.py` next to `config.py` and transfer (copy) the configuration in `config.py` to `config_private.py`. `config_private.py` is not controlled by git, which can make your privacy information more secure.) +(P.S. プログラム実行時にconfig.pyの隣にconfig_private.pyという名前のプライバシー設定ファイルを作成し、同じ名前の設定を上書きするconfig_private.pyが存在するかどうかを優先的に確認します。そのため、私たちの構成読み取りロジックを理解できる場合は、config.pyの隣にconfig_private.pyという名前の新しい設定ファイルを作成し、その中のconfig.pyから設定を移動してください。config_private.pyはgitで保守されていないため、プライバシー情報をより安全にすることができます。) -3. Install dependencies +3. 依存関係をインストールします。 ```sh -# (Option 1) Recommended -python -m pip install -r requirements.txt +# 選択肢があります。 +python -m pip install -r requirements.txt -# (Option 2) If you use anaconda, the steps are similar: -# (Option 2.1) conda create -n gptac_venv python=3.11 -# (Option 2.2) conda activate gptac_venv -# (Option 2.3) python -m pip install -r requirements.txt -# Note: Use the official pip source or Ali pip source. Other pip sources (such as some universities' pip) may have problems. Temporary switching source method: +# (選択肢2) もしAnacondaを使用する場合、手順は同様です: +# (選択肢2.1) conda create -n gptac_venv python=3.11 +# (選択肢2.2) conda activate gptac_venv +# (選択肢2.3) python -m pip install -r requirements.txt + +# 注: 公式のpipソースまたはAlibabaのpipソースを使用してください。 別のpipソース(例:一部の大学のpip)は問題が発生する可能性があります。 一時的なソースの切り替え方法: # python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ``` -If you need to support Tsinghua ChatGLM, you need to install more dependencies separately (if you are not familiar with Python or your computer configuration is not good, it is not recommended to try): +もしあなたが清華ChatGLMをサポートする必要がある場合、さらに多くの依存関係をインストールする必要があります(Pythonに慣れない方やコンピューターの設定が十分でない方は、試みないことをお勧めします): ```sh python -m pip install -r request_llm/requirements_chatglm.txt ``` -4. Run +4. 実行 ```sh python main.py ``` -5. Test function plugin +5. 関数プラグインのテスト ``` -- Test Python project analysis - In the input area, enter `./crazy_functions/test_project/python/dqn`, and then click "Analyze the Entire Python Project" -- Test self-code interpretation - Click "[Multi-threaded Demo] Analyze this Project Itself (Self-Translating Source Code)" -- Test experimental functional template function (requires gpt to answer what happened in history today), you can use this function as a template to implement more complex functions - Click "[Function Plugin Template Demo] In History Today" -- There are more features to choose from in the function plugin dropdown menu +- Pythonプロジェクト分析のテスト + 入力欄に `./crazy_functions/test_project/python/dqn` と入力し、「Pythonプロジェクト全体の解析」をクリックします。 +- 自己コード解読のテスト + 「[マルチスレッドデモ] このプロジェクト自体を解析します(ソースを翻訳して解読します)」をクリックします。 +- 実験的な機能テンプレート関数のテスト(GPTが「今日の歴史」に何が起こったかを回答することが求められます)。この関数をテンプレートとして使用して、より複雑な機能を実装できます。 + 「[関数プラグインテンプレートデモ] 今日の歴史」をクリックします。 +- 関数プラグインエリアのドロップダウンメニューには他にも選択肢があります。 ``` -## Installation-Method 2: Using Docker (Linux) +## インストール方法2:Dockerを使用する(Linux) - -あなたは、プロの学術論文翻訳者です。 - -1. ChatGPTのみを使用(一般的にはこのオプションが推奨されます) +1. ChatGPTのみ(大多数の人にお勧めです) ``` sh -# プロジェクトをダウンロードします +# プロジェクトのダウンロード git clone https://github.com/binary-husky/chatgpt_academic.git cd chatgpt_academic -# 海外ProxyとOpenAI API KEYを構成する -config.pyをテキストエディタで開いて編集します。 +# 海外プロキシとOpenAI API KEYの設定 +config.pyを任意のテキストエディタで編集する # インストール docker build -t gpt-academic . # 実行 docker run --rm -it --net=host gpt-academic # 関数プラグインのテスト -## 関数プラグインテンプレート関数をテストします(gptが「今日の歴史で何が起こったか」と答えるように要求します)。この関数をテンプレートとして使用して、より複雑な機能を実装できます。 -[関数プラグインテンプレートDemo]をクリックしてください。 -## LaTexプロジェクトの要約を書くテスト -入力エリアに./crazy_functions/test_project/latex/attentionを入力し、「LaTex論文を読んで要約を書く」をクリックします。 -## Pythonプロジェクトの解析をテストする -入力エリアに./crazy_functions/test_project/python/dqnを入力して、「Pythonプロジェクトを解析する」をクリックします。 +## 関数プラグインテンプレート関数のテスト(GPTが「今日の歴史」に何が起こったかを回答することが求められます)。この関数をテンプレートとして使用して、より複雑な機能を実装できます。 +「[関数プラグインテンプレートデモ] 今日の歴史」をクリックします。 +## Latexプロジェクトの要約を書くテスト +入力欄に./crazy_functions/test_project/latex/attentionと入力し、「テックス論文を読んで要約を書く」をクリックします。 +## Pythonプロジェクト分析のテスト +入力欄に./crazy_functions/test_project/python/dqnと入力し、[Pythonプロジェクトの全解析]をクリックします。 -より多くの機能が関数プラグインエリアのドロップダウンメニューで利用可能です。 +関数プラグインエリアのドロップダウンメニューには他にも選択肢があります。 ``` -2. ChatGPT+ChatGLMを使用する(Dockerに非常に詳しい必要があります + コンピュータの構成が十分に強力な必要があります) +2. ChatGPT + ChatGLM(Dockerに非常に詳しい人+十分なコンピューター設定が必要) -``` sh -# dockerfileを変更する + + +```sh +# Dockerfileの編集 cd docs && nano Dockerfile+ChatGLM -# How to build | 構築方法(Dockerfile+ChatGLMはdocsのパスにありますので、まずcd docsを実行してください) +# ビルド方法 docker build -t gpt-academic --network=host -f Dockerfile+ChatGLM . -# How to run | 実行方法 (1) 直接実行: +# 実行方法 (1) 直接実行: docker run --rm -it --net=host --gpus=all gpt-academic -# How to run | 実行方法 (2) コンテナに入って調整することを予め行いたい場合: +# 実行方法 (2) コンテナに入って調整する: docker run --rm -it --net=host --gpus=all gpt-academic bash ``` +## インストール方法3:その他のデプロイ方法 -## Installation-Method 3: その他のデプロイ方法 +1. クラウドサーバーデプロイ +[デプロイwiki-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) -1. クラウドサーバーにリモートでインストールする -[デプロイwiki-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97) - -2. WSL2を使用する(Windows Subsystem for Linuxサブシステム) -[デプロイwiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) +2. WSL2を使用 (Windows Subsystem for Linux) +[デプロイwiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2) -## Installation-プロキシ構成 -### 方法1: 標準的な方法 -[プロキシの構成](https://github.com/binary-husky/chatgpt_academic/issues/1) +## インストール-プロキシ設定 +1. 通常の方法 +[プロキシを設定する](https://github.com/binary-husky/chatgpt_academic/issues/1) -### 方法2:初心者向けのチュートリアル -[初心者向けチュートリアル](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) +2. 初心者向けチュートリアル +[初心者向けチュートリアル](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BB%A3%E7%90%86%E8%BD%AF%E4%BB%B6%E9%97%AE%E9%A2%98%E7%9A%84%E6%96%B0%E6%89%8B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%EF%BC%88%E6%96%B9%E6%B3%95%E5%8F%AA%E9%80%82%E7%94%A8%E4%BA%8E%E6%96%B0%E6%89%8B%EF%BC%89) --- -## カスタムコンビニエンスボタンの作成方法(学術用ショートカットキーをカスタマイズする) -任意のテキストエディタで`core_functional.py`を開き、以下のエントリを追加し、プログラムを再起動します。 (ボタンが正常に追加されて表示されている場合、プレフィックスとサフィックスは両方ともリアルタイムで変更できるため、プログラムを再起動する必要はありません。) -例えば +## カスタムボタンの追加(学術ショートカットキー) + +`core_functional.py`を任意のテキストエディタで開き、以下のエントリーを追加し、プログラムを再起動してください。(ボタンが追加されて表示される場合、前置詞と後置詞はホット編集がサポートされているため、プログラムを再起動せずに即座に有効になります。) + +例: ``` "超级英译中": { - # プレフィックス、あなたの要求を説明するために使用されます。翻訳、コードの解析、精緻化など。 - "Prefix": "以下の内容を中国語に翻訳し、専門用語が含まれる場合はマークダウン表で1つずつ説明してください:\n\n", + # 前置詞 - あなたの要求を説明するために使用されます。翻訳、コードの説明、編集など。 + "Prefix": "以下のコンテンツを中国語に翻訳して、マークダウンテーブルを使用して専門用語を説明してください。\n\n", - #サフィックス、プレフィックスと組み合わせて、入力内容を引用符で囲むことができます。 + # 後置詞 - プレフィックスと共に使用すると、入力内容を引用符で囲むことができます。 "Suffix": "", }, ``` +
+ --- - -## 一部の機能のデモ +## いくつかの機能の例 ### 画像表示: @@ -221,7 +224,7 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash -### プログラムが自分自身を読み取り、分解できる場合: +### プログラムが自己解析できる場合:
@@ -231,8 +234,8 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash
+### 他のPython/Cppプロジェクトの解析: -### 他のPython/Cppプロジェクトの分析:
@@ -241,54 +244,59 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash -### LaTeX論文の読解と要約の自動生成 +### Latex論文の一括読解と要約生成 +
-### 自動レポート生成 +### 自動報告生成 +
-### モジュール化された機能設計 +### モジュール化された機能デザイン +
-### ソースコードを英語に変換する +### ソースコードの英語翻訳
-## タスクとバージョン計画: -- version 3.2+ (todo): 関数プラグインがより多くのパラメーターインターフェースをサポート -- version 3.1: 複数のgptモデルを同時に問い合わせるサポート! api2dをサポートし、複数のapikeyの負荷分散をサポート -- version 3.0: chatglmおよびその他の小規模llmのサポート -- version 2.6: プラグイン構造を再構築し、対話性を強化し、さらに多くのプラグインを追加 -- version 2.5: 自動更新、長い文章、トークンのオーバーフローを解決する -- version 2.4: (1) PDF全文翻訳機能を追加。 (2)位置の切り替え機能を追加。 (3)垂直レイアウトオプションを追加。 (4)マルチスレッド関数プラグインの最適化。 -- version 2.3: マルチスレッドの相互作用の強化 -- version 2.2: 関数プラグインのホットリロードをサポート +## Todo およびバージョン計画: +- version 3.2+ (todo): 関数プラグインがより多くのパラメーターインターフェースをサポートするようになります。 +- version 3.1: 複数のgptモデルを同時にクエリし、api2dをサポートし、複数のapikeyの負荷分散をサポートします。 +- version 3.0: chatglmおよび他の小型llmのサポート +- version 2.6: プラグイン構造を再構成し、相互作用性を高め、より多くのプラグインを追加しました。 +- version 2.5: 自己更新。総括的な大規模プロジェクトのソースコードをまとめた場合、テキストが長すぎる、トークンがオーバーフローする問題を解決します。 +- version 2.4: (1)PDF全文翻訳機能を追加。(2)入力エリアの位置を切り替える機能を追加。(3)垂直レイアウトオプションを追加。(4)マルチスレッド関数プラグインの最適化。 +- version 2.3: 多スレッドの相互作用性を向上させました。 +- version 2.2: 関数プラグインでホットリロードをサポート - version 2.1: 折りたたみ式レイアウト - version 2.0: モジュール化された関数プラグインを導入 - version 1.0: 基本機能 -## 参考と学習 +## 参考および学習 + + +以下は中国語のマークダウンファイルです。日本語に翻訳してください。既存のマークダウンコマンドを変更しないでください: ``` -多くの優れたプロジェクトの設計を参考にしています: +多くの優秀なプロジェクトの設計を参考にしています。主なものは以下の通りです: -# 参考プロジェクト1:ChuanhuChatGPTから多くの技術を借りました +# 参考プロジェクト1:ChuanhuChatGPTから多くのテクニックを借用 https://github.com/GaiZhenbiao/ChuanhuChatGPT # 参考プロジェクト2:清華ChatGLM-6B: https://github.com/THUDM/ChatGLM-6B ``` - From 74e3cd4c6f66c21fb3c8f1965962e6fc6f9d0ed8 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Wed, 19 Apr 2023 17:34:45 +0800 Subject: [PATCH 49/50] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9fefdcb..d0daf1a 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,8 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash - version 2.0: 引入模块化函数插件 - version 1.0: 基础功能 +chatgpt_academic开发者QQ群:734063350 + ## 参考与学习 ``` From 97385c98fc998fe0762f11fab55c7b14b57ce767 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Wed, 19 Apr 2023 18:14:13 +0800 Subject: [PATCH 50/50] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d0daf1a..2af81ce 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ cd chatgpt_academic ``` 1. 如果你在国内,需要设置海外代理才能够顺利使用 OpenAI API,设置方法请仔细阅读config.py(1.修改其中的USE_PROXY为True; 2.按照说明修改其中的proxies)。 2. 配置 OpenAI API KEY。你需要在 OpenAI 官网上注册并获取 API KEY。一旦你拿到了 API KEY,在 config.py 文件里配置好即可。 +3. 支持任意数量的OpenAI的密钥和API2D的密钥共存/负载均衡,多个KEY用英文逗号分隔即可,例如输入 API_KEY="OpenAI密钥1,API2D密钥2,OpenAI密钥3,OpenAI密钥4" 3. 与代理网络有关的issue(网络超时、代理不起作用)汇总到 https://github.com/binary-husky/chatgpt_academic/issues/1 ``` (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。)