From 2b96217f2be3fe3f8a96c43adc797d8075a384cb Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sat, 22 Apr 2023 21:18:35 +0800 Subject: [PATCH 01/66] =?UTF-8?q?=E5=AE=9E=E7=8E=B0Newbing=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 7 +- request_llm/bridge_all.py | 13 +- request_llm/bridge_newbing.py | 608 +++++++++++++++++++++++++++ request_llm/requirements_newbing.txt | 8 + 4 files changed, 633 insertions(+), 3 deletions(-) create mode 100644 request_llm/bridge_newbing.py create mode 100644 request_llm/requirements_newbing.txt diff --git a/config.py b/config.py index b2727da..4c4495a 100644 --- a/config.py +++ b/config.py @@ -44,8 +44,8 @@ WEB_PORT = -1 MAX_RETRY = 2 # 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"] +LLM_MODEL = "newbing" # 可选 ↓↓↓ +AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm", "newbing"] # 本地LLM模型如ChatGLM的执行方式 CPU/GPU LOCAL_MODEL_DEVICE = "cpu" # 可选 "cuda" @@ -63,3 +63,6 @@ API_URL_REDIRECT = {} # 如果需要在二级路径下运行(常规情况下,不要修改!!)(需要配合修改main.py才能生效!) CUSTOM_PATH = "/" + + +newbing_cookies = """your bing cookies here""" \ No newline at end of file diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 311dc6f..15116b9 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -19,6 +19,9 @@ 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_newbing import predict_no_ui_long_connection as newbing_noui +from .bridge_newbing import predict as newbing_ui + # from .bridge_tgui import predict_no_ui_long_connection as tgui_noui # from .bridge_tgui import predict as tgui_ui @@ -116,7 +119,15 @@ model_info = { "tokenizer": tokenizer_gpt35, "token_cnt": get_token_num_gpt35, }, - + # newbing + "newbing": { + "fn_with_ui": newbing_ui, + "fn_without_ui": newbing_noui, + "endpoint": None, + "max_token": 4096, + "tokenizer": tokenizer_gpt35, + "token_cnt": get_token_num_gpt35, + }, } diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py new file mode 100644 index 0000000..4dfcd55 --- /dev/null +++ b/request_llm/bridge_newbing.py @@ -0,0 +1,608 @@ +""" +Main.py +""" + +from transformers import AutoModel, AutoTokenizer +import time +import importlib +from toolbox import update_ui, get_conf +from multiprocessing import Process, Pipe +import argparse +import asyncio +import json +import os +import random +import re +import ssl +import sys +import uuid +from enum import Enum +from pathlib import Path +from typing import Generator +from typing import Literal +from typing import Optional +from typing import Union +import certifi +import httpx +import websockets.client as websockets +from prompt_toolkit import PromptSession +from prompt_toolkit.auto_suggest import AutoSuggestFromHistory +from prompt_toolkit.completion import WordCompleter +from prompt_toolkit.history import InMemoryHistory +from prompt_toolkit.key_binding import KeyBindings +from rich.live import Live +from rich.markdown import Markdown + +DELIMITER = "\x1e" + + +# Generate random IP between range 13.104.0.0/14 +FORWARDED_IP = ( + f"13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}" +) + +HEADERS = { + "accept": "application/json", + "accept-language": "en-US,en;q=0.9", + "content-type": "application/json", + "sec-ch-ua": '"Not_A Brand";v="99", "Microsoft Edge";v="110", "Chromium";v="110"', + "sec-ch-ua-arch": '"x86"', + "sec-ch-ua-bitness": '"64"', + "sec-ch-ua-full-version": '"109.0.1518.78"', + "sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-model": "", + "sec-ch-ua-platform": '"Windows"', + "sec-ch-ua-platform-version": '"15.0.0"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-origin", + "x-ms-client-request-id": str(uuid.uuid4()), + "x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32", + "Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx", + "Referrer-Policy": "origin-when-cross-origin", + "x-forwarded-for": FORWARDED_IP, +} + +HEADERS_INIT_CONVER = { + "authority": "edgeservices.bing.com", + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", + "accept-language": "en-US,en;q=0.9", + "cache-control": "max-age=0", + "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"', + "sec-ch-ua-arch": '"x86"', + "sec-ch-ua-bitness": '"64"', + "sec-ch-ua-full-version": '"110.0.1587.69"', + "sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-model": '""', + "sec-ch-ua-platform": '"Windows"', + "sec-ch-ua-platform-version": '"15.0.0"', + "sec-fetch-dest": "document", + "sec-fetch-mode": "navigate", + "sec-fetch-site": "none", + "sec-fetch-user": "?1", + "upgrade-insecure-requests": "1", + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69", + "x-edge-shopping-flag": "1", + "x-forwarded-for": FORWARDED_IP, +} + +ssl_context = ssl.create_default_context() +ssl_context.load_verify_locations(certifi.where()) + + +class NotAllowedToAccess(Exception): + pass + + +class ConversationStyle(Enum): + creative = "h3imaginative,clgalileo,gencontentv3" + balanced = "galileo" + precise = "h3precise,clgalileo" + + +CONVERSATION_STYLE_TYPE = Optional[ + Union[ConversationStyle, Literal["creative", "balanced", "precise"]] +] + + +def _append_identifier(msg: dict) -> str: + """ + Appends special character to end of message to identify end of message + """ + # Convert dict to json string + return json.dumps(msg) + DELIMITER + + +def _get_ran_hex(length: int = 32) -> str: + """ + Returns random hex string + """ + return "".join(random.choice("0123456789abcdef") for _ in range(length)) + + +class _ChatHubRequest: + """ + Request object for ChatHub + """ + + def __init__( + self, + conversation_signature: str, + client_id: str, + conversation_id: str, + invocation_id: int = 0, + ) -> None: + self.struct: dict = {} + + self.client_id: str = client_id + self.conversation_id: str = conversation_id + self.conversation_signature: str = conversation_signature + self.invocation_id: int = invocation_id + + def update( + self, + prompt: str, + conversation_style: CONVERSATION_STYLE_TYPE, + options: list | None = None, + ) -> None: + """ + Updates request object + """ + if options is None: + options = [ + "deepleo", + "enable_debug_commands", + "disable_emoji_spoken_text", + "enablemm", + ] + if conversation_style: + if not isinstance(conversation_style, ConversationStyle): + conversation_style = getattr(ConversationStyle, conversation_style) + options = [ + "nlu_direct_response_filter", + "deepleo", + "disable_emoji_spoken_text", + "responsible_ai_policy_235", + "enablemm", + conversation_style.value, + "dtappid", + "cricinfo", + "cricinfov2", + "dv3sugg", + ] + self.struct = { + "arguments": [ + { + "source": "cib", + "optionsSets": options, + "sliceIds": [ + "222dtappid", + "225cricinfo", + "224locals0", + ], + "traceId": _get_ran_hex(32), + "isStartOfSession": self.invocation_id == 0, + "message": { + "author": "user", + "inputMethod": "Keyboard", + "text": prompt, + "messageType": "Chat", + }, + "conversationSignature": self.conversation_signature, + "participant": { + "id": self.client_id, + }, + "conversationId": self.conversation_id, + }, + ], + "invocationId": str(self.invocation_id), + "target": "chat", + "type": 4, + } + self.invocation_id += 1 + + +class _Conversation: + """ + Conversation API + """ + + def __init__( + self, + cookies: dict, + proxy: str | None = None, + ) -> None: + self.struct: dict = { + "conversationId": None, + "clientId": None, + "conversationSignature": None, + "result": {"value": "Success", "message": None}, + } + self.proxy = proxy + proxy = ( + proxy + or os.environ.get("all_proxy") + or os.environ.get("ALL_PROXY") + or os.environ.get("https_proxy") + or os.environ.get("HTTPS_PROXY") + or None + ) + if proxy is not None and proxy.startswith("socks5h://"): + proxy = "socks5://" + proxy[len("socks5h://") :] + self.session = httpx.Client( + proxies=proxy, + timeout=30, + headers=HEADERS_INIT_CONVER, + ) + for cookie in cookies: + self.session.cookies.set(cookie["name"], cookie["value"]) + + # Send GET request + response = self.session.get( + url=os.environ.get("BING_PROXY_URL") + or "https://edgeservices.bing.com/edgesvc/turing/conversation/create", + ) + if response.status_code != 200: + response = self.session.get( + "https://edge.churchless.tech/edgesvc/turing/conversation/create", + ) + if response.status_code != 200: + print(f"Status code: {response.status_code}") + print(response.text) + print(response.url) + raise Exception("Authentication failed") + try: + self.struct = response.json() + except (json.decoder.JSONDecodeError, NotAllowedToAccess) as exc: + raise Exception( + "Authentication failed. You have not been accepted into the beta.", + ) from exc + if self.struct["result"]["value"] == "UnauthorizedRequest": + raise NotAllowedToAccess(self.struct["result"]["message"]) + + +class _ChatHub: + """ + Chat API + """ + + def __init__(self, conversation: _Conversation) -> None: + self.wss: websockets.WebSocketClientProtocol | None = None + self.request: _ChatHubRequest + self.loop: bool + self.task: asyncio.Task + self.request = _ChatHubRequest( + conversation_signature=conversation.struct["conversationSignature"], + client_id=conversation.struct["clientId"], + conversation_id=conversation.struct["conversationId"], + ) + + async def ask_stream( + self, + prompt: str, + wss_link: str, + conversation_style: CONVERSATION_STYLE_TYPE = None, + raw: bool = False, + options: dict = None, + ) -> Generator[str, None, None]: + """ + Ask a question to the bot + """ + if self.wss and not self.wss.closed: + await self.wss.close() + # Check if websocket is closed + self.wss = await websockets.connect( + wss_link, + extra_headers=HEADERS, + max_size=None, + ssl=ssl_context, + ) + await self._initial_handshake() + # Construct a ChatHub request + self.request.update( + prompt=prompt, + conversation_style=conversation_style, + options=options, + ) + # Send request + await self.wss.send(_append_identifier(self.request.struct)) + final = False + while not final: + objects = str(await self.wss.recv()).split(DELIMITER) + for obj in objects: + if obj is None or not obj: + continue + response = json.loads(obj) + if response.get("type") != 2 and raw: + yield False, response + elif response.get("type") == 1 and response["arguments"][0].get( + "messages", + ): + resp_txt = response["arguments"][0]["messages"][0]["adaptiveCards"][ + 0 + ]["body"][0].get("text") + yield False, resp_txt + elif response.get("type") == 2: + final = True + yield True, response + + async def _initial_handshake(self) -> None: + await self.wss.send(_append_identifier({"protocol": "json", "version": 1})) + await self.wss.recv() + + async def close(self) -> None: + """ + Close the connection + """ + if self.wss and not self.wss.closed: + await self.wss.close() + + +class Chatbot: + """ + Combines everything to make it seamless + """ + + def __init__( + self, + cookies: dict = None, + proxy: str | None = None, + cookie_path: str = None, + ) -> None: + if cookies is None: + cookies = {} + if cookie_path is not None: + try: + with open(cookie_path, encoding="utf-8") as f: + self.cookies = json.load(f) + except FileNotFoundError as exc: + raise FileNotFoundError("Cookie file not found") from exc + else: + self.cookies = cookies + self.proxy: str | None = proxy + self.chat_hub: _ChatHub = _ChatHub( + _Conversation(self.cookies, self.proxy), + ) + + async def ask( + self, + prompt: str, + wss_link: str = "wss://sydney.bing.com/sydney/ChatHub", + conversation_style: CONVERSATION_STYLE_TYPE = None, + options: dict = None, + ) -> dict: + """ + Ask a question to the bot + """ + async for final, response in self.chat_hub.ask_stream( + prompt=prompt, + conversation_style=conversation_style, + wss_link=wss_link, + options=options, + ): + if final: + return response + await self.chat_hub.wss.close() + return None + + async def ask_stream( + self, + prompt: str, + wss_link: str = "wss://sydney.bing.com/sydney/ChatHub", + conversation_style: CONVERSATION_STYLE_TYPE = None, + raw: bool = False, + options: dict = None, + ) -> Generator[str, None, None]: + """ + Ask a question to the bot + """ + async for response in self.chat_hub.ask_stream( + prompt=prompt, + conversation_style=conversation_style, + wss_link=wss_link, + raw=raw, + options=options, + ): + yield response + + async def close(self) -> None: + """ + Close the connection + """ + await self.chat_hub.close() + + async def reset(self) -> None: + """ + Reset the conversation + """ + await self.close() + self.chat_hub = _ChatHub(_Conversation(self.cookies)) + + +async def _get_input_async( + session: PromptSession = None, + completer: WordCompleter = None, +) -> str: + """ + Multiline input function. + """ + return await session.prompt_async( + completer=completer, + multiline=True, + auto_suggest=AutoSuggestFromHistory(), + ) + + +def _create_session() -> PromptSession: + kb = KeyBindings() + + @kb.add("enter") + def _(event): + buffer_text = event.current_buffer.text + if buffer_text.startswith("!"): + event.current_buffer.validate_and_handle() + else: + event.current_buffer.insert_text("\n") + + @kb.add("escape") + def _(event): + if event.current_buffer.complete_state: + # event.current_buffer.cancel_completion() + event.current_buffer.text = "" + + return PromptSession(key_bindings=kb, history=InMemoryHistory()) + + +def _create_completer(commands: list, pattern_str: str = "$"): + return WordCompleter(words=commands, pattern=re.compile(pattern_str)) + + +load_message = "" + +################################################################################# +################################################################################# +################################################################################# +################################################################################# +################################################################################# +################################################################################# +class GetNewBingHandle(Process): + def __init__(self): + super().__init__(daemon=True) + self.parent, self.child = Pipe() + self.chatglm_model = None + self.chatglm_tokenizer = None + self.info = "" + self.success = True + self.check_dependency() + self.start() + + def check_dependency(self): + try: + import rich + self.info = "依赖检测通过" + self.success = True + except: + self.info = "缺少的依赖,如果要使用Newbing,除了基础的pip依赖以外,您还需要运行`pip install -r request_llm/requirements_newbing.txt`安装Newbing的依赖。" + self.success = False + + def ready(self): + return self.chatglm_model is not None + + async def async_run(self, question): + async for final, response in self.chatglm_model.ask_stream( + prompt=question, + conversation_style="balanced", # ["creative", "balanced", "precise"] + wss_link="wss://sydney.bing.com/sydney/ChatHub", # "wss://sydney.bing.com/sydney/ChatHub" + ): + if not final: + self.child.send(response) + print(response) + + def run(self): + # 第一次运行,加载参数 + retry = 0 + while True: + try: + if self.chatglm_model is None: + proxies, = get_conf('proxies') + newbing_cookies, = get_conf('newbing_cookies') + cookies = json.loads(newbing_cookies) + self.chatglm_model = Chatbot(proxy=proxies['https'], cookies=cookies) + break + else: + break + except: + retry += 1 + if retry > 3: + self.child.send('[Local Message] 不能加载Newbing组件。') + raise RuntimeError("不能加载Newbing组件。") + + # 进入任务等待状态 + while True: + kwargs = self.child.recv() + try: + asyncio.run(self.async_run(question=kwargs['query'])) + except: + self.child.send('[Local Message] Newbing失败.') + 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 glm_handle + if glm_handle is None: + glm_handle = GetNewBingHandle() + 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 = [] + history_feedin.append(["What can I do?", sys_prompt]) + for i in range(len(history)//2): + history_feedin.append([history[2*i], history[2*i+1]] ) + + watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 + response = "" + 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 + if len(observe_window) >= 2: + if (time.time()-observe_window[1]) > watch_dog_patience: + raise RuntimeError("程序终止。") + return response + + + +def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): + """ + 单线程方法 + 函数的说明请见 request_llm/bridge_all.py + """ + chatbot.append((inputs, "")) + + global glm_handle + if glm_handle is None: + glm_handle = GetNewBingHandle() + 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 + 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 = [] + history_feedin.append(["What can I do?", system_prompt] ) + for i in range(len(history)//2): + history_feedin.append([history[2*i], history[2*i+1]] ) + + 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) + + history.extend([inputs, response]) + yield from update_ui(chatbot=chatbot, history=history) \ No newline at end of file diff --git a/request_llm/requirements_newbing.txt b/request_llm/requirements_newbing.txt new file mode 100644 index 0000000..73455f4 --- /dev/null +++ b/request_llm/requirements_newbing.txt @@ -0,0 +1,8 @@ +BingImageCreator +certifi +httpx +prompt_toolkit +requests +rich +websockets +httpx[socks] From 676fe40d39883b6cae70234652ec84c3db1b31e3 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 17:32:44 +0800 Subject: [PATCH 02/66] =?UTF-8?q?=E4=BC=98=E5=8C=96chatgpt=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E7=9A=84=E6=88=AA=E6=96=AD=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/谷歌检索小助手.py | 3 +- request_llm/bridge_chatgpt.py | 17 +++++++----- toolbox.py | 46 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/crazy_functions/谷歌检索小助手.py b/crazy_functions/谷歌检索小助手.py index 94ef256..786b266 100644 --- a/crazy_functions/谷歌检索小助手.py +++ b/crazy_functions/谷歌检索小助手.py @@ -98,7 +98,8 @@ def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst history.extend([ "第一批", gpt_say ]) meta_paper_info_list = meta_paper_info_list[10:] - chatbot.append(["状态?", "已经全部完成"]) + chatbot.append(["状态?", + "已经全部完成,您可以试试让AI写一个Related Works,例如您可以继续输入Write a \"Related Works\" section about \"你搜索的研究领域\" for me."]) msg = '正常' yield from update_ui(chatbot=chatbot, history=history, msg=msg) # 刷新界面 res = write_results_to_file(history) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index c1a900b..a1614b7 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -21,7 +21,7 @@ import importlib # config_private.py放自己的秘密如API和代理网址 # 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件 -from toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys +from toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys, clip_history proxies, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \ get_conf('proxies', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY') @@ -145,7 +145,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp yield from update_ui(chatbot=chatbot, history=history, msg="api-key不满足要求") # 刷新界面 return - history.append(inputs); history.append(" ") + history.append(inputs); history.append("") retry = 0 while True: @@ -198,14 +198,17 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp 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 = [] # 清除历史 + if len(history) >= 2: history[-1] = ""; history[-2] = "" # 清除当前溢出的输入:history[-2] 是本次输入, history[-1] 是本次输出 + history = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'], + max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])//2) # history至少释放二分之一 + 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. 模型不存在,或者您没有获得体验资格.") + 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为由,拒绝服务.") + 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以账户额度不足为由,拒绝服务.") + 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账户额度不足.") elif "Not enough point" in error_msg: diff --git a/toolbox.py b/toolbox.py index 6625c43..4340814 100644 --- a/toolbox.py +++ b/toolbox.py @@ -551,3 +551,49 @@ def run_gradio_in_subpath(demo, auth, port, custom_path): return {"message": f"Gradio is running at: {custom_path}"} app = gr.mount_gradio_app(app, demo, path=custom_path) uvicorn.run(app, host="0.0.0.0", port=port) # , auth=auth + + +def clip_history(inputs, history, tokenizer, max_token_limit): + """ + reduce the length of input/history by clipping. + this function search for the longest entries to clip, little by little, + until the number of token of input/history is reduced under threshold. + 通过剪辑来缩短输入/历史记录的长度。 + 此函数逐渐地搜索最长的条目进行剪辑, + 直到输入/历史记录的标记数量降低到阈值以下。 + """ + import numpy as np + from request_llm.bridge_all import model_info + def get_token_num(txt): + return len(tokenizer.encode(txt, disallowed_special=())) + input_token_num = get_token_num(inputs) + if input_token_num < max_token_limit * 3 / 4: + # 当输入部分的token占比小于限制的3/4时,在裁剪时把input的余量留出来 + max_token_limit = max_token_limit - input_token_num + if max_token_limit < 128: + # 余量太小了,直接清除历史 + history = [] + return history + else: + # 当输入部分的token占比 > 限制的3/4时,直接清除历史 + history = [] + return history + + everything = [''] + everything.extend(history) + n_token = get_token_num('\n'.join(everything)) + everything_token = [get_token_num(e) for e in everything] + + # 截断时的颗粒度 + delta = max(everything_token) // 16 + + while n_token > max_token_limit: + where = np.argmax(everything_token) + encoded = tokenizer.encode(everything[where], disallowed_special=()) + clipped_encoded = encoded[:len(encoded)-delta] + everything[where] = tokenizer.decode(clipped_encoded)[:-1] # -1 to remove the may-be illegal char + everything_token[where] = get_token_num(everything[where]) + n_token = get_token_num('\n'.join(everything)) + + history = everything[1:] + return history From 0785ff2aedabbd1f4a4aee78b6b7c070ed30a069 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 17:45:56 +0800 Subject: [PATCH 03/66] =?UTF-8?q?=E5=BE=AE=E8=B0=83=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E8=A3=81=E5=89=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatgpt.py | 2 +- toolbox.py | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index a1614b7..5e32f45 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -200,7 +200,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp if "reduce the length" in error_msg: if len(history) >= 2: history[-1] = ""; history[-2] = "" # 清除当前溢出的输入:history[-2] 是本次输入, history[-1] 是本次输出 history = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'], - max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])//2) # history至少释放二分之一 + max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])) # history至少释放二分之一 chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长, 或历史数据过长. 历史缓存数据已部分释放, 您可以请再次尝试. (若再次失败则更可能是因为输入过长.)") # history = [] # 清除历史 elif "does not exist" in error_msg: diff --git a/toolbox.py b/toolbox.py index 4340814..c9dc207 100644 --- a/toolbox.py +++ b/toolbox.py @@ -555,23 +555,26 @@ def run_gradio_in_subpath(demo, auth, port, custom_path): def clip_history(inputs, history, tokenizer, max_token_limit): """ - reduce the length of input/history by clipping. + reduce the length of history by clipping. this function search for the longest entries to clip, little by little, - until the number of token of input/history is reduced under threshold. - 通过剪辑来缩短输入/历史记录的长度。 + until the number of token of history is reduced under threshold. + 通过裁剪来缩短历史记录的长度。 此函数逐渐地搜索最长的条目进行剪辑, - 直到输入/历史记录的标记数量降低到阈值以下。 + 直到历史记录的标记数量降低到阈值以下。 """ import numpy as np from request_llm.bridge_all import model_info def get_token_num(txt): return len(tokenizer.encode(txt, disallowed_special=())) input_token_num = get_token_num(inputs) - if input_token_num < max_token_limit * 3 / 4: - # 当输入部分的token占比小于限制的3/4时,在裁剪时把input的余量留出来 + if input_token_num < max_token_limit * 3 / 4: + # 当输入部分的token占比小于限制的3/4时,裁剪时 + # 1. 把input的余量留出来 max_token_limit = max_token_limit - input_token_num + # 2. 把输出用的余量留出来 + max_token_limit = max_token_limit - 128 + # 3. 如果余量太小了,直接清除历史 if max_token_limit < 128: - # 余量太小了,直接清除历史 history = [] return history else: From ab61418410db5bb7d12f21f05a70d200197e64a7 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 18:13:30 +0800 Subject: [PATCH 04/66] better traceback --- crazy_functions/crazy_utils.py | 11 +++++------ request_llm/bridge_all.py | 7 ++----- request_llm/bridge_chatgpt.py | 4 ++-- toolbox.py | 32 +++++++++++++++++++++++++++----- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 4e0eba4..3412aa3 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -1,5 +1,4 @@ -import traceback -from toolbox import update_ui, get_conf +from toolbox import update_ui, get_conf, trimmed_format_exc def input_clipping(inputs, history, max_token_limit): import numpy as np @@ -94,12 +93,12 @@ def request_gpt_model_in_new_thread_with_ui_alive( continue # 返回重试 else: # 【选择放弃】 - tb_str = '```\n' + traceback.format_exc() + '```' + tb_str = '```\n' + trimmed_format_exc() + '```' mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n" return mutable[0] # 放弃 except: # 【第三种情况】:其他错误:重试几次 - tb_str = '```\n' + traceback.format_exc() + '```' + tb_str = '```\n' + trimmed_format_exc() + '```' print(tb_str) mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n" if retry_op > 0: @@ -220,14 +219,14 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( continue # 返回重试 else: # 【选择放弃】 - tb_str = '```\n' + traceback.format_exc() + '```' + tb_str = '```\n' + trimmed_format_exc() + '```' gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n" if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0] mutable[index][2] = "输入过长已放弃" return gpt_say # 放弃 except: # 【第三种情况】:其他错误 - tb_str = '```\n' + traceback.format_exc() + '```' + tb_str = '```\n' + trimmed_format_exc() + '```' print(tb_str) gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n" if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0] diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 311dc6f..4fd88bf 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -11,7 +11,7 @@ import tiktoken from functools import lru_cache from concurrent.futures import ThreadPoolExecutor -from toolbox import get_conf +from toolbox import get_conf, trimmed_format_exc from .bridge_chatgpt import predict_no_ui_long_connection as chatgpt_noui from .bridge_chatgpt import predict as chatgpt_ui @@ -128,10 +128,7 @@ def LLM_CATCH_EXCEPTION(f): 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' + tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n' observe_window[0] = tb_str return tb_str return decorated diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index 5e32f45..48eaba0 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -21,7 +21,7 @@ import importlib # config_private.py放自己的秘密如API和代理网址 # 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件 -from toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys, clip_history +from toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys, clip_history, trimmed_format_exc proxies, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \ get_conf('proxies', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY') @@ -215,7 +215,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp chatbot[-1] = (chatbot[-1][0], "[Local Message] Not enough point. API2D账户点数不足.") else: from toolbox import regular_txt_to_markdown - tb_str = '```\n' + traceback.format_exc() + '```' + tb_str = '```\n' + trimmed_format_exc() + '```' 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/toolbox.py b/toolbox.py index c9dc207..5236f5f 100644 --- a/toolbox.py +++ b/toolbox.py @@ -5,7 +5,17 @@ import inspect import re from latex2mathml.converter import convert as tex2mathml from functools import wraps, lru_cache -############################### 插件输入输出接驳区 ####################################### + +""" +======================================================================== +函数插件输入输出接驳区 + - ChatBotWithCookies: 带Cookies的Chatbot类,为实现更多强大的功能做基础 + - ArgsGeneralWrapper: 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构 + - update_ui: 刷新界面用 yield from update_ui(chatbot, history) + - CatchException: 将插件中出的所有问题显示在界面上 +======================================================================== +""" + class ChatBotWithCookies(list): def __init__(self, cookie): self._cookies = cookie @@ -20,6 +30,7 @@ class ChatBotWithCookies(list): def get_cookies(self): return self._cookies + def ArgsGeneralWrapper(f): """ 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。 @@ -47,6 +58,7 @@ def ArgsGeneralWrapper(f): yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args) return decorated + def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面 """ 刷新用户界面 @@ -54,10 +66,18 @@ def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面 assert isinstance(chatbot, ChatBotWithCookies), "在传递chatbot的过程中不要将其丢弃。必要时,可用clear将其清空,然后用for+append循环重新赋值。" yield chatbot.get_cookies(), chatbot, history, msg +def trimmed_format_exc(): + import os, traceback + str = traceback.format_exc() + current_path = os.getcwd() + replace_path = "." + return str.replace(current_path, replace_path) + def CatchException(f): """ 装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。 """ + @wraps(f) def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT): try: @@ -66,7 +86,7 @@ def CatchException(f): from check_proxy import check_proxy from toolbox import get_conf proxies, = get_conf('proxies') - tb_str = '```\n' + traceback.format_exc() + '```' + tb_str = '```\n' + trimmed_format_exc() + '```' if chatbot is None or len(chatbot) == 0: chatbot = [["插件调度异常", "异常原因"]] chatbot[-1] = (chatbot[-1][0], @@ -92,8 +112,11 @@ def HotReload(f): yield from f_hot_reload(*args, **kwargs) return decorated - -####################################### 其他小工具 ##################################### +""" +======================================================================== +其他小工具 +======================================================================== +""" def get_reduce_token_percent(text): """ @@ -113,7 +136,6 @@ def get_reduce_token_percent(text): return 0.5, '不详' - def write_results_to_file(history, file_name=None): """ 将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。 From 06fbdf43af3b003093837e9a2aa446c18dd31e4a Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 18:34:16 +0800 Subject: [PATCH 05/66] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toolbox.py | 29 +++++++++++++++++++++++++---- version | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/toolbox.py b/toolbox.py index 5236f5f..81ff91d 100644 --- a/toolbox.py +++ b/toolbox.py @@ -8,11 +8,14 @@ from functools import wraps, lru_cache """ ======================================================================== +第一部分 函数插件输入输出接驳区 - ChatBotWithCookies: 带Cookies的Chatbot类,为实现更多强大的功能做基础 - ArgsGeneralWrapper: 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构 - update_ui: 刷新界面用 yield from update_ui(chatbot, history) - CatchException: 将插件中出的所有问题显示在界面上 + - HotReload: 实现插件的热更新 + - trimmed_format_exc: 打印traceback,为了安全而隐藏绝对地址 ======================================================================== """ @@ -112,9 +115,22 @@ def HotReload(f): yield from f_hot_reload(*args, **kwargs) return decorated + """ ======================================================================== -其他小工具 +第二部分 +其他小工具: + - write_results_to_file: 将结果写入markdown文件中 + - regular_txt_to_markdown: 将普通文本转换为Markdown格式的文本。 + - report_execption: 向chatbot中添加简单的意外错误信息 + - text_divide_paragraph: 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 + - markdown_convertion: 用多种方式组合,将markdown转化为好看的html + - format_io: 接管gradio默认的markdown处理方式 + - on_file_uploaded: 处理文件的上传(自动解压) + - on_report_generated: 将生成的报告自动投射到文件上传区 + - clip_history: 当历史上下文过长时,自动截断 + - get_conf: 获取设置 + - select_api_key: 根据当前的模型类别,抽取可用的api-key ======================================================================== """ @@ -391,6 +407,9 @@ def find_recent_files(directory): def on_file_uploaded(files, chatbot, txt, txt2, checkboxes): + """ + 当文件被上传时的回调函数 + """ if len(files) == 0: return chatbot, txt import shutil @@ -410,8 +429,7 @@ def on_file_uploaded(files, chatbot, txt, txt2, checkboxes): shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}') err_msg += extract_archive(f'private_upload/{time_tag}/{file_origin_name}', dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract') - moved_files = [fp for fp in glob.glob( - 'private_upload/**/*', recursive=True)] + moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)] if "底部输入区" in checkboxes: txt = "" txt2 = f'private_upload/{time_tag}' @@ -530,7 +548,7 @@ def clear_line_break(txt): class DummyWith(): """ 这段代码定义了一个名为DummyWith的空上下文管理器, - 它的作用是……额……没用,即在代码结构不变得情况下取代其他的上下文管理器。 + 它的作用是……额……就是不起作用,即在代码结构不变得情况下取代其他的上下文管理器。 上下文管理器是一种Python对象,用于与with语句一起使用, 以确保一些资源在代码块执行期间得到正确的初始化和清理。 上下文管理器必须实现两个方法,分别为 __enter__()和 __exit__()。 @@ -544,6 +562,9 @@ class DummyWith(): return def run_gradio_in_subpath(demo, auth, port, custom_path): + """ + 把gradio的运行地址更改到指定的二次路径上 + """ def is_path_legal(path: str)->bool: ''' check path for sub url diff --git a/version b/version index a2a877b..a1e8bc9 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { - "version": 3.2, + "version": 3.3, "show_feature": true, "new_feature": "保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)" } From 94dc3981631b0fc4953f22c375e80a7fc4ccfc38 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 18:37:15 +0800 Subject: [PATCH 06/66] restore default model --- config.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index 4c4495a..b3f2c55 100644 --- a/config.py +++ b/config.py @@ -44,7 +44,7 @@ WEB_PORT = -1 MAX_RETRY = 2 # OpenAI模型选择是(gpt4现在只对申请成功的人开放,体验gpt-4可以试试api2d) -LLM_MODEL = "newbing" # 可选 ↓↓↓ +LLM_MODEL = "gpt-3.5-turbo" # 可选 ↓↓↓ AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm", "newbing"] # 本地LLM模型如ChatGLM的执行方式 CPU/GPU @@ -64,5 +64,7 @@ API_URL_REDIRECT = {} # 如果需要在二级路径下运行(常规情况下,不要修改!!)(需要配合修改main.py才能生效!) CUSTOM_PATH = "/" - -newbing_cookies = """your bing cookies here""" \ No newline at end of file +# 如果需要使用newbing,把newbing的cookie放到这里 +newbing_cookies = """ +your bing cookies here +""" \ No newline at end of file From 9cb51ccc70de36409c37c040844637dc8ab52019 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 18:38:05 +0800 Subject: [PATCH 07/66] restore default model --- config.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index 4c4495a..febb902 100644 --- a/config.py +++ b/config.py @@ -44,7 +44,7 @@ WEB_PORT = -1 MAX_RETRY = 2 # OpenAI模型选择是(gpt4现在只对申请成功的人开放,体验gpt-4可以试试api2d) -LLM_MODEL = "newbing" # 可选 ↓↓↓ +LLM_MODEL = "gpt-3.5-turbo" # 可选 ↓↓↓ AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm", "newbing"] # 本地LLM模型如ChatGLM的执行方式 CPU/GPU @@ -64,5 +64,7 @@ API_URL_REDIRECT = {} # 如果需要在二级路径下运行(常规情况下,不要修改!!)(需要配合修改main.py才能生效!) CUSTOM_PATH = "/" - -newbing_cookies = """your bing cookies here""" \ No newline at end of file +# 如果需要使用newbing,把newbing的长长的cookie放到这里 +newbing_cookies = """ +your bing cookies here +""" \ No newline at end of file From 518385dea2112b780bcbadc80b0058ad343987d7 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 19:17:09 +0800 Subject: [PATCH 08/66] add newbing, testing --- config.py | 3 +- request_llm/bridge_all.py | 4 +- request_llm/bridge_newbing.py | 97 +++++++++++++++++++++-------------- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/config.py b/config.py index 4ccf49f..04ceb40 100644 --- a/config.py +++ b/config.py @@ -65,6 +65,7 @@ API_URL_REDIRECT = {} CUSTOM_PATH = "/" # 如果需要使用newbing,把newbing的长长的cookie放到这里 -newbing_cookies = """ +NEWBING_STYLE = "creative", # ["creative", "balanced", "precise"] +NEWBING_COOKIES = """ your bing cookies here """ \ No newline at end of file diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 0334aca..7937d5a 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -51,6 +51,7 @@ class LazyloadTiktoken(object): API_URL_REDIRECT, = get_conf("API_URL_REDIRECT") openai_endpoint = "https://api.openai.com/v1/chat/completions" api2d_endpoint = "https://openai.api2d.net/v1/chat/completions" +newbing_endpoint = "wss://sydney.bing.com/sydney/ChatHub" # 兼容旧版的配置 try: API_URL, = get_conf("API_URL") @@ -62,6 +63,7 @@ except: # 新版配置 if openai_endpoint in API_URL_REDIRECT: openai_endpoint = API_URL_REDIRECT[openai_endpoint] if api2d_endpoint in API_URL_REDIRECT: api2d_endpoint = API_URL_REDIRECT[api2d_endpoint] +if newbing_endpoint in API_URL_REDIRECT: newbing_endpoint = API_URL_REDIRECT[newbing_endpoint] # 获取tokenizer @@ -123,7 +125,7 @@ model_info = { "newbing": { "fn_with_ui": newbing_ui, "fn_without_ui": newbing_noui, - "endpoint": None, + "endpoint": newbing_endpoint, "max_token": 4096, "tokenizer": tokenizer_gpt35, "token_cnt": get_token_num_gpt35, diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 4dfcd55..a70984c 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -1,5 +1,8 @@ """ -Main.py +======================================================================== +第一部分:来自EdgeGPT.py +https://github.com/acheong08/EdgeGPT +======================================================================== """ from transformers import AutoModel, AutoTokenizer @@ -273,6 +276,7 @@ class _ChatHub: self.request: _ChatHubRequest self.loop: bool self.task: asyncio.Task + print(conversation.struct) self.request = _ChatHubRequest( conversation_signature=conversation.struct["conversationSignature"], client_id=conversation.struct["clientId"], @@ -461,20 +465,20 @@ def _create_completer(commands: list, pattern_str: str = "$"): load_message = "" -################################################################################# -################################################################################# -################################################################################# -################################################################################# -################################################################################# -################################################################################# +""" +======================================================================== +第二部分:子进程Worker +======================================================================== +""" + class GetNewBingHandle(Process): def __init__(self): super().__init__(daemon=True) self.parent, self.child = Pipe() - self.chatglm_model = None - self.chatglm_tokenizer = None + self.newbing_model = None self.info = "" self.success = True + self.local_history = [] self.check_dependency() self.start() @@ -488,28 +492,36 @@ class GetNewBingHandle(Process): self.success = False def ready(self): - return self.chatglm_model is not None + return self.newbing_model is not None - async def async_run(self, question): - async for final, response in self.chatglm_model.ask_stream( + async def async_run(self, question, history): + # 读取配置 + NEWBING_STYLE, = get_conf('NEWBING_STYLE') + from request_llm.bridge_all import model_info + endpoint = model_info['newbing']['endpoint'] + + # 开始问问题 + self.local_history.append(question) + async for final, response in self.newbing_model.ask_stream( prompt=question, - conversation_style="balanced", # ["creative", "balanced", "precise"] - wss_link="wss://sydney.bing.com/sydney/ChatHub", # "wss://sydney.bing.com/sydney/ChatHub" + conversation_style=NEWBING_STYLE, # ["creative", "balanced", "precise"] + wss_link=endpoint, # "wss://sydney.bing.com/sydney/ChatHub" ): if not final: - self.child.send(response) + self.child.send(str(response)) print(response) def run(self): # 第一次运行,加载参数 retry = 0 + self.local_history = [] while True: try: - if self.chatglm_model is None: + if self.newbing_model is None: proxies, = get_conf('proxies') - newbing_cookies, = get_conf('newbing_cookies') - cookies = json.loads(newbing_cookies) - self.chatglm_model = Chatbot(proxy=proxies['https'], cookies=cookies) + NEWBING_COOKIES, = get_conf('NEWBING_COOKIES') + cookies = json.loads(NEWBING_COOKIES) + self.newbing_model = Chatbot(proxy=proxies['https'], cookies=cookies) break else: break @@ -517,13 +529,14 @@ class GetNewBingHandle(Process): retry += 1 if retry > 3: self.child.send('[Local Message] 不能加载Newbing组件。') + self.success = False raise RuntimeError("不能加载Newbing组件。") # 进入任务等待状态 while True: kwargs = self.child.recv() try: - asyncio.run(self.async_run(question=kwargs['query'])) + asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history'])) except: self.child.send('[Local Message] Newbing失败.') self.child.send('[Finish]') @@ -538,24 +551,30 @@ class GetNewBingHandle(Process): break return -global glm_handle -glm_handle = None -################################################################################# + +""" +======================================================================== +第三部分:主进程统一调用函数接口 +======================================================================== +""" +global newbing_handle +newbing_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 glm_handle - if glm_handle is None: - glm_handle = GetNewBingHandle() - observe_window[0] = load_message + "\n\n" + glm_handle.info - if not glm_handle.success: - error = glm_handle.info - glm_handle = None + global newbing_handle + if newbing_handle is None or (not newbing_handle.success): + newbing_handle = GetNewBingHandle() + observe_window[0] = load_message + "\n\n" + newbing_handle.info + if not newbing_handle.success: + error = newbing_handle.info + newbing_handle = None raise RuntimeError(error) - # chatglm 没有 sys_prompt 接口,因此把prompt加入 history + # 没有 sys_prompt 接口,因此把prompt加入 history history_feedin = [] history_feedin.append(["What can I do?", sys_prompt]) for i in range(len(history)//2): @@ -563,7 +582,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 response = "" - 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']): + for response in newbing_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 if len(observe_window) >= 2: if (time.time()-observe_window[1]) > watch_dog_patience: @@ -579,13 +598,13 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp """ chatbot.append((inputs, "")) - global glm_handle - if glm_handle is None: - glm_handle = GetNewBingHandle() - chatbot[-1] = (inputs, load_message + "\n\n" + glm_handle.info) + global newbing_handle + if newbing_handle is None or (not newbing_handle.success): + newbing_handle = GetNewBingHandle() + chatbot[-1] = (inputs, load_message + "\n\n" + newbing_handle.info) yield from update_ui(chatbot=chatbot, history=[]) - if not glm_handle.success: - glm_handle = None + if not newbing_handle.success: + newbing_handle = None return if additional_fn is not None: @@ -600,7 +619,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) - 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']): + for response in newbing_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) From 385c775aa52c1cdc0cd71a316ae58b255a61c0a8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 20:54:57 +0800 Subject: [PATCH 09/66] =?UTF-8?q?=E6=94=AF=E6=8C=813.10=E4=BB=A5=E4=B8=8B?= =?UTF-8?q?=E7=9A=84python=E7=89=88=E6=9C=AC=E4=BD=BF=E7=94=A8newbing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- request_llm/bridge_newbing.py | 87 +++++++---------------------------- 2 files changed, 18 insertions(+), 71 deletions(-) diff --git a/config.py b/config.py index 04ceb40..688f35d 100644 --- a/config.py +++ b/config.py @@ -65,7 +65,7 @@ API_URL_REDIRECT = {} CUSTOM_PATH = "/" # 如果需要使用newbing,把newbing的长长的cookie放到这里 -NEWBING_STYLE = "creative", # ["creative", "balanced", "precise"] +NEWBING_STYLE = "creative" # ["creative", "balanced", "precise"] NEWBING_COOKIES = """ your bing cookies here """ \ No newline at end of file diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index a70984c..3bc59eb 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -5,11 +5,7 @@ https://github.com/acheong08/EdgeGPT ======================================================================== """ -from transformers import AutoModel, AutoTokenizer -import time -import importlib -from toolbox import update_ui, get_conf -from multiprocessing import Process, Pipe + import argparse import asyncio import json @@ -28,13 +24,6 @@ from typing import Union import certifi import httpx import websockets.client as websockets -from prompt_toolkit import PromptSession -from prompt_toolkit.auto_suggest import AutoSuggestFromHistory -from prompt_toolkit.completion import WordCompleter -from prompt_toolkit.history import InMemoryHistory -from prompt_toolkit.key_binding import KeyBindings -from rich.live import Live -from rich.markdown import Markdown DELIMITER = "\x1e" @@ -146,9 +135,9 @@ class _ChatHubRequest: def update( self, - prompt: str, - conversation_style: CONVERSATION_STYLE_TYPE, - options: list | None = None, + prompt, + conversation_style, + options, ) -> None: """ Updates request object @@ -214,8 +203,8 @@ class _Conversation: def __init__( self, - cookies: dict, - proxy: str | None = None, + cookies, + proxy, ) -> None: self.struct: dict = { "conversationId": None, @@ -271,8 +260,8 @@ class _ChatHub: Chat API """ - def __init__(self, conversation: _Conversation) -> None: - self.wss: websockets.WebSocketClientProtocol | None = None + def __init__(self, conversation) -> None: + self.wss = None self.request: _ChatHubRequest self.loop: bool self.task: asyncio.Task @@ -351,21 +340,13 @@ class Chatbot: def __init__( self, - cookies: dict = None, - proxy: str | None = None, - cookie_path: str = None, + cookies, + proxy ) -> None: if cookies is None: cookies = {} - if cookie_path is not None: - try: - with open(cookie_path, encoding="utf-8") as f: - self.cookies = json.load(f) - except FileNotFoundError as exc: - raise FileNotFoundError("Cookie file not found") from exc - else: - self.cookies = cookies - self.proxy: str | None = proxy + self.cookies = cookies + self.proxy = proxy self.chat_hub: _ChatHub = _ChatHub( _Conversation(self.cookies, self.proxy), ) @@ -425,43 +406,6 @@ class Chatbot: self.chat_hub = _ChatHub(_Conversation(self.cookies)) -async def _get_input_async( - session: PromptSession = None, - completer: WordCompleter = None, -) -> str: - """ - Multiline input function. - """ - return await session.prompt_async( - completer=completer, - multiline=True, - auto_suggest=AutoSuggestFromHistory(), - ) - - -def _create_session() -> PromptSession: - kb = KeyBindings() - - @kb.add("enter") - def _(event): - buffer_text = event.current_buffer.text - if buffer_text.startswith("!"): - event.current_buffer.validate_and_handle() - else: - event.current_buffer.insert_text("\n") - - @kb.add("escape") - def _(event): - if event.current_buffer.complete_state: - # event.current_buffer.cancel_completion() - event.current_buffer.text = "" - - return PromptSession(key_bindings=kb, history=InMemoryHistory()) - - -def _create_completer(commands: list, pattern_str: str = "$"): - return WordCompleter(words=commands, pattern=re.compile(pattern_str)) - load_message = "" @@ -470,7 +414,10 @@ load_message = "" 第二部分:子进程Worker ======================================================================== """ - +import time +import importlib +from toolbox import update_ui, get_conf, trimmed_format_exc +from multiprocessing import Process, Pipe class GetNewBingHandle(Process): def __init__(self): super().__init__(daemon=True) @@ -484,7 +431,6 @@ class GetNewBingHandle(Process): def check_dependency(self): try: - import rich self.info = "依赖检测通过" self.success = True except: @@ -538,6 +484,7 @@ class GetNewBingHandle(Process): try: asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history'])) except: + tb_str = '```\n' + trimmed_format_exc() + '```' self.child.send('[Local Message] Newbing失败.') self.child.send('[Finish]') From 4a494354b1ecc718fe25ad839b4d966e8259bac3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 22:34:24 +0800 Subject: [PATCH 10/66] =?UTF-8?q?=E6=98=BE=E7=A4=BAnewbing=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E7=BD=91=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 132 ++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 39 deletions(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 3bc59eb..986dc56 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -16,7 +16,6 @@ import ssl import sys import uuid from enum import Enum -from pathlib import Path from typing import Generator from typing import Literal from typing import Optional @@ -354,7 +353,7 @@ class Chatbot: async def ask( self, prompt: str, - wss_link: str = "wss://sydney.bing.com/sydney/ChatHub", + wss_link: str, conversation_style: CONVERSATION_STYLE_TYPE = None, options: dict = None, ) -> dict: @@ -375,7 +374,7 @@ class Chatbot: async def ask_stream( self, prompt: str, - wss_link: str = "wss://sydney.bing.com/sydney/ChatHub", + wss_link: str, conversation_style: CONVERSATION_STYLE_TYPE = None, raw: bool = False, options: dict = None, @@ -403,7 +402,7 @@ class Chatbot: Reset the conversation """ await self.close() - self.chat_hub = _ChatHub(_Conversation(self.cookies)) + self.chat_hub = _ChatHub(_Conversation(self.cookies, self.proxy)) @@ -411,13 +410,14 @@ load_message = "" """ ======================================================================== -第二部分:子进程Worker +第二部分:子进程Worker(调用主体) ======================================================================== """ import time import importlib from toolbox import update_ui, get_conf, trimmed_format_exc from multiprocessing import Process, Pipe + class GetNewBingHandle(Process): def __init__(self): super().__init__(daemon=True) @@ -431,7 +431,8 @@ class GetNewBingHandle(Process): def check_dependency(self): try: - self.info = "依赖检测通过" + import rich + self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口,否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。" self.success = True except: self.info = "缺少的依赖,如果要使用Newbing,除了基础的pip依赖以外,您还需要运行`pip install -r request_llm/requirements_newbing.txt`安装Newbing的依赖。" @@ -440,34 +441,77 @@ class GetNewBingHandle(Process): def ready(self): return self.newbing_model is not None - async def async_run(self, question, history): + async def async_run(self): # 读取配置 NEWBING_STYLE, = get_conf('NEWBING_STYLE') from request_llm.bridge_all import model_info endpoint = model_info['newbing']['endpoint'] - - # 开始问问题 - self.local_history.append(question) - async for final, response in self.newbing_model.ask_stream( - prompt=question, - conversation_style=NEWBING_STYLE, # ["creative", "balanced", "precise"] - wss_link=endpoint, # "wss://sydney.bing.com/sydney/ChatHub" - ): - if not final: - self.child.send(str(response)) - print(response) + while True: + # 等待 + kwargs = self.child.recv() + question=kwargs['query'] + history=kwargs['history'] + system_prompt=kwargs['system_prompt'] + # 是否重置 + if len(self.local_history) > 0 and len(history)==0: + await self.newbing_model.reset() + self.local_history = [] + + # 开始问问题 + prompt = "" + if system_prompt not in self.local_history: + self.local_history.append(system_prompt) + prompt += system_prompt + '\n' + + # 追加历史 + for ab in history: + a, b = ab + if a not in self.local_history: + self.local_history.append(a) + prompt += a + '\n' + if b not in self.local_history: + self.local_history.append(b) + prompt += b + '\n' + + # 问题 + prompt += question + self.local_history.append(question) + + # 提交 + async for final, response in self.newbing_model.ask_stream( + prompt=question, + conversation_style=NEWBING_STYLE, # ["creative", "balanced", "precise"] + wss_link=endpoint, # "wss://sydney.bing.com/sydney/ChatHub" + ): + if not final: + print(response) + self.child.send(str(response)) + else: + print('-------- receive final ---------') + self.child.send('[Finish]') + + def run(self): + """ + 这个函数运行在子进程 + """ # 第一次运行,加载参数 retry = 0 self.local_history = [] while True: try: if self.newbing_model is None: + # 代理设置 proxies, = get_conf('proxies') + if proxies is None: + self.proxies_https = None + else: + self.proxies_https = proxies['https'] + NEWBING_COOKIES, = get_conf('NEWBING_COOKIES') cookies = json.loads(NEWBING_COOKIES) - self.newbing_model = Chatbot(proxy=proxies['https'], cookies=cookies) + self.newbing_model = Chatbot(proxy=self.proxies_https, cookies=cookies) break else: break @@ -479,23 +523,24 @@ class GetNewBingHandle(Process): raise RuntimeError("不能加载Newbing组件。") # 进入任务等待状态 - while True: - kwargs = self.child.recv() - try: - asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history'])) - except: - tb_str = '```\n' + trimmed_format_exc() + '```' - self.child.send('[Local Message] Newbing失败.') + try: + asyncio.run(self.async_run()) + except Exception: + tb_str = '```\n' + trimmed_format_exc() + '```' + self.child.send(f'[Local Message] Newbing失败 {tb_str}.') self.child.send('[Finish]') def stream_chat(self, **kwargs): - self.parent.send(kwargs) + """ + 这个函数运行在主进程 + """ + self.parent.send(kwargs) # 发送请求到子进程 while True: - res = self.parent.recv() + res = self.parent.recv() # 等待newbing回复的片段 if res != '[Finish]': - yield res + yield res # newbing回复的片段 else: - break + break # 结束 return @@ -523,13 +568,12 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", # 没有 sys_prompt 接口,因此把prompt加入 history history_feedin = [] - history_feedin.append(["What can I do?", sys_prompt]) for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 response = "" - for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): + for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=sys_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): observe_window[0] = response if len(observe_window) >= 2: if (time.time()-observe_window[1]) > watch_dog_patience: @@ -543,7 +587,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp 单线程方法 函数的说明请见 request_llm/bridge_all.py """ - chatbot.append((inputs, "")) + chatbot.append((inputs, "[Local Message]: 等待Bing响应 ...")) global newbing_handle if newbing_handle is None or (not newbing_handle.success): @@ -562,13 +606,23 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp inputs = core_functional[additional_fn]["Prefix"] + inputs + core_functional[additional_fn]["Suffix"] history_feedin = [] - history_feedin.append(["What can I do?", system_prompt] ) for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) - for response in newbing_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) + yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") + for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=system_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): + chatbot[-1] = (inputs, preprocess_newbing_out(response)) + yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") - history.extend([inputs, response]) - yield from update_ui(chatbot=chatbot, history=history) \ No newline at end of file + history.extend([inputs, preprocess_newbing_out(response)]) + yield from update_ui(chatbot=chatbot, history=history, msg="完成全部响应,请提交新问题。") + +def preprocess_newbing_out(s): + pattern = r'\^(\d+)\^' # 匹配^数字^ + sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值 + result = re.sub(pattern, sub, s) # 替换操作 + + if '[1]' in result: + result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' + + return result \ No newline at end of file From 781ef4487c7d9cecc47c73c110676aabdcaa6789 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 22:44:18 +0800 Subject: [PATCH 11/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=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 --- request_llm/bridge_all.py | 2 +- request_llm/bridge_newbing.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 7937d5a..fddc9a7 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -192,7 +192,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, obser def mutex_manager(window_mutex, observe_window): while True: - time.sleep(0.5) + time.sleep(0.25) if not window_mutex[-1]: break # 看门狗(watchdog) for i in range(n_model): diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 986dc56..3fdc7ba 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -406,7 +406,7 @@ class Chatbot: -load_message = "" +load_message = "等待NewBing响应。" """ ======================================================================== @@ -574,13 +574,16 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 response = "" for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=sys_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): - observe_window[0] = response + observe_window[0] = preprocess_newbing_out_simple(response) if len(observe_window) >= 2: if (time.time()-observe_window[1]) > watch_dog_patience: raise RuntimeError("程序终止。") - return response - + return preprocess_newbing_out_simple(response) +def preprocess_newbing_out_simple(result): + if '[1]' in result: + result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' + return result def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): """ @@ -609,6 +612,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) + chatbot[-1] = (inputs, preprocess_newbing_out(response)) yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=system_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): chatbot[-1] = (inputs, preprocess_newbing_out(response)) From 1cf8b6c6c8193939e6e39681ef900253acf8d3fd Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 22:47:45 +0800 Subject: [PATCH 12/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 3fdc7ba..33bcbd6 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -612,7 +612,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) - chatbot[-1] = (inputs, preprocess_newbing_out(response)) + chatbot[-1] = (inputs, "[Local Message]: 等待Bing响应 ...") yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=system_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): chatbot[-1] = (inputs, preprocess_newbing_out(response)) From 9c2a6bc413d9ce5ea08da24e749e59407a94a20d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:13:00 +0800 Subject: [PATCH 13/66] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 96 ++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 33bcbd6..321e42d 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -431,6 +431,7 @@ class GetNewBingHandle(Process): def check_dependency(self): try: + self.success = False import rich self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口,否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。" self.success = True @@ -497,39 +498,47 @@ class GetNewBingHandle(Process): 这个函数运行在子进程 """ # 第一次运行,加载参数 - retry = 0 + self.success = False self.local_history = [] - while True: + if (self.newbing_model is None) or (not self.success): + # 代理设置 + proxies, = get_conf('proxies') + if proxies is None: + self.proxies_https = None + else: + self.proxies_https = proxies['https'] + # cookie + NEWBING_COOKIES, = get_conf('NEWBING_COOKIES') try: - if self.newbing_model is None: - # 代理设置 - proxies, = get_conf('proxies') - if proxies is None: - self.proxies_https = None - else: - self.proxies_https = proxies['https'] - - NEWBING_COOKIES, = get_conf('NEWBING_COOKIES') - cookies = json.loads(NEWBING_COOKIES) - self.newbing_model = Chatbot(proxy=self.proxies_https, cookies=cookies) - break - else: - break + cookies = json.loads(NEWBING_COOKIES) except: - retry += 1 - if retry > 3: - self.child.send('[Local Message] 不能加载Newbing组件。') - self.success = False - raise RuntimeError("不能加载Newbing组件。") + self.success = False + tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n' + self.child.send(f'[Local Message] 不能加载Newbing组件。NEWBING_COOKIES未填写或有格式错误。') + self.child.send('[Fail]') + self.child.send('[Finish]') + raise RuntimeError(f"不能加载Newbing组件。NEWBING_COOKIES未填写或有格式错误。") - # 进入任务等待状态 + try: + self.newbing_model = Chatbot(proxy=self.proxies_https, cookies=cookies) + except: + self.success = False + tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n' + self.child.send(f'[Local Message] 不能加载Newbing组件。{tb_str}') + self.child.send('[Fail]') + self.child.send('[Finish]') + raise RuntimeError(f"不能加载Newbing组件。") + + self.success = True try: + # 进入任务等待状态 asyncio.run(self.async_run()) except Exception: tb_str = '```\n' + trimmed_format_exc() + '```' self.child.send(f'[Local Message] Newbing失败 {tb_str}.') + self.child.send('[Fail]') self.child.send('[Finish]') - + def stream_chat(self, **kwargs): """ 这个函数运行在主进程 @@ -537,13 +546,30 @@ class GetNewBingHandle(Process): self.parent.send(kwargs) # 发送请求到子进程 while True: res = self.parent.recv() # 等待newbing回复的片段 - if res != '[Finish]': - yield res # newbing回复的片段 - else: + if res == '[Finish]': break # 结束 + elif res == '[Fail]': + self.success = False + break + else: + yield res # newbing回复的片段 return +def preprocess_newbing_out(s): + pattern = r'\^(\d+)\^' # 匹配^数字^ + sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值 + result = re.sub(pattern, sub, s) # 替换操作 + if '[1]' in result: + result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' + return result + +def preprocess_newbing_out_simple(result): + if '[1]' in result: + result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' + return result + + """ ======================================================================== 第三部分:主进程统一调用函数接口 @@ -558,7 +584,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", 函数的说明请见 request_llm/bridge_all.py """ global newbing_handle - if newbing_handle is None or (not newbing_handle.success): + if (newbing_handle is None) or (not newbing_handle.success): newbing_handle = GetNewBingHandle() observe_window[0] = load_message + "\n\n" + newbing_handle.info if not newbing_handle.success: @@ -580,11 +606,6 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", raise RuntimeError("程序终止。") return preprocess_newbing_out_simple(response) -def preprocess_newbing_out_simple(result): - if '[1]' in result: - result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' - return result - def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): """ 单线程方法 @@ -593,7 +614,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp chatbot.append((inputs, "[Local Message]: 等待Bing响应 ...")) global newbing_handle - if newbing_handle is None or (not newbing_handle.success): + if (newbing_handle is None) or (not newbing_handle.success): newbing_handle = GetNewBingHandle() chatbot[-1] = (inputs, load_message + "\n\n" + newbing_handle.info) yield from update_ui(chatbot=chatbot, history=[]) @@ -621,12 +642,3 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp history.extend([inputs, preprocess_newbing_out(response)]) yield from update_ui(chatbot=chatbot, history=history, msg="完成全部响应,请提交新问题。") -def preprocess_newbing_out(s): - pattern = r'\^(\d+)\^' # 匹配^数字^ - sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值 - result = re.sub(pattern, sub, s) # 替换操作 - - if '[1]' in result: - result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' - - return result \ No newline at end of file From 3041858e7f7d41195be5a6c43852fb78effe92b5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:16:25 +0800 Subject: [PATCH 14/66] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 321e42d..43c3e76 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -611,7 +611,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp 单线程方法 函数的说明请见 request_llm/bridge_all.py """ - chatbot.append((inputs, "[Local Message]: 等待Bing响应 ...")) + chatbot.append((inputs, "[Local Message]: 等待NewBing响应中 ...")) global newbing_handle if (newbing_handle is None) or (not newbing_handle.success): @@ -633,7 +633,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) - chatbot[-1] = (inputs, "[Local Message]: 等待Bing响应 ...") + chatbot[-1] = (inputs, "[Local Message]: 等待NewBing响应中 ...") yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=system_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): chatbot[-1] = (inputs, preprocess_newbing_out(response)) From 5948dcacd579559db8f8e25ebf875afbe99f497b Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:25:49 +0800 Subject: [PATCH 15/66] =?UTF-8?q?=E5=8A=A0=E7=BA=BF=E7=A8=8B=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 43c3e76..3166e21 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -415,6 +415,7 @@ load_message = "等待NewBing响应。" """ import time import importlib +import threading from toolbox import update_ui, get_conf, trimmed_format_exc from multiprocessing import Process, Pipe @@ -428,6 +429,7 @@ class GetNewBingHandle(Process): self.local_history = [] self.check_dependency() self.start() + self.threadLock = threading.Lock() def check_dependency(self): try: @@ -543,6 +545,7 @@ class GetNewBingHandle(Process): """ 这个函数运行在主进程 """ + self.threadLock.acquire() self.parent.send(kwargs) # 发送请求到子进程 while True: res = self.parent.recv() # 等待newbing回复的片段 @@ -553,7 +556,7 @@ class GetNewBingHandle(Process): break else: yield res # newbing回复的片段 - return + self.threadLock.release() def preprocess_newbing_out(s): @@ -599,6 +602,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", watch_dog_patience = 5 # 看门狗 (watchdog) 的耐心, 设置5秒即可 response = "" + observe_window[0] = "[Local Message]: 等待NewBing响应中 ..." for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=sys_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): observe_window[0] = preprocess_newbing_out_simple(response) if len(observe_window) >= 2: From d2e46f668465c8054d1d6ed30a4035df6d3a8e1a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:26:23 +0800 Subject: [PATCH 16/66] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 3166e21..1f5f493 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -435,7 +435,7 @@ class GetNewBingHandle(Process): try: self.success = False import rich - self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口,否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。" + self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口(有线程锁),否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。" self.success = True except: self.info = "缺少的依赖,如果要使用Newbing,除了基础的pip依赖以外,您还需要运行`pip install -r request_llm/requirements_newbing.txt`安装Newbing的依赖。" From ee94fa6dc4b19e98d0a5ec9aa68ed34227ced9bc Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:32:35 +0800 Subject: [PATCH 17/66] =?UTF-8?q?=E6=8B=86=E5=88=86=E6=88=90=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 414 +--------------------------------- request_llm/edge_gpt.py | 406 +++++++++++++++++++++++++++++++++ 2 files changed, 414 insertions(+), 406 deletions(-) create mode 100644 request_llm/edge_gpt.py diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 1f5f493..79557ee 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -4,408 +4,7 @@ https://github.com/acheong08/EdgeGPT ======================================================================== """ - - -import argparse -import asyncio -import json -import os -import random -import re -import ssl -import sys -import uuid -from enum import Enum -from typing import Generator -from typing import Literal -from typing import Optional -from typing import Union -import certifi -import httpx -import websockets.client as websockets - -DELIMITER = "\x1e" - - -# Generate random IP between range 13.104.0.0/14 -FORWARDED_IP = ( - f"13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}" -) - -HEADERS = { - "accept": "application/json", - "accept-language": "en-US,en;q=0.9", - "content-type": "application/json", - "sec-ch-ua": '"Not_A Brand";v="99", "Microsoft Edge";v="110", "Chromium";v="110"', - "sec-ch-ua-arch": '"x86"', - "sec-ch-ua-bitness": '"64"', - "sec-ch-ua-full-version": '"109.0.1518.78"', - "sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"', - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-model": "", - "sec-ch-ua-platform": '"Windows"', - "sec-ch-ua-platform-version": '"15.0.0"', - "sec-fetch-dest": "empty", - "sec-fetch-mode": "cors", - "sec-fetch-site": "same-origin", - "x-ms-client-request-id": str(uuid.uuid4()), - "x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32", - "Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx", - "Referrer-Policy": "origin-when-cross-origin", - "x-forwarded-for": FORWARDED_IP, -} - -HEADERS_INIT_CONVER = { - "authority": "edgeservices.bing.com", - "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", - "accept-language": "en-US,en;q=0.9", - "cache-control": "max-age=0", - "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"', - "sec-ch-ua-arch": '"x86"', - "sec-ch-ua-bitness": '"64"', - "sec-ch-ua-full-version": '"110.0.1587.69"', - "sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"', - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-model": '""', - "sec-ch-ua-platform": '"Windows"', - "sec-ch-ua-platform-version": '"15.0.0"', - "sec-fetch-dest": "document", - "sec-fetch-mode": "navigate", - "sec-fetch-site": "none", - "sec-fetch-user": "?1", - "upgrade-insecure-requests": "1", - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69", - "x-edge-shopping-flag": "1", - "x-forwarded-for": FORWARDED_IP, -} - -ssl_context = ssl.create_default_context() -ssl_context.load_verify_locations(certifi.where()) - - -class NotAllowedToAccess(Exception): - pass - - -class ConversationStyle(Enum): - creative = "h3imaginative,clgalileo,gencontentv3" - balanced = "galileo" - precise = "h3precise,clgalileo" - - -CONVERSATION_STYLE_TYPE = Optional[ - Union[ConversationStyle, Literal["creative", "balanced", "precise"]] -] - - -def _append_identifier(msg: dict) -> str: - """ - Appends special character to end of message to identify end of message - """ - # Convert dict to json string - return json.dumps(msg) + DELIMITER - - -def _get_ran_hex(length: int = 32) -> str: - """ - Returns random hex string - """ - return "".join(random.choice("0123456789abcdef") for _ in range(length)) - - -class _ChatHubRequest: - """ - Request object for ChatHub - """ - - def __init__( - self, - conversation_signature: str, - client_id: str, - conversation_id: str, - invocation_id: int = 0, - ) -> None: - self.struct: dict = {} - - self.client_id: str = client_id - self.conversation_id: str = conversation_id - self.conversation_signature: str = conversation_signature - self.invocation_id: int = invocation_id - - def update( - self, - prompt, - conversation_style, - options, - ) -> None: - """ - Updates request object - """ - if options is None: - options = [ - "deepleo", - "enable_debug_commands", - "disable_emoji_spoken_text", - "enablemm", - ] - if conversation_style: - if not isinstance(conversation_style, ConversationStyle): - conversation_style = getattr(ConversationStyle, conversation_style) - options = [ - "nlu_direct_response_filter", - "deepleo", - "disable_emoji_spoken_text", - "responsible_ai_policy_235", - "enablemm", - conversation_style.value, - "dtappid", - "cricinfo", - "cricinfov2", - "dv3sugg", - ] - self.struct = { - "arguments": [ - { - "source": "cib", - "optionsSets": options, - "sliceIds": [ - "222dtappid", - "225cricinfo", - "224locals0", - ], - "traceId": _get_ran_hex(32), - "isStartOfSession": self.invocation_id == 0, - "message": { - "author": "user", - "inputMethod": "Keyboard", - "text": prompt, - "messageType": "Chat", - }, - "conversationSignature": self.conversation_signature, - "participant": { - "id": self.client_id, - }, - "conversationId": self.conversation_id, - }, - ], - "invocationId": str(self.invocation_id), - "target": "chat", - "type": 4, - } - self.invocation_id += 1 - - -class _Conversation: - """ - Conversation API - """ - - def __init__( - self, - cookies, - proxy, - ) -> None: - self.struct: dict = { - "conversationId": None, - "clientId": None, - "conversationSignature": None, - "result": {"value": "Success", "message": None}, - } - self.proxy = proxy - proxy = ( - proxy - or os.environ.get("all_proxy") - or os.environ.get("ALL_PROXY") - or os.environ.get("https_proxy") - or os.environ.get("HTTPS_PROXY") - or None - ) - if proxy is not None and proxy.startswith("socks5h://"): - proxy = "socks5://" + proxy[len("socks5h://") :] - self.session = httpx.Client( - proxies=proxy, - timeout=30, - headers=HEADERS_INIT_CONVER, - ) - for cookie in cookies: - self.session.cookies.set(cookie["name"], cookie["value"]) - - # Send GET request - response = self.session.get( - url=os.environ.get("BING_PROXY_URL") - or "https://edgeservices.bing.com/edgesvc/turing/conversation/create", - ) - if response.status_code != 200: - response = self.session.get( - "https://edge.churchless.tech/edgesvc/turing/conversation/create", - ) - if response.status_code != 200: - print(f"Status code: {response.status_code}") - print(response.text) - print(response.url) - raise Exception("Authentication failed") - try: - self.struct = response.json() - except (json.decoder.JSONDecodeError, NotAllowedToAccess) as exc: - raise Exception( - "Authentication failed. You have not been accepted into the beta.", - ) from exc - if self.struct["result"]["value"] == "UnauthorizedRequest": - raise NotAllowedToAccess(self.struct["result"]["message"]) - - -class _ChatHub: - """ - Chat API - """ - - def __init__(self, conversation) -> None: - self.wss = None - self.request: _ChatHubRequest - self.loop: bool - self.task: asyncio.Task - print(conversation.struct) - self.request = _ChatHubRequest( - conversation_signature=conversation.struct["conversationSignature"], - client_id=conversation.struct["clientId"], - conversation_id=conversation.struct["conversationId"], - ) - - async def ask_stream( - self, - prompt: str, - wss_link: str, - conversation_style: CONVERSATION_STYLE_TYPE = None, - raw: bool = False, - options: dict = None, - ) -> Generator[str, None, None]: - """ - Ask a question to the bot - """ - if self.wss and not self.wss.closed: - await self.wss.close() - # Check if websocket is closed - self.wss = await websockets.connect( - wss_link, - extra_headers=HEADERS, - max_size=None, - ssl=ssl_context, - ) - await self._initial_handshake() - # Construct a ChatHub request - self.request.update( - prompt=prompt, - conversation_style=conversation_style, - options=options, - ) - # Send request - await self.wss.send(_append_identifier(self.request.struct)) - final = False - while not final: - objects = str(await self.wss.recv()).split(DELIMITER) - for obj in objects: - if obj is None or not obj: - continue - response = json.loads(obj) - if response.get("type") != 2 and raw: - yield False, response - elif response.get("type") == 1 and response["arguments"][0].get( - "messages", - ): - resp_txt = response["arguments"][0]["messages"][0]["adaptiveCards"][ - 0 - ]["body"][0].get("text") - yield False, resp_txt - elif response.get("type") == 2: - final = True - yield True, response - - async def _initial_handshake(self) -> None: - await self.wss.send(_append_identifier({"protocol": "json", "version": 1})) - await self.wss.recv() - - async def close(self) -> None: - """ - Close the connection - """ - if self.wss and not self.wss.closed: - await self.wss.close() - - -class Chatbot: - """ - Combines everything to make it seamless - """ - - def __init__( - self, - cookies, - proxy - ) -> None: - if cookies is None: - cookies = {} - self.cookies = cookies - self.proxy = proxy - self.chat_hub: _ChatHub = _ChatHub( - _Conversation(self.cookies, self.proxy), - ) - - async def ask( - self, - prompt: str, - wss_link: str, - conversation_style: CONVERSATION_STYLE_TYPE = None, - options: dict = None, - ) -> dict: - """ - Ask a question to the bot - """ - async for final, response in self.chat_hub.ask_stream( - prompt=prompt, - conversation_style=conversation_style, - wss_link=wss_link, - options=options, - ): - if final: - return response - await self.chat_hub.wss.close() - return None - - async def ask_stream( - self, - prompt: str, - wss_link: str, - conversation_style: CONVERSATION_STYLE_TYPE = None, - raw: bool = False, - options: dict = None, - ) -> Generator[str, None, None]: - """ - Ask a question to the bot - """ - async for response in self.chat_hub.ask_stream( - prompt=prompt, - conversation_style=conversation_style, - wss_link=wss_link, - raw=raw, - options=options, - ): - yield response - - async def close(self) -> None: - """ - Close the connection - """ - await self.chat_hub.close() - - async def reset(self) -> None: - """ - Reset the conversation - """ - await self.close() - self.chat_hub = _ChatHub(_Conversation(self.cookies, self.proxy)) - - - +from .edge_gpt import NewbingChatbot load_message = "等待NewBing响应。" """ @@ -414,12 +13,15 @@ load_message = "等待NewBing响应。" ======================================================================== """ import time +import json +import re +import asyncio import importlib import threading from toolbox import update_ui, get_conf, trimmed_format_exc from multiprocessing import Process, Pipe -class GetNewBingHandle(Process): +class NewBingHandle(Process): def __init__(self): super().__init__(daemon=True) self.parent, self.child = Pipe() @@ -522,7 +124,7 @@ class GetNewBingHandle(Process): raise RuntimeError(f"不能加载Newbing组件。NEWBING_COOKIES未填写或有格式错误。") try: - self.newbing_model = Chatbot(proxy=self.proxies_https, cookies=cookies) + self.newbing_model = NewbingChatbot(proxy=self.proxies_https, cookies=cookies) except: self.success = False tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n' @@ -588,7 +190,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", """ global newbing_handle if (newbing_handle is None) or (not newbing_handle.success): - newbing_handle = GetNewBingHandle() + newbing_handle = NewBingHandle() observe_window[0] = load_message + "\n\n" + newbing_handle.info if not newbing_handle.success: error = newbing_handle.info @@ -619,7 +221,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp global newbing_handle if (newbing_handle is None) or (not newbing_handle.success): - newbing_handle = GetNewBingHandle() + newbing_handle = NewBingHandle() chatbot[-1] = (inputs, load_message + "\n\n" + newbing_handle.info) yield from update_ui(chatbot=chatbot, history=[]) if not newbing_handle.success: diff --git a/request_llm/edge_gpt.py b/request_llm/edge_gpt.py new file mode 100644 index 0000000..b896b94 --- /dev/null +++ b/request_llm/edge_gpt.py @@ -0,0 +1,406 @@ +""" +======================================================================== +第一部分:来自EdgeGPT.py +https://github.com/acheong08/EdgeGPT +======================================================================== +""" + +import argparse +import asyncio +import json +import os +import random +import re +import ssl +import sys +import uuid +from enum import Enum +from typing import Generator +from typing import Literal +from typing import Optional +from typing import Union +import certifi +import httpx +import websockets.client as websockets + +DELIMITER = "\x1e" + + +# Generate random IP between range 13.104.0.0/14 +FORWARDED_IP = ( + f"13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}" +) + +HEADERS = { + "accept": "application/json", + "accept-language": "en-US,en;q=0.9", + "content-type": "application/json", + "sec-ch-ua": '"Not_A Brand";v="99", "Microsoft Edge";v="110", "Chromium";v="110"', + "sec-ch-ua-arch": '"x86"', + "sec-ch-ua-bitness": '"64"', + "sec-ch-ua-full-version": '"109.0.1518.78"', + "sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-model": "", + "sec-ch-ua-platform": '"Windows"', + "sec-ch-ua-platform-version": '"15.0.0"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-origin", + "x-ms-client-request-id": str(uuid.uuid4()), + "x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32", + "Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx", + "Referrer-Policy": "origin-when-cross-origin", + "x-forwarded-for": FORWARDED_IP, +} + +HEADERS_INIT_CONVER = { + "authority": "edgeservices.bing.com", + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", + "accept-language": "en-US,en;q=0.9", + "cache-control": "max-age=0", + "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"', + "sec-ch-ua-arch": '"x86"', + "sec-ch-ua-bitness": '"64"', + "sec-ch-ua-full-version": '"110.0.1587.69"', + "sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-model": '""', + "sec-ch-ua-platform": '"Windows"', + "sec-ch-ua-platform-version": '"15.0.0"', + "sec-fetch-dest": "document", + "sec-fetch-mode": "navigate", + "sec-fetch-site": "none", + "sec-fetch-user": "?1", + "upgrade-insecure-requests": "1", + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69", + "x-edge-shopping-flag": "1", + "x-forwarded-for": FORWARDED_IP, +} + +ssl_context = ssl.create_default_context() +ssl_context.load_verify_locations(certifi.where()) + + +class NotAllowedToAccess(Exception): + pass + + +class ConversationStyle(Enum): + creative = "h3imaginative,clgalileo,gencontentv3" + balanced = "galileo" + precise = "h3precise,clgalileo" + + +CONVERSATION_STYLE_TYPE = Optional[ + Union[ConversationStyle, Literal["creative", "balanced", "precise"]] +] + + +def _append_identifier(msg: dict) -> str: + """ + Appends special character to end of message to identify end of message + """ + # Convert dict to json string + return json.dumps(msg) + DELIMITER + + +def _get_ran_hex(length: int = 32) -> str: + """ + Returns random hex string + """ + return "".join(random.choice("0123456789abcdef") for _ in range(length)) + + +class _ChatHubRequest: + """ + Request object for ChatHub + """ + + def __init__( + self, + conversation_signature: str, + client_id: str, + conversation_id: str, + invocation_id: int = 0, + ) -> None: + self.struct: dict = {} + + self.client_id: str = client_id + self.conversation_id: str = conversation_id + self.conversation_signature: str = conversation_signature + self.invocation_id: int = invocation_id + + def update( + self, + prompt, + conversation_style, + options, + ) -> None: + """ + Updates request object + """ + if options is None: + options = [ + "deepleo", + "enable_debug_commands", + "disable_emoji_spoken_text", + "enablemm", + ] + if conversation_style: + if not isinstance(conversation_style, ConversationStyle): + conversation_style = getattr(ConversationStyle, conversation_style) + options = [ + "nlu_direct_response_filter", + "deepleo", + "disable_emoji_spoken_text", + "responsible_ai_policy_235", + "enablemm", + conversation_style.value, + "dtappid", + "cricinfo", + "cricinfov2", + "dv3sugg", + ] + self.struct = { + "arguments": [ + { + "source": "cib", + "optionsSets": options, + "sliceIds": [ + "222dtappid", + "225cricinfo", + "224locals0", + ], + "traceId": _get_ran_hex(32), + "isStartOfSession": self.invocation_id == 0, + "message": { + "author": "user", + "inputMethod": "Keyboard", + "text": prompt, + "messageType": "Chat", + }, + "conversationSignature": self.conversation_signature, + "participant": { + "id": self.client_id, + }, + "conversationId": self.conversation_id, + }, + ], + "invocationId": str(self.invocation_id), + "target": "chat", + "type": 4, + } + self.invocation_id += 1 + + +class _Conversation: + """ + Conversation API + """ + + def __init__( + self, + cookies, + proxy, + ) -> None: + self.struct: dict = { + "conversationId": None, + "clientId": None, + "conversationSignature": None, + "result": {"value": "Success", "message": None}, + } + self.proxy = proxy + proxy = ( + proxy + or os.environ.get("all_proxy") + or os.environ.get("ALL_PROXY") + or os.environ.get("https_proxy") + or os.environ.get("HTTPS_PROXY") + or None + ) + if proxy is not None and proxy.startswith("socks5h://"): + proxy = "socks5://" + proxy[len("socks5h://") :] + self.session = httpx.Client( + proxies=proxy, + timeout=30, + headers=HEADERS_INIT_CONVER, + ) + for cookie in cookies: + self.session.cookies.set(cookie["name"], cookie["value"]) + + # Send GET request + response = self.session.get( + url=os.environ.get("BING_PROXY_URL") + or "https://edgeservices.bing.com/edgesvc/turing/conversation/create", + ) + if response.status_code != 200: + response = self.session.get( + "https://edge.churchless.tech/edgesvc/turing/conversation/create", + ) + if response.status_code != 200: + print(f"Status code: {response.status_code}") + print(response.text) + print(response.url) + raise Exception("Authentication failed") + try: + self.struct = response.json() + except (json.decoder.JSONDecodeError, NotAllowedToAccess) as exc: + raise Exception( + "Authentication failed. You have not been accepted into the beta.", + ) from exc + if self.struct["result"]["value"] == "UnauthorizedRequest": + raise NotAllowedToAccess(self.struct["result"]["message"]) + + +class _ChatHub: + """ + Chat API + """ + + def __init__(self, conversation) -> None: + self.wss = None + self.request: _ChatHubRequest + self.loop: bool + self.task: asyncio.Task + print(conversation.struct) + self.request = _ChatHubRequest( + conversation_signature=conversation.struct["conversationSignature"], + client_id=conversation.struct["clientId"], + conversation_id=conversation.struct["conversationId"], + ) + + async def ask_stream( + self, + prompt: str, + wss_link: str, + conversation_style: CONVERSATION_STYLE_TYPE = None, + raw: bool = False, + options: dict = None, + ) -> Generator[str, None, None]: + """ + Ask a question to the bot + """ + if self.wss and not self.wss.closed: + await self.wss.close() + # Check if websocket is closed + self.wss = await websockets.connect( + wss_link, + extra_headers=HEADERS, + max_size=None, + ssl=ssl_context, + ) + await self._initial_handshake() + # Construct a ChatHub request + self.request.update( + prompt=prompt, + conversation_style=conversation_style, + options=options, + ) + # Send request + await self.wss.send(_append_identifier(self.request.struct)) + final = False + while not final: + objects = str(await self.wss.recv()).split(DELIMITER) + for obj in objects: + if obj is None or not obj: + continue + response = json.loads(obj) + if response.get("type") != 2 and raw: + yield False, response + elif response.get("type") == 1 and response["arguments"][0].get( + "messages", + ): + resp_txt = response["arguments"][0]["messages"][0]["adaptiveCards"][ + 0 + ]["body"][0].get("text") + yield False, resp_txt + elif response.get("type") == 2: + final = True + yield True, response + + async def _initial_handshake(self) -> None: + await self.wss.send(_append_identifier({"protocol": "json", "version": 1})) + await self.wss.recv() + + async def close(self) -> None: + """ + Close the connection + """ + if self.wss and not self.wss.closed: + await self.wss.close() + + +class NewbingChatbot: + """ + Combines everything to make it seamless + """ + + def __init__( + self, + cookies, + proxy + ) -> None: + if cookies is None: + cookies = {} + self.cookies = cookies + self.proxy = proxy + self.chat_hub: _ChatHub = _ChatHub( + _Conversation(self.cookies, self.proxy), + ) + + async def ask( + self, + prompt: str, + wss_link: str, + conversation_style: CONVERSATION_STYLE_TYPE = None, + options: dict = None, + ) -> dict: + """ + Ask a question to the bot + """ + async for final, response in self.chat_hub.ask_stream( + prompt=prompt, + conversation_style=conversation_style, + wss_link=wss_link, + options=options, + ): + if final: + return response + await self.chat_hub.wss.close() + return None + + async def ask_stream( + self, + prompt: str, + wss_link: str, + conversation_style: CONVERSATION_STYLE_TYPE = None, + raw: bool = False, + options: dict = None, + ) -> Generator[str, None, None]: + """ + Ask a question to the bot + """ + async for response in self.chat_hub.ask_stream( + prompt=prompt, + conversation_style=conversation_style, + wss_link=wss_link, + raw=raw, + options=options, + ): + yield response + + async def close(self) -> None: + """ + Close the connection + """ + await self.chat_hub.close() + + async def reset(self) -> None: + """ + Reset the conversation + """ + await self.close() + self.chat_hub = _ChatHub(_Conversation(self.cookies, self.proxy)) + + From d98d0a291e34a3f05029210fe377499b0a6ceefa Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:34:13 +0800 Subject: [PATCH 18/66] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 79557ee..e65f6d0 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -21,6 +21,19 @@ import threading from toolbox import update_ui, get_conf, trimmed_format_exc from multiprocessing import Process, Pipe +def preprocess_newbing_out(s): + pattern = r'\^(\d+)\^' # 匹配^数字^ + sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值 + result = re.sub(pattern, sub, s) # 替换操作 + if '[1]' in result: + result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' + return result + +def preprocess_newbing_out_simple(result): + if '[1]' in result: + result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' + return result + class NewBingHandle(Process): def __init__(self): super().__init__(daemon=True) @@ -159,20 +172,6 @@ class NewBingHandle(Process): else: yield res # newbing回复的片段 self.threadLock.release() - - -def preprocess_newbing_out(s): - pattern = r'\^(\d+)\^' # 匹配^数字^ - sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值 - result = re.sub(pattern, sub, s) # 替换操作 - if '[1]' in result: - result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' - return result - -def preprocess_newbing_out_simple(result): - if '[1]' in result: - result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n' - return result """ From 3814c3a915fb240e4a55beb1015a8c212cf0d65d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:36:55 +0800 Subject: [PATCH 19/66] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 1cee117..43b0015 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ gradio==3.25.0 tiktoken>=0.3.3 requests[socks] +httpx[socks] transformers python-markdown-math beautifulsoup4 @@ -11,6 +12,7 @@ colorama Markdown pygments pymupdf +certifi openai numpy arxiv From 59bed52fafae1c374313bd1b814e11f86199c855 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:39:54 +0800 Subject: [PATCH 20/66] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E7=9A=84=E5=BC=95=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 2 +- request_llm/edge_gpt.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index e65f6d0..c373c7d 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -49,7 +49,7 @@ class NewBingHandle(Process): def check_dependency(self): try: self.success = False - import rich + import certifi, httpx, rich self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口(有线程锁),否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。" self.success = True except: diff --git a/request_llm/edge_gpt.py b/request_llm/edge_gpt.py index b896b94..bbf8400 100644 --- a/request_llm/edge_gpt.py +++ b/request_llm/edge_gpt.py @@ -19,8 +19,6 @@ from typing import Generator from typing import Literal from typing import Optional from typing import Union -import certifi -import httpx import websockets.client as websockets DELIMITER = "\x1e" @@ -78,8 +76,12 @@ HEADERS_INIT_CONVER = { "x-forwarded-for": FORWARDED_IP, } -ssl_context = ssl.create_default_context() -ssl_context.load_verify_locations(certifi.where()) +def get_ssl_context(): + import certifi + ssl_context = ssl.create_default_context() + ssl_context.load_verify_locations(certifi.where()) + return ssl_context + class NotAllowedToAccess(Exception): @@ -210,6 +212,7 @@ class _Conversation: "conversationSignature": None, "result": {"value": "Success", "message": None}, } + import httpx self.proxy = proxy proxy = ( proxy @@ -288,7 +291,7 @@ class _ChatHub: wss_link, extra_headers=HEADERS, max_size=None, - ssl=ssl_context, + ssl=get_ssl_context() ) await self._initial_handshake() # Construct a ChatHub request From 97443d1f836cd484e086dc637c416ad15a36df85 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:40:18 +0800 Subject: [PATCH 21/66] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 43b0015..1cee117 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,6 @@ gradio==3.25.0 tiktoken>=0.3.3 requests[socks] -httpx[socks] transformers python-markdown-math beautifulsoup4 @@ -12,7 +11,6 @@ colorama Markdown pygments pymupdf -certifi openai numpy arxiv From df97213d3b197dbbe6bd6882461d56267e3e812d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:43:07 +0800 Subject: [PATCH 22/66] version 3.3 --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index a1e8bc9..076c2e7 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.3, "show_feature": true, - "new_feature": "保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)" + "new_feature": "支持NewBing !! <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)" } From ed8db8c8ae03845da737e1d496ad26cee9b69428 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 23 Apr 2023 23:49:55 +0800 Subject: [PATCH 23/66] README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d4e8539..aac762e 100644 --- a/README.md +++ b/README.md @@ -20,16 +20,15 @@ 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://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/pdf论文全文并生成摘要 Latex全文翻译、润色 | [函数插件] 一键翻译或润色latex论文 -批量注释生成 | [函数插件] 一键批量生成函数注释 -chat分析报告生成 | [函数插件] 运行后自动生成总结汇报 +生成GPT分析报告 | [函数插件] 运行后自动生成总结汇报,支持一键导出html格式对话记录 Markdown中英互译 | [函数插件] 看到上面5种语言的[README](https://github.com/binary-husky/chatgpt_academic/blob/master/docs/README_EN.md)了吗? [arxiv小助手](https://www.bilibili.com/video/BV1LM4y1279X) | [函数插件] 输入arxiv文章url即可一键翻译摘要+下载PDF [PDF论文全文翻译功能](https://www.bilibili.com/video/BV1KT411x7Wn) | [函数插件] PDF论文提取题目&摘要+翻译全文(多线程) @@ -38,6 +37,7 @@ Markdown中英互译 | [函数插件] 看到上面5种语言的[README](https:// 多线程函数插件支持 | 支持多线调用chatgpt,一键处理海量文本或程序 启动暗色gradio[主题](https://github.com/binary-husky/chatgpt_academic/issues/173) | 在浏览器url后面添加```/?__dark-theme=true```可以切换dark主题 [多LLM模型](https://www.bilibili.com/video/BV1wT411p7yf)支持,[API2D](https://api2d.com/)接口支持 | 同时被GPT3.5、GPT4和[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)伺候的感觉一定会很不错吧? +更多LLM模型接入 | 新加入Newbing测试接口(新必应AI) huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/gpt-academic) | 登陆huggingface后复制[此空间](https://huggingface.co/spaces/qingxu98/gpt-academic) …… | …… From e7b73f30416f6fd8cc90c005d19bf1a1fd122a00 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 24 Apr 2023 00:43:57 +0800 Subject: [PATCH 24/66] update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aac762e..bfcf6f9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ Latex全文翻译、润色 | [函数插件] 一键翻译或润色latex论文 Markdown中英互译 | [函数插件] 看到上面5种语言的[README](https://github.com/binary-husky/chatgpt_academic/blob/master/docs/README_EN.md)了吗? [arxiv小助手](https://www.bilibili.com/video/BV1LM4y1279X) | [函数插件] 输入arxiv文章url即可一键翻译摘要+下载PDF [PDF论文全文翻译功能](https://www.bilibili.com/video/BV1KT411x7Wn) | [函数插件] PDF论文提取题目&摘要+翻译全文(多线程) -[谷歌学术统合小助手](https://www.bilibili.com/video/BV19L411U7ia) | [函数插件] 给定任意谷歌学术搜索页面URL,让gpt帮你选择有趣的文章 +[谷歌学术统合小助手](https://www.bilibili.com/video/BV19L411U7ia) | [函数插件] 给定任意谷歌学术搜索页面的URL,让GPT帮你写Related Works +互联网信息聚合+GPT | [函数插件] 一键让ChatGPT先Google搜索,再回答问题,信息流永不过时 公式/图片/表格显示 | 可以同时显示公式的tex形式和渲染形式,支持公式、代码高亮 多线程函数插件支持 | 支持多线调用chatgpt,一键处理海量文本或程序 启动暗色gradio[主题](https://github.com/binary-husky/chatgpt_academic/issues/173) | 在浏览器url后面添加```/?__dark-theme=true```可以切换dark主题 From a097b6cd032cee3779af953b67b0260f5ad5ac6d Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 11:34:47 +0800 Subject: [PATCH 25/66] =?UTF-8?q?=E5=87=8F=E5=B0=91=E6=AF=8F=E6=AC=A1?= =?UTF-8?q?=E5=A4=84=E7=90=86=E7=9A=84=E8=AE=BA=E6=96=87=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/谷歌检索小助手.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/crazy_functions/谷歌检索小助手.py b/crazy_functions/谷歌检索小助手.py index 786b266..2e2d3d1 100644 --- a/crazy_functions/谷歌检索小助手.py +++ b/crazy_functions/谷歌检索小助手.py @@ -82,21 +82,22 @@ def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst history = [] meta_paper_info_list = yield from get_meta_information(txt, chatbot, history) + batchsize = 7 + for batch in range(len(meta_paper_info_list)//batchsize): + if len(meta_paper_info_list[:batchsize]) > 0: + i_say = "下面是一些学术文献的数据,提取出以下内容,使用Markdown格式输出:" + \ + "1、英文题目;2、中文题目翻译;3、作者;4、arxiv公开(is_paper_in_arxiv);4、引用数量(cite);5、中文摘要翻译。" + \ + f"以下是信息源:{str(meta_paper_info_list[:batchsize])}" - if len(meta_paper_info_list[:10]) > 0: - i_say = "下面是一些学术文献的数据,请从中提取出以下内容。" + \ - "1、英文题目;2、中文题目翻译;3、作者;4、arxiv公开(is_paper_in_arxiv);4、引用数量(cite);5、中文摘要翻译。" + \ - f"以下是信息源:{str(meta_paper_info_list[:10])}" + inputs_show_user = f"请分析此页面中出现的所有文章:{txt},这是第{batch+1}批" + gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( + inputs=i_say, inputs_show_user=inputs_show_user, + llm_kwargs=llm_kwargs, chatbot=chatbot, history=[], + sys_prompt="你是一个学术翻译,请从数据中提取信息。你必须使用Markdown格式。你必须逐个文献进行处理。" + ) - inputs_show_user = f"请分析此页面中出现的所有文章:{txt}" - gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( - inputs=i_say, inputs_show_user=inputs_show_user, - llm_kwargs=llm_kwargs, chatbot=chatbot, history=[], - sys_prompt="你是一个学术翻译,请从数据中提取信息。你必须使用Markdown格式。你必须逐个文献进行处理。" - ) - - history.extend([ "第一批", gpt_say ]) - meta_paper_info_list = meta_paper_info_list[10:] + history.extend([ f"第{batch+1}批", gpt_say ]) + meta_paper_info_list = meta_paper_info_list[batchsize:] chatbot.append(["状态?", "已经全部完成,您可以试试让AI写一个Related Works,例如您可以继续输入Write a \"Related Works\" section about \"你搜索的研究领域\" for me."]) From f04683732ea8eb8fd80f6d16c642ad237c5645bc Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 11:39:40 +0800 Subject: [PATCH 26/66] =?UTF-8?q?=E5=BE=85=E8=B0=83=E6=9F=A5=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index c373c7d..232eb6b 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -239,6 +239,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp history_feedin.append([history[2*i], history[2*i+1]] ) chatbot[-1] = (inputs, "[Local Message]: 等待NewBing响应中 ...") + response = "[Local Message]: 等待NewBing响应中 ..." yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=system_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): chatbot[-1] = (inputs, preprocess_newbing_out(response)) From d1efbd26da8c08f20cda792bf15ac685e461541a Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 11:48:39 +0800 Subject: [PATCH 27/66] =?UTF-8?q?=E4=BF=AE=E6=AD=A3prompt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/谷歌检索小助手.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crazy_functions/谷歌检索小助手.py b/crazy_functions/谷歌检索小助手.py index 2e2d3d1..11a8da5 100644 --- a/crazy_functions/谷歌检索小助手.py +++ b/crazy_functions/谷歌检索小助手.py @@ -85,7 +85,7 @@ def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst batchsize = 7 for batch in range(len(meta_paper_info_list)//batchsize): if len(meta_paper_info_list[:batchsize]) > 0: - i_say = "下面是一些学术文献的数据,提取出以下内容,使用Markdown格式输出:" + \ + i_say = "下面是一些学术文献的数据,提取出以下内容:" + \ "1、英文题目;2、中文题目翻译;3、作者;4、arxiv公开(is_paper_in_arxiv);4、引用数量(cite);5、中文摘要翻译。" + \ f"以下是信息源:{str(meta_paper_info_list[:batchsize])}" @@ -93,7 +93,7 @@ def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=i_say, inputs_show_user=inputs_show_user, llm_kwargs=llm_kwargs, chatbot=chatbot, history=[], - sys_prompt="你是一个学术翻译,请从数据中提取信息。你必须使用Markdown格式。你必须逐个文献进行处理。" + sys_prompt="你是一个学术翻译,请从数据中提取信息。你必须使用Markdown表格。你必须逐个文献进行处理。" ) history.extend([ f"第{batch+1}批", gpt_say ]) From 6595ab813e4de8554a1eeef46e2e32429e4db0a4 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 11:54:15 +0800 Subject: [PATCH 28/66] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/谷歌检索小助手.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crazy_functions/谷歌检索小助手.py b/crazy_functions/谷歌检索小助手.py index 11a8da5..b9e1f8e 100644 --- a/crazy_functions/谷歌检索小助手.py +++ b/crazy_functions/谷歌检索小助手.py @@ -70,6 +70,7 @@ def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst # 尝试导入依赖,如果缺少依赖,则给出安装建议 try: import arxiv + import math from bs4 import BeautifulSoup except: report_execption(chatbot, history, @@ -80,10 +81,9 @@ def 谷歌检索小助手(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst # 清空历史,以免输入溢出 history = [] - meta_paper_info_list = yield from get_meta_information(txt, chatbot, history) - batchsize = 7 - for batch in range(len(meta_paper_info_list)//batchsize): + batchsize = 5 + for batch in range(math.ceil(len(meta_paper_info_list)/batchsize)): if len(meta_paper_info_list[:batchsize]) > 0: i_say = "下面是一些学术文献的数据,提取出以下内容:" + \ "1、英文题目;2、中文题目翻译;3、作者;4、arxiv公开(is_paper_in_arxiv);4、引用数量(cite);5、中文摘要翻译。" + \ From b3e5cdb3a54b801e2df1186aa62b98740eeecf83 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 12:08:42 +0800 Subject: [PATCH 29/66] =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=BA=9B=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_chatglm.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/request_llm/bridge_chatglm.py b/request_llm/bridge_chatglm.py index 8eef322..fb44043 100644 --- a/request_llm/bridge_chatglm.py +++ b/request_llm/bridge_chatglm.py @@ -32,6 +32,7 @@ class GetGLMHandle(Process): return self.chatglm_model is not None def run(self): + # 子进程执行 # 第一次运行,加载参数 retry = 0 while True: @@ -53,17 +54,24 @@ class GetGLMHandle(Process): self.child.send('[Local Message] Call ChatGLM fail 不能正常加载ChatGLM的参数。') raise RuntimeError("不能正常加载ChatGLM的参数!") - # 进入任务等待状态 while True: + # 进入任务等待状态 kwargs = self.child.recv() + # 收到消息,开始请求 try: for response, history in self.chatglm_model.stream_chat(self.chatglm_tokenizer, **kwargs): self.child.send(response) + # # 中途接收可能的终止指令(如果有的话) + # if self.child.poll(): + # command = self.child.recv() + # if command == '[Terminate]': break 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() @@ -130,14 +138,17 @@ 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 = [] history_feedin.append(["What can I do?", system_prompt] ) for i in range(len(history)//2): history_feedin.append([history[2*i], history[2*i+1]] ) + # 开始接收chatglm的回复 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) + # 总结输出 history.extend([inputs, response]) - yield from update_ui(chatbot=chatbot, history=history) \ No newline at end of file + yield from update_ui(chatbot=chatbot, history=history) From 71590426f9f346caab309304df5be9bda6dde7bc Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:16:49 +0800 Subject: [PATCH 30/66] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b40282a..2163586 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ - +> **Note** +> +> 本项目依赖的Gradio组件的最新版pip包有bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 +> +> `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` +> # ChatGPT 学术优化 From ddb6c90a8fbaccd9100e24983ffb7dce849d0091 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:17:04 +0800 Subject: [PATCH 31/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2163586..f6c3c0e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ > **Note** > -> 本项目依赖的Gradio组件的最新版pip包有bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 +> 本项目依赖的Gradio组件的最新版pip包有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 > > `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` > From 09c413a2729b1ea521c17d870da539e49ec4c534 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:17:58 +0800 Subject: [PATCH 32/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6c3c0e..3e2b7cb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ > **Note** > -> 本项目依赖的Gradio组件的最新版pip包有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 +> 本项目依赖的Gradio组件的最新版pip包有严重bug需要降级使用。所以,请在安装时严格选择requirements.txt中**指定的版本**。 > > `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` > From 4451770061f0f61a5b8e314733e5091b01ee8d85 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:24:29 +0800 Subject: [PATCH 33/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3e2b7cb..9ca6411 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ > **Note** > -> 本项目依赖的Gradio组件的最新版pip包有严重bug需要降级使用。所以,请在安装时严格选择requirements.txt中**指定的版本**。 +> 本项目依赖的Gradio组件的较新版pip包(3.26~3.27)有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 > > `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` > From 75adcbffeb05d2a6ff89d29609a9ce6bc7e77ef3 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:24:46 +0800 Subject: [PATCH 34/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ca6411..1407260 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ > **Note** > -> 本项目依赖的Gradio组件的较新版pip包(3.26~3.27)有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 +> 本项目依赖的Gradio组件的较新版pip包(Gradio 3.26~3.27)有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 > > `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` > From 22bffdb737601976cde948369dc17f1a07d07ec6 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:25:10 +0800 Subject: [PATCH 35/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1407260..28fc7f8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ > **Note** > -> 本项目依赖的Gradio组件的较新版pip包(Gradio 3.26~3.27)有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 +> 本项目依赖的Gradio组件的新版pip包(Gradio 3.26~3.27)有严重bug。所以,请在安装时严格选择requirements.txt中**指定的版本**。 > > `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` > From 61379633558a07e5c822f794acdc8783c9d742b0 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 17:33:18 +0800 Subject: [PATCH 36/66] =?UTF-8?q?=E6=8B=AF=E6=95=91=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E7=81=BE=E9=9A=BE=E6=80=A7=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=85=8D=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- theme.py | 246 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 178 insertions(+), 68 deletions(-) diff --git a/theme.py b/theme.py index 1cc26b0..252a312 100644 --- a/theme.py +++ b/theme.py @@ -137,6 +137,15 @@ advanced_css = """ /* 行内代码的背景设为淡灰色,设定圆角和间距. */ .markdown-body code { + display: inline; + white-space: break-spaces; + border-radius: 6px; + margin: 0 2px 0 2px; + padding: .2em .4em .1em .4em; + background-color: rgba(13, 17, 23, 0.95); +} + +.dark .markdown-body code { display: inline; white-space: break-spaces; border-radius: 6px; @@ -144,8 +153,19 @@ advanced_css = """ padding: .2em .4em .1em .4em; background-color: rgba(175,184,193,0.2); } + /* 设定代码块的样式,包括背景颜色、内、外边距、圆角。 */ .markdown-body pre code { + display: block; + overflow: auto; + white-space: pre; + background-color: rgba(13, 17, 23, 0.95); + border-radius: 10px; + padding: 1em; + margin: 1em 2em 1em 0.5em; +} + +.dark .markdown-body pre code { display: block; overflow: auto; white-space: pre; @@ -160,72 +180,162 @@ advanced_css = """ if CODE_HIGHLIGHT: advanced_css += """ -.hll { background-color: #ffffcc } -.c { color: #3D7B7B; font-style: italic } /* Comment */ -.err { border: 1px solid #FF0000 } /* Error */ -.k { color: hsl(197, 94%, 51%); font-weight: bold } /* Keyword */ -.o { color: #666666 } /* Operator */ -.ch { color: #3D7B7B; font-style: italic } /* Comment.Hashbang */ -.cm { color: #3D7B7B; font-style: italic } /* Comment.Multiline */ -.cp { color: #9C6500 } /* Comment.Preproc */ -.cpf { color: #3D7B7B; font-style: italic } /* Comment.PreprocFile */ -.c1 { color: #3D7B7B; font-style: italic } /* Comment.Single */ -.cs { color: #3D7B7B; font-style: italic } /* Comment.Special */ -.gd { color: #A00000 } /* Generic.Deleted */ -.ge { font-style: italic } /* Generic.Emph */ -.gr { color: #E40000 } /* Generic.Error */ -.gh { color: #000080; font-weight: bold } /* Generic.Heading */ -.gi { color: #008400 } /* Generic.Inserted */ -.go { color: #717171 } /* Generic.Output */ -.gp { color: #000080; font-weight: bold } /* Generic.Prompt */ -.gs { font-weight: bold } /* Generic.Strong */ -.gu { color: #800080; font-weight: bold } /* Generic.Subheading */ -.gt { color: #a9dd00 } /* Generic.Traceback */ -.kc { color: #008000; font-weight: bold } /* Keyword.Constant */ -.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ -.kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ -.kp { color: #008000 } /* Keyword.Pseudo */ -.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ -.kt { color: #B00040 } /* Keyword.Type */ -.m { color: #666666 } /* Literal.Number */ -.s { color: #BA2121 } /* Literal.String */ -.na { color: #687822 } /* Name.Attribute */ -.nb { color: #e5f8c3 } /* Name.Builtin */ -.nc { color: #ffad65; font-weight: bold } /* Name.Class */ -.no { color: #880000 } /* Name.Constant */ -.nd { color: #AA22FF } /* Name.Decorator */ -.ni { color: #717171; font-weight: bold } /* Name.Entity */ -.ne { color: #CB3F38; font-weight: bold } /* Name.Exception */ -.nf { color: #f9f978 } /* Name.Function */ -.nl { color: #767600 } /* Name.Label */ -.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ -.nt { color: #008000; font-weight: bold } /* Name.Tag */ -.nv { color: #19177C } /* Name.Variable */ -.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ -.w { color: #bbbbbb } /* Text.Whitespace */ -.mb { color: #666666 } /* Literal.Number.Bin */ -.mf { color: #666666 } /* Literal.Number.Float */ -.mh { color: #666666 } /* Literal.Number.Hex */ -.mi { color: #666666 } /* Literal.Number.Integer */ -.mo { color: #666666 } /* Literal.Number.Oct */ -.sa { color: #BA2121 } /* Literal.String.Affix */ -.sb { color: #BA2121 } /* Literal.String.Backtick */ -.sc { color: #BA2121 } /* Literal.String.Char */ -.dl { color: #BA2121 } /* Literal.String.Delimiter */ -.sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ -.s2 { color: #2bf840 } /* Literal.String.Double */ -.se { color: #AA5D1F; font-weight: bold } /* Literal.String.Escape */ -.sh { color: #BA2121 } /* Literal.String.Heredoc */ -.si { color: #A45A77; font-weight: bold } /* Literal.String.Interpol */ -.sx { color: #008000 } /* Literal.String.Other */ -.sr { color: #A45A77 } /* Literal.String.Regex */ -.s1 { color: #BA2121 } /* Literal.String.Single */ -.ss { color: #19177C } /* Literal.String.Symbol */ -.bp { color: #008000 } /* Name.Builtin.Pseudo */ -.fm { color: #0000FF } /* Name.Function.Magic */ -.vc { color: #19177C } /* Name.Variable.Class */ -.vg { color: #19177C } /* Name.Variable.Global */ -.vi { color: #19177C } /* Name.Variable.Instance */ -.vm { color: #19177C } /* Name.Variable.Magic */ -.il { color: #666666 } /* Literal.Number.Integer.Long */ +.codehilite .hll { background-color: #6e7681 } +.codehilite .c { color: #8b949e; font-style: italic } /* Comment */ +.codehilite .err { color: #f85149 } /* Error */ +.codehilite .esc { color: #c9d1d9 } /* Escape */ +.codehilite .g { color: #c9d1d9 } /* Generic */ +.codehilite .k { color: #ff7b72 } /* Keyword */ +.codehilite .l { color: #a5d6ff } /* Literal */ +.codehilite .n { color: #c9d1d9 } /* Name */ +.codehilite .o { color: #ff7b72; font-weight: bold } /* Operator */ +.codehilite .x { color: #c9d1d9 } /* Other */ +.codehilite .p { color: #c9d1d9 } /* Punctuation */ +.codehilite .ch { color: #8b949e; font-style: italic } /* Comment.Hashbang */ +.codehilite .cm { color: #8b949e; font-style: italic } /* Comment.Multiline */ +.codehilite .cp { color: #8b949e; font-weight: bold; font-style: italic } /* Comment.Preproc */ +.codehilite .cpf { color: #8b949e; font-style: italic } /* Comment.PreprocFile */ +.codehilite .c1 { color: #8b949e; font-style: italic } /* Comment.Single */ +.codehilite .cs { color: #8b949e; font-weight: bold; font-style: italic } /* Comment.Special */ +.codehilite .gd { color: #ffa198; background-color: #490202 } /* Generic.Deleted */ +.codehilite .ge { color: #c9d1d9; font-style: italic } /* Generic.Emph */ +.codehilite .gr { color: #ffa198 } /* Generic.Error */ +.codehilite .gh { color: #79c0ff; font-weight: bold } /* Generic.Heading */ +.codehilite .gi { color: #56d364; background-color: #0f5323 } /* Generic.Inserted */ +.codehilite .go { color: #8b949e } /* Generic.Output */ +.codehilite .gp { color: #8b949e } /* Generic.Prompt */ +.codehilite .gs { color: #c9d1d9; font-weight: bold } /* Generic.Strong */ +.codehilite .gu { color: #79c0ff } /* Generic.Subheading */ +.codehilite .gt { color: #ff7b72 } /* Generic.Traceback */ +.codehilite .g-Underline { color: #c9d1d9; text-decoration: underline } /* Generic.Underline */ +.codehilite .kc { color: #79c0ff } /* Keyword.Constant */ +.codehilite .kd { color: #ff7b72 } /* Keyword.Declaration */ +.codehilite .kn { color: #ff7b72 } /* Keyword.Namespace */ +.codehilite .kp { color: #79c0ff } /* Keyword.Pseudo */ +.codehilite .kr { color: #ff7b72 } /* Keyword.Reserved */ +.codehilite .kt { color: #ff7b72 } /* Keyword.Type */ +.codehilite .ld { color: #79c0ff } /* Literal.Date */ +.codehilite .m { color: #a5d6ff } /* Literal.Number */ +.codehilite .s { color: #a5d6ff } /* Literal.String */ +.codehilite .na { color: #c9d1d9 } /* Name.Attribute */ +.codehilite .nb { color: #c9d1d9 } /* Name.Builtin */ +.codehilite .nc { color: #f0883e; font-weight: bold } /* Name.Class */ +.codehilite .no { color: #79c0ff; font-weight: bold } /* Name.Constant */ +.codehilite .nd { color: #d2a8ff; font-weight: bold } /* Name.Decorator */ +.codehilite .ni { color: #ffa657 } /* Name.Entity */ +.codehilite .ne { color: #f0883e; font-weight: bold } /* Name.Exception */ +.codehilite .nf { color: #d2a8ff; font-weight: bold } /* Name.Function */ +.codehilite .nl { color: #79c0ff; font-weight: bold } /* Name.Label */ +.codehilite .nn { color: #ff7b72 } /* Name.Namespace */ +.codehilite .nx { color: #c9d1d9 } /* Name.Other */ +.codehilite .py { color: #79c0ff } /* Name.Property */ +.codehilite .nt { color: #7ee787 } /* Name.Tag */ +.codehilite .nv { color: #79c0ff } /* Name.Variable */ +.codehilite .ow { color: #ff7b72; font-weight: bold } /* Operator.Word */ +.codehilite .pm { color: #c9d1d9 } /* Punctuation.Marker */ +.codehilite .w { color: #6e7681 } /* Text.Whitespace */ +.codehilite .mb { color: #a5d6ff } /* Literal.Number.Bin */ +.codehilite .mf { color: #a5d6ff } /* Literal.Number.Float */ +.codehilite .mh { color: #a5d6ff } /* Literal.Number.Hex */ +.codehilite .mi { color: #a5d6ff } /* Literal.Number.Integer */ +.codehilite .mo { color: #a5d6ff } /* Literal.Number.Oct */ +.codehilite .sa { color: #79c0ff } /* Literal.String.Affix */ +.codehilite .sb { color: #a5d6ff } /* Literal.String.Backtick */ +.codehilite .sc { color: #a5d6ff } /* Literal.String.Char */ +.codehilite .dl { color: #79c0ff } /* Literal.String.Delimiter */ +.codehilite .sd { color: #a5d6ff } /* Literal.String.Doc */ +.codehilite .s2 { color: #a5d6ff } /* Literal.String.Double */ +.codehilite .se { color: #79c0ff } /* Literal.String.Escape */ +.codehilite .sh { color: #79c0ff } /* Literal.String.Heredoc */ +.codehilite .si { color: #a5d6ff } /* Literal.String.Interpol */ +.codehilite .sx { color: #a5d6ff } /* Literal.String.Other */ +.codehilite .sr { color: #79c0ff } /* Literal.String.Regex */ +.codehilite .s1 { color: #a5d6ff } /* Literal.String.Single */ +.codehilite .ss { color: #a5d6ff } /* Literal.String.Symbol */ +.codehilite .bp { color: #c9d1d9 } /* Name.Builtin.Pseudo */ +.codehilite .fm { color: #d2a8ff; font-weight: bold } /* Name.Function.Magic */ +.codehilite .vc { color: #79c0ff } /* Name.Variable.Class */ +.codehilite .vg { color: #79c0ff } /* Name.Variable.Global */ +.codehilite .vi { color: #79c0ff } /* Name.Variable.Instance */ +.codehilite .vm { color: #79c0ff } /* Name.Variable.Magic */ +.codehilite .il { color: #a5d6ff } /* Literal.Number.Integer.Long */ + +.dark .codehilite .hll { background-color: #2C3B41 } +.dark .codehilite .c { color: #79d618; font-style: italic } /* Comment */ +.dark .codehilite .err { color: #FF5370 } /* Error */ +.dark .codehilite .esc { color: #89DDFF } /* Escape */ +.dark .codehilite .g { color: #EEFFFF } /* Generic */ +.dark .codehilite .k { color: #BB80B3 } /* Keyword */ +.dark .codehilite .l { color: #C3E88D } /* Literal */ +.dark .codehilite .n { color: #EEFFFF } /* Name */ +.dark .codehilite .o { color: #89DDFF } /* Operator */ +.dark .codehilite .p { color: #89DDFF } /* Punctuation */ +.dark .codehilite .ch { color: #79d618; font-style: italic } /* Comment.Hashbang */ +.dark .codehilite .cm { color: #79d618; font-style: italic } /* Comment.Multiline */ +.dark .codehilite .cp { color: #79d618; font-style: italic } /* Comment.Preproc */ +.dark .codehilite .cpf { color: #79d618; font-style: italic } /* Comment.PreprocFile */ +.dark .codehilite .c1 { color: #79d618; font-style: italic } /* Comment.Single */ +.dark .codehilite .cs { color: #79d618; font-style: italic } /* Comment.Special */ +.dark .codehilite .gd { color: #FF5370 } /* Generic.Deleted */ +.dark .codehilite .ge { color: #89DDFF } /* Generic.Emph */ +.dark .codehilite .gr { color: #FF5370 } /* Generic.Error */ +.dark .codehilite .gh { color: #C3E88D } /* Generic.Heading */ +.dark .codehilite .gi { color: #C3E88D } /* Generic.Inserted */ +.dark .codehilite .go { color: #79d618 } /* Generic.Output */ +.dark .codehilite .gp { color: #FFCB6B } /* Generic.Prompt */ +.dark .codehilite .gs { color: #FF5370 } /* Generic.Strong */ +.dark .codehilite .gu { color: #89DDFF } /* Generic.Subheading */ +.dark .codehilite .gt { color: #FF5370 } /* Generic.Traceback */ +.dark .codehilite .kc { color: #89DDFF } /* Keyword.Constant */ +.dark .codehilite .kd { color: #BB80B3 } /* Keyword.Declaration */ +.dark .codehilite .kn { color: #89DDFF; font-style: italic } /* Keyword.Namespace */ +.dark .codehilite .kp { color: #89DDFF } /* Keyword.Pseudo */ +.dark .codehilite .kr { color: #BB80B3 } /* Keyword.Reserved */ +.dark .codehilite .kt { color: #BB80B3 } /* Keyword.Type */ +.dark .codehilite .ld { color: #C3E88D } /* Literal.Date */ +.dark .codehilite .m { color: #F78C6C } /* Literal.Number */ +.dark .codehilite .s { color: #C3E88D } /* Literal.String */ +.dark .codehilite .na { color: #BB80B3 } /* Name.Attribute */ +.dark .codehilite .nb { color: #82AAFF } /* Name.Builtin */ +.dark .codehilite .nc { color: #FFCB6B } /* Name.Class */ +.dark .codehilite .no { color: #EEFFFF } /* Name.Constant */ +.dark .codehilite .nd { color: #82AAFF } /* Name.Decorator */ +.dark .codehilite .ni { color: #89DDFF } /* Name.Entity */ +.dark .codehilite .ne { color: #FFCB6B } /* Name.Exception */ +.dark .codehilite .nf { color: #82AAFF } /* Name.Function */ +.dark .codehilite .nl { color: #82AAFF } /* Name.Label */ +.dark .codehilite .nn { color: #FFCB6B } /* Name.Namespace */ +.dark .codehilite .nx { color: #EEFFFF } /* Name.Other */ +.dark .codehilite .py { color: #FFCB6B } /* Name.Property */ +.dark .codehilite .nt { color: #FF5370 } /* Name.Tag */ +.dark .codehilite .nv { color: #89DDFF } /* Name.Variable */ +.dark .codehilite .ow { color: #89DDFF; font-style: italic } /* Operator.Word */ +.dark .codehilite .pm { color: #89DDFF } /* Punctuation.Marker */ +.dark .codehilite .w { color: #EEFFFF } /* Text.Whitespace */ +.dark .codehilite .mb { color: #F78C6C } /* Literal.Number.Bin */ +.dark .codehilite .mf { color: #F78C6C } /* Literal.Number.Float */ +.dark .codehilite .mh { color: #F78C6C } /* Literal.Number.Hex */ +.dark .codehilite .mi { color: #F78C6C } /* Literal.Number.Integer */ +.dark .codehilite .mo { color: #F78C6C } /* Literal.Number.Oct */ +.dark .codehilite .sa { color: #BB80B3 } /* Literal.String.Affix */ +.dark .codehilite .sb { color: #C3E88D } /* Literal.String.Backtick */ +.dark .codehilite .sc { color: #C3E88D } /* Literal.String.Char */ +.dark .codehilite .dl { color: #EEFFFF } /* Literal.String.Delimiter */ +.dark .codehilite .sd { color: #79d618; font-style: italic } /* Literal.String.Doc */ +.dark .codehilite .s2 { color: #C3E88D } /* Literal.String.Double */ +.dark .codehilite .se { color: #EEFFFF } /* Literal.String.Escape */ +.dark .codehilite .sh { color: #C3E88D } /* Literal.String.Heredoc */ +.dark .codehilite .si { color: #89DDFF } /* Literal.String.Interpol */ +.dark .codehilite .sx { color: #C3E88D } /* Literal.String.Other */ +.dark .codehilite .sr { color: #89DDFF } /* Literal.String.Regex */ +.dark .codehilite .s1 { color: #C3E88D } /* Literal.String.Single */ +.dark .codehilite .ss { color: #89DDFF } /* Literal.String.Symbol */ +.dark .codehilite .bp { color: #89DDFF } /* Name.Builtin.Pseudo */ +.dark .codehilite .fm { color: #82AAFF } /* Name.Function.Magic */ +.dark .codehilite .vc { color: #89DDFF } /* Name.Variable.Class */ +.dark .codehilite .vg { color: #89DDFF } /* Name.Variable.Global */ +.dark .codehilite .vi { color: #89DDFF } /* Name.Variable.Instance */ +.dark .codehilite .vm { color: #82AAFF } /* Name.Variable.Magic */ +.dark .codehilite .il { color: #F78C6C } /* Literal.Number.Integer.Long */ + """ From 081acc64040a575752bc1995643df3177666d419 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 17:42:24 +0800 Subject: [PATCH 37/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- theme.py | 1 + 1 file changed, 1 insertion(+) diff --git a/theme.py b/theme.py index 252a312..5433124 100644 --- a/theme.py +++ b/theme.py @@ -143,6 +143,7 @@ advanced_css = """ margin: 0 2px 0 2px; padding: .2em .4em .1em .4em; background-color: rgba(13, 17, 23, 0.95); + color: #c9d1d9; } .dark .markdown-body code { From e4c4b28ddfeb1759660b454d6eb6c36848c8766b Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Mon, 24 Apr 2023 18:20:33 +0800 Subject: [PATCH 38/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 28fc7f8..0dac72a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ # ChatGPT 学术优化 -**如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发issue或者pull requests** +**如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发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)[日本語|](docs/README_JP.md)[Русский|](docs/README_RS.md)[Français](docs/README_FR.md) translated by this project itself. From 73ce471a0e0b47044fac627b37b57ec0f9bff49d Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 19:24:19 +0800 Subject: [PATCH 39/66] max_worker_limit --- 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 3412aa3..6af956f 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -172,7 +172,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( 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 + if max_workers <= 0: max_workers = 3 # 屏蔽掉 chatglm的多线程,可能会导致严重卡顿 if not (llm_kwargs['llm_model'].startswith('gpt-') or llm_kwargs['llm_model'].startswith('api2d-')): max_workers = 1 From b6d2766e59dc40d13adb14d1735f2a70917af4ba Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 19:54:28 +0800 Subject: [PATCH 40/66] =?UTF-8?q?=E6=94=B9=E5=96=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/解析项目源代码.py | 9 ++++++--- main.py | 3 --- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crazy_functions/解析项目源代码.py b/crazy_functions/解析项目源代码.py index bfa473a..49f41b1 100644 --- a/crazy_functions/解析项目源代码.py +++ b/crazy_functions/解析项目源代码.py @@ -1,5 +1,6 @@ from toolbox import update_ui from toolbox import CatchException, report_execption, write_results_to_file +from .crazy_utils import input_clipping def 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt): import os, copy @@ -61,13 +62,15 @@ def 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, previous_iteration_files.extend([os.path.relpath(fp, project_folder) for index, fp in enumerate(this_iteration_file_manifest)]) previous_iteration_files_string = ', '.join(previous_iteration_files) current_iteration_focus = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(this_iteration_file_manifest)]) - i_say = f'根据以上分析,对程序的整体功能和构架重新做出概括。然后用一张markdown表格整理每个文件的功能(包括{previous_iteration_files_string})。' + i_say = f'用一张Markdown表格简要描述以下文件的功能:{previous_iteration_files_string}。根据以上分析,用一句话概括程序的整体功能。' inputs_show_user = f'根据以上分析,对程序的整体功能和构架重新做出概括,由于输入长度限制,可能需要分组处理,本组文件为 {current_iteration_focus} + 已经汇总的文件组。' this_iteration_history = copy.deepcopy(this_iteration_gpt_response_collection) this_iteration_history.append(last_iteration_result) + # 裁剪input + inputs, this_iteration_history_feed = input_clipping(inputs=i_say, history=this_iteration_history, max_token_limit=2560) result = yield from request_gpt_model_in_new_thread_with_ui_alive( - inputs=i_say, inputs_show_user=inputs_show_user, llm_kwargs=llm_kwargs, chatbot=chatbot, - history=this_iteration_history, # 迭代之前的分析 + inputs=inputs, inputs_show_user=inputs_show_user, llm_kwargs=llm_kwargs, chatbot=chatbot, + history=this_iteration_history_feed, # 迭代之前的分析 sys_prompt="你是一个程序架构分析师,正在分析一个项目的源代码。") report_part_2.extend([i_say, result]) last_iteration_result = result diff --git a/main.py b/main.py index 55bd680..29ac825 100644 --- a/main.py +++ b/main.py @@ -173,9 +173,6 @@ def main(): 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) From 8e59412c47b49d8929a2c53fd332d0e933e1b688 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 20:14:23 +0800 Subject: [PATCH 41/66] =?UTF-8?q?=E4=BF=AE=E6=AD=A3newbing=E4=BA=A4?= =?UTF-8?q?=E4=BA=92=E7=9A=84=E4=B8=8D=E5=90=88=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 232eb6b..eb8c916 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -88,14 +88,14 @@ class NewBingHandle(Process): if a not in self.local_history: self.local_history.append(a) prompt += a + '\n' - if b not in self.local_history: - self.local_history.append(b) - prompt += b + '\n' + # if b not in self.local_history: + # self.local_history.append(b) + # prompt += b + '\n' # 问题 prompt += question self.local_history.append(question) - + print('question:', question) # 提交 async for final, response in self.newbing_model.ask_stream( prompt=question, @@ -108,7 +108,8 @@ class NewBingHandle(Process): else: print('-------- receive final ---------') self.child.send('[Finish]') - + # self.local_history.append(response) + def run(self): """ @@ -245,6 +246,6 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp chatbot[-1] = (inputs, preprocess_newbing_out(response)) yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") - history.extend([inputs, preprocess_newbing_out(response)]) + history.extend([inputs, response]) yield from update_ui(chatbot=chatbot, history=history, msg="完成全部响应,请提交新问题。") From 856df8fb6231adb16c57fb4bece22d9f24791fdb Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 20:18:32 +0800 Subject: [PATCH 42/66] =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_newbing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index eb8c916..05803cf 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -95,7 +95,7 @@ class NewBingHandle(Process): # 问题 prompt += question self.local_history.append(question) - print('question:', question) + print('question:', prompt) # 提交 async for final, response in self.newbing_model.ask_stream( prompt=question, From 5eea959103742498a552acc9fde72a90ea3f5cb2 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 20:51:34 +0800 Subject: [PATCH 43/66] =?UTF-8?q?Markdown=E7=BF=BB=E8=AF=91=E6=94=AF?= =?UTF-8?q?=E6=8C=81github=20url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functional.py | 15 ++++++++------- crazy_functions/批量Markdown翻译.py | 26 +++++++++++++++++++++++--- request_llm/bridge_newbing.py | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/crazy_functional.py b/crazy_functional.py index 8b8a394..db0af66 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -21,6 +21,7 @@ def get_crazy_functions(): from crazy_functions.总结word文档 import 总结word文档 from crazy_functions.解析JupyterNotebook import 解析ipynb文件 from crazy_functions.对话历史存档 import 对话历史存档 + from crazy_functions.批量Markdown翻译 import Markdown英译中 function_plugins = { "解析整个Python项目": { @@ -81,8 +82,14 @@ def get_crazy_functions(): "Color": "stop", # 按钮颜色 "Function": HotReload(读文章写摘要) }, + "Markdown/Readme英译中": { + # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效 + "Color": "stop", + "Function": HotReload(Markdown英译中) + }, "批量生成函数注释": { "Color": "stop", # 按钮颜色 + "AsButton": False, # 加入下拉菜单中 "Function": HotReload(批量生成函数注释) }, "[多线程Demo] 解析此项目本身(源码自译解)": { @@ -110,7 +117,6 @@ def get_crazy_functions(): from crazy_functions.Latex全文翻译 import Latex中译英 from crazy_functions.Latex全文翻译 import Latex英译中 from crazy_functions.批量Markdown翻译 import Markdown中译英 - from crazy_functions.批量Markdown翻译 import Markdown英译中 function_plugins.update({ "批量翻译PDF文档(多线程)": { @@ -175,12 +181,7 @@ def get_crazy_functions(): "AsButton": False, # 加入下拉菜单中 "Function": HotReload(Markdown中译英) }, - "[测试功能] 批量Markdown英译中(输入路径或上传压缩包)": { - # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效 - "Color": "stop", - "AsButton": False, # 加入下拉菜单中 - "Function": HotReload(Markdown英译中) - }, + }) diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index ee6a1a4..862da53 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -98,6 +98,7 @@ def Markdown英译中(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p # 尝试导入依赖,如果缺少依赖,则给出安装建议 try: import tiktoken + import glob, os except: report_execption(chatbot, history, a=f"解析项目: {txt}", @@ -105,19 +106,38 @@ def Markdown英译中(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return history = [] # 清空历史,以免输入溢出 - import glob, os - if os.path.exists(txt): + + if txt.startswith('http'): + # 网络的远程文件 + txt = txt.replace("https://github.com/", "https://raw.githubusercontent.com/") + txt = txt.replace("/blob/", "/") + import requests + from toolbox import get_conf + proxies, = get_conf('proxies') + r = requests.get(txt, proxies=proxies) + with open('./gpt_log/temp.md', 'wb+') as f: f.write(r.content) + project_folder = './gpt_log/' + file_manifest = ['./gpt_log/temp.md'] + elif txt.endswith('.md'): + # 直接给定文件 + file_manifest = [txt] + project_folder = os.path.dirname(txt) + elif os.path.exists(txt): + # 本地路径,递归搜索 project_folder = txt + file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.md', recursive=True)] else: + # 什么都没有 if txt == "": txt = '空空如也的输入栏' report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}") yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return - file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.md', recursive=True)] + if len(file_manifest) == 0: report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}") yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return + yield from 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, language='en->zh') diff --git a/request_llm/bridge_newbing.py b/request_llm/bridge_newbing.py index 05803cf..0c48752 100644 --- a/request_llm/bridge_newbing.py +++ b/request_llm/bridge_newbing.py @@ -245,7 +245,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp for response in newbing_handle.stream_chat(query=inputs, history=history_feedin, system_prompt=system_prompt, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']): chatbot[-1] = (inputs, preprocess_newbing_out(response)) yield from update_ui(chatbot=chatbot, history=history, msg="NewBing响应缓慢,尚未完成全部响应,请耐心完成后再提交新问题。") - + if response == "[Local Message]: 等待NewBing响应中 ...": response = "[Local Message]: NewBing响应异常,请刷新界面重试 ..." history.extend([inputs, response]) yield from update_ui(chatbot=chatbot, history=history, msg="完成全部响应,请提交新问题。") From 9a4b56277cb5899eda677d4dfeef4135751c039a Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 20:59:10 +0800 Subject: [PATCH 44/66] Function Refector --- crazy_functions/批量Markdown翻译.py | 49 +++++++++++++++++------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index 862da53..a6722ca 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -84,7 +84,33 @@ def 多文件翻译(file_manifest, project_folder, llm_kwargs, plugin_kwargs, ch yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 +def get_files_from_everything(txt): + import glob, os + success = True + if txt.startswith('http'): + # 网络的远程文件 + txt = txt.replace("https://github.com/", "https://raw.githubusercontent.com/") + txt = txt.replace("/blob/", "/") + import requests + from toolbox import get_conf + proxies, = get_conf('proxies') + r = requests.get(txt, proxies=proxies) + with open('./gpt_log/temp.md', 'wb+') as f: f.write(r.content) + project_folder = './gpt_log/' + file_manifest = ['./gpt_log/temp.md'] + elif txt.endswith('.md'): + # 直接给定文件 + file_manifest = [txt] + project_folder = os.path.dirname(txt) + elif os.path.exists(txt): + # 本地路径,递归搜索 + project_folder = txt + file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.md', recursive=True)] + else: + success = False + + return success, file_manifest, project_folder @CatchException @@ -107,26 +133,9 @@ def Markdown英译中(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p return history = [] # 清空历史,以免输入溢出 - if txt.startswith('http'): - # 网络的远程文件 - txt = txt.replace("https://github.com/", "https://raw.githubusercontent.com/") - txt = txt.replace("/blob/", "/") - import requests - from toolbox import get_conf - proxies, = get_conf('proxies') - r = requests.get(txt, proxies=proxies) - with open('./gpt_log/temp.md', 'wb+') as f: f.write(r.content) - project_folder = './gpt_log/' - file_manifest = ['./gpt_log/temp.md'] - elif txt.endswith('.md'): - # 直接给定文件 - file_manifest = [txt] - project_folder = os.path.dirname(txt) - elif os.path.exists(txt): - # 本地路径,递归搜索 - project_folder = txt - file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.md', recursive=True)] - else: + success, file_manifest, project_folder = get_files_from_everything(txt) + + if not success: # 什么都没有 if txt == "": txt = '空空如也的输入栏' report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}") From 4783fd6f37d2666c9dd5425e72262ecff059cf48 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 21:02:16 +0800 Subject: [PATCH 45/66] UP --- crazy_functions/批量Markdown翻译.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/crazy_functions/批量Markdown翻译.py b/crazy_functions/批量Markdown翻译.py index a6722ca..26f42ca 100644 --- a/crazy_functions/批量Markdown翻译.py +++ b/crazy_functions/批量Markdown翻译.py @@ -134,7 +134,7 @@ def Markdown英译中(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p history = [] # 清空历史,以免输入溢出 success, file_manifest, project_folder = get_files_from_everything(txt) - + if not success: # 什么都没有 if txt == "": txt = '空空如也的输入栏' @@ -164,6 +164,7 @@ def Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p # 尝试导入依赖,如果缺少依赖,则给出安装建议 try: import tiktoken + import glob, os except: report_execption(chatbot, history, a=f"解析项目: {txt}", @@ -171,18 +172,13 @@ def Markdown中译英(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_p yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return history = [] # 清空历史,以免输入溢出 - import glob, os - if os.path.exists(txt): - project_folder = txt - else: + success, file_manifest, project_folder = get_files_from_everything(txt) + if not success: + # 什么都没有 if txt == "": txt = '空空如也的输入栏' report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}") yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return - if txt.endswith('.md'): - file_manifest = [txt] - else: - file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.md', recursive=True)] if len(file_manifest) == 0: report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.md文件: {txt}") yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 From 5a83b3b096bfbd4c54a1999238528d9db8ca65e5 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Mon, 24 Apr 2023 21:10:01 +0800 Subject: [PATCH 46/66] version 3.3 --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index 076c2e7..7a4a4bd 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.3, "show_feature": true, - "new_feature": "支持NewBing !! <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)" + "new_feature": "支持NewBing <-> Markdown翻译功能支持直接输入Readme文件网址 <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)" } From 5500fbe6820ebad7d0e7ee8b9ae4baacba6d4176 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 15:49:57 +0800 Subject: [PATCH 47/66] Update README.md --- README.md | 93 ++----------------------------------------------------- 1 file changed, 2 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 973ffd5..2cc5646 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ chat分析报告生成 | [函数插件] 运行后自动生成总结汇报 启动暗色gradio[主题](https://github.com/binary-husky/chatgpt_academic/issues/173) | 在浏览器url后面添加```/?__dark-theme=true```可以切换dark主题 [多LLM模型](https://www.bilibili.com/video/BV1wT411p7yf)支持,[API2D](https://api2d.com/)接口支持 | 同时被GPT3.5、GPT4和[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)伺候的感觉一定会很不错吧? 更多LLM模型接入 | 新加入Newbing测试接口(新必应AI) -huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/gpt-academic) | 登陆huggingface后复制[此空间](https://huggingface.co/spaces/qingxu98/gpt-academic) …… | …… @@ -82,9 +81,6 @@ huggingface免科学上网[在线体验](https://huggingface.co/spaces/qingxu98/ -多种大语言模型混合调用[huggingface测试版](https://huggingface.co/spaces/qingxu98/academic-chatgpt-beta)(huggingface版不支持chatglm) - - --- ## 安装-方法1:直接运行 (Windows, Linux or MacOS) @@ -97,12 +93,7 @@ cd chatgpt_academic 2. 配置API_KEY和代理设置 -在`config.py`中,配置 海外Proxy 和 OpenAI API KEY,说明如下 -``` -1. 如果你在国内,需要设置海外代理才能够顺利使用OpenAI API,设置方法请仔细阅读config.py(1.修改其中的USE_PROXY为True; 2.按照说明修改其中的proxies)。 -2. 配置 OpenAI API KEY。支持任意数量的OpenAI的密钥和API2D的密钥共存/负载均衡,多个KEY用英文逗号分隔即可,例如输入 API_KEY="OpenAI密钥1,API2D密钥2,OpenAI密钥3,OpenAI密钥4" -3. 与代理网络有关的issue(网络超时、代理不起作用)汇总到 https://github.com/binary-husky/chatgpt_academic/issues/1 -``` +在`config.py`中,配置 Proxy 和 OpenAI API KEY,说明如下 (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。) @@ -130,14 +121,8 @@ python main.py 5. 测试函数插件 ``` -- 测试Python项目分析 - (选择1)input区域 输入 `./crazy_functions/test_project/python/dqn` , 然后点击 "解析整个Python项目" - (选择2)展开文件上传区,将python文件/包含python文件的压缩包拖拽进去,在出现反馈提示后, 然后点击 "解析整个Python项目" -- 测试自我代码解读(本项目自译解) - 点击 "[多线程Demo] 解析此项目本身(源码自译解)" - 测试函数插件模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能 点击 "[函数插件模板Demo] 历史上的今天" -- 函数插件区下拉菜单中有更多功能可供选择 ``` ## 安装-方法2:使用Docker @@ -171,7 +156,6 @@ docker run --rm -it --net=host --gpus=all gpt-academic docker run --rm -it --net=host --gpus=all gpt-academic bash ``` - ## 安装-方法3:其他部署方式(需要云服务器知识与经验) 1. 远程云服务器部署 @@ -183,14 +167,6 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash 3. 如何在二级网址(如`http://localhost/subpath`)下运行 请访问[FastAPI运行说明](docs/WithFastapi.md) -## 安装-代理配置 -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) - - --- ## 自定义新的便捷按钮 / 自定义函数插件 @@ -218,73 +194,8 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash 详情请参考[函数插件指南](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)。 ---- - -## 部分功能展示 - -1. 图片显示: - -
- -
- -2. 本项目的代码自译解(如果一个程序能够读懂并剖析自己): - -
- -
- -
- -
- -3. 其他任意Python/Cpp/Java/Go/Rect/...项目剖析: -
- -
- -
- -
- -4. Latex论文一键阅读理解与摘要生成 -
- -
- -5. 自动报告生成 -
- - - -
- -6. 模块化功能设计 -
- - -
- - -7. 源代码转译英文 - -
- -
- -8. 互联网在线信息综合 - -
- - - -
- - - -## Todo 与 版本规划: -- version 3.3+ (todo): NewBing支持 +## 版本: - version 3.2: 函数插件支持更多参数接口 (保存对话功能, 解读任意语言代码+同时询问任意的LLM组合) - version 3.1: 支持同时问询多个gpt模型!支持api2d,支持多个apikey负载均衡 - version 3.0: 对chatglm和其他小型llm的支持 From 7c9195ddd22908e558dc80d3d2d33fb594e7f1d9 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 15:50:35 +0800 Subject: [PATCH 48/66] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 2cc5646..68684b2 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,6 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash - version 2.0: 引入模块化函数插件 - version 1.0: 基础功能 -chatgpt_academic开发者QQ群:734063350 ## 参考与学习 From 48555f570c7f28b7546fed547c055c6c2443b6b5 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 16:11:00 +0800 Subject: [PATCH 49/66] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 68684b2..e731c38 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,6 @@ 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) | 支持代理连接OpenAI/Google等,秒解锁ChatGPT互联网[实时信息聚合](https://www.bilibili.com/video/BV1om4y127ck/)能力 模块化设计 | 支持自定义强大的[函数插件](https://github.com/binary-husky/chatgpt_academic/tree/master/crazy_functions),插件支持[热更新](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/...项目树 From 231c9c2e57438becd539dbdb14746a91c61de3c2 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 16:11:35 +0800 Subject: [PATCH 50/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e731c38..0a7b9cf 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ 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,说明如下 (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。) From e04946c8161e73c18780e0087315773c72f0cf9d Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 16:45:53 +0800 Subject: [PATCH 51/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a7b9cf..e665aa9 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ cd chatgpt_academic 2. 配置API_KEY -在`config.py`中,配置 Proxy 和 OpenAI API KEY,说明如下 +在`config.py`中,配置 [Proxy](https://github.com/binary-husky/gpt_academic/issues/1) 和 OpenAI API KEY,说明如下 (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。) From a0fa64de47da112537c80deb58eb3f06a9400f1e Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 16:46:36 +0800 Subject: [PATCH 52/66] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e665aa9..a0dd1ab 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ cd chatgpt_academic 2. 配置API_KEY -在`config.py`中,配置 [Proxy](https://github.com/binary-husky/gpt_academic/issues/1) 和 OpenAI API KEY,说明如下 +在`config.py`中,配置 [Proxy](https://github.com/binary-husky/gpt_academic/issues/1) 和 OpenAI API KEY。 + (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。) From e35eb9048e9f82c89a6faca4375a8bd077039e19 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 16:48:08 +0800 Subject: [PATCH 53/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a0dd1ab..ba4c109 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ python main.py # 下载项目 git clone https://github.com/binary-husky/chatgpt_academic.git cd chatgpt_academic -# 配置 “海外Proxy”, “API_KEY” 以及 “WEB_PORT” (例如50923) 等 +# 配置 “Proxy”, “API_KEY” 以及 “WEB_PORT” (例如50923) 等 用任意文本编辑器编辑 config.py # 安装 docker build -t gpt-academic . From 6538c58b8e5a4a7ae08dfa1ae9970bc422158096 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Tue, 25 Apr 2023 18:30:11 +0800 Subject: [PATCH 54/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba4c109..744798a 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ cd chatgpt_academic 2. 配置API_KEY -在`config.py`中,配置 [Proxy](https://github.com/binary-husky/gpt_academic/issues/1) 和 OpenAI API KEY。 +在`config.py`中,配置API KEY等[设置](https://github.com/binary-husky/gpt_academic/issues/1) 。 (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。) From c23db4b4f9bd2fe2e207894c25efbee98c5684da Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Wed, 26 Apr 2023 23:04:58 +0800 Subject: [PATCH 55/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 744798a..6be368d 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ chat分析报告生成 | [函数插件] 运行后自动生成总结汇报 [PDF论文全文翻译功能](https://www.bilibili.com/video/BV1KT411x7Wn) | [函数插件] PDF论文提取题目&摘要+翻译全文(多线程) [Arxiv小助手](https://www.bilibili.com/video/BV1LM4y1279X) | [函数插件] 输入arxiv文章url即可一键翻译摘要+下载PDF [谷歌学术统合小助手](https://www.bilibili.com/video/BV19L411U7ia) | [函数插件] 给定任意谷歌学术搜索页面URL,让gpt帮你[写relatedworks](https://www.bilibili.com/video/BV1GP411U7Az/) -互联网信息聚合+GPT | [函数插件] 一键让ChatGPT先Google搜索,再回答问题,信息流永不过时 +互联网信息聚合+GPT | [函数插件] 一键让GPT先从互联网获取信息,再回答问题,让信息永不过时 公式/图片/表格显示 | 可以同时显示公式的[tex形式和渲染形式](https://user-images.githubusercontent.com/96192199/230598842-1d7fcddd-815d-40ee-af60-baf488a199df.png),支持公式、代码高亮 多线程函数插件支持 | 支持多线调用chatgpt,一键处理[海量文本](https://www.bilibili.com/video/BV1FT411H7c5/)或程序 启动暗色gradio[主题](https://github.com/binary-husky/chatgpt_academic/issues/173) | 在浏览器url后面添加```/?__dark-theme=true```可以切换dark主题 From 1eb0174dfffc8a8cfbafc9a87af9f0af93f79d9d Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Thu, 27 Apr 2023 10:58:45 +0800 Subject: [PATCH 56/66] =?UTF-8?q?=E6=96=B0=E5=A2=9EDARK=5FMODE=E9=80=89?= =?UTF-8?q?=E9=A1=B9=EF=BC=8C=E5=8F=AF=E9=80=89=E6=8B=A9=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 1 + main.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index 688f35d..2901343 100644 --- a/config.py +++ b/config.py @@ -33,6 +33,7 @@ CODE_HIGHLIGHT = True # 窗口布局 LAYOUT = "LEFT-RIGHT" # "LEFT-RIGHT"(左右布局) # "TOP-DOWN"(上下布局) +DARK_MODE = True # "LEFT-RIGHT"(左右布局) # "TOP-DOWN"(上下布局) # 发送请求到OpenAI后,等待多久判定为超时 TIMEOUT_SECONDS = 30 diff --git a/main.py b/main.py index 29ac825..cb14776 100644 --- a/main.py +++ b/main.py @@ -186,7 +186,9 @@ def main(): 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") + DARK_MODE, = get_conf('DARK_MODE') + if DARK_MODE: webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") + else: webbrowser.open_new_tab(f"http://localhost:{PORT}") 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() From 34b767d1fd02298c7c08d6685fea34597b9d6703 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Thu, 27 Apr 2023 11:17:19 +0800 Subject: [PATCH 57/66] thread lock in chatglm --- request_llm/bridge_chatglm.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/request_llm/bridge_chatglm.py b/request_llm/bridge_chatglm.py index fb44043..7c86a22 100644 --- a/request_llm/bridge_chatglm.py +++ b/request_llm/bridge_chatglm.py @@ -1,6 +1,7 @@ from transformers import AutoModel, AutoTokenizer import time +import threading import importlib from toolbox import update_ui, get_conf from multiprocessing import Process, Pipe @@ -18,6 +19,7 @@ class GetGLMHandle(Process): self.success = True self.check_dependency() self.start() + self.threadLock = threading.Lock() def check_dependency(self): try: @@ -72,6 +74,7 @@ class GetGLMHandle(Process): def stream_chat(self, **kwargs): # 主进程执行 + self.threadLock.acquire() self.parent.send(kwargs) while True: res = self.parent.recv() @@ -79,7 +82,7 @@ class GetGLMHandle(Process): yield res else: break - return + self.threadLock.release() global glm_handle glm_handle = None @@ -145,10 +148,13 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp history_feedin.append([history[2*i], history[2*i+1]] ) # 开始接收chatglm的回复 + response = "[Local Message]: 等待ChatGLM响应中 ..." 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) # 总结输出 + if response == "[Local Message]: 等待ChatGLM响应中 ...": + response = "[Local Message]: ChatGLM响应异常 ..." history.extend([inputs, response]) yield from update_ui(chatbot=chatbot, history=history) From 69540d07c5ad75a41ad7f8d0a05ea596569dcf34 Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Thu, 27 Apr 2023 11:22:02 +0800 Subject: [PATCH 58/66] =?UTF-8?q?=E4=BF=AE=E6=94=B9dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/Dockerfile+ChatGLM | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/Dockerfile+ChatGLM b/docs/Dockerfile+ChatGLM index dafcee7..9631d4b 100644 --- a/docs/Dockerfile+ChatGLM +++ b/docs/Dockerfile+ChatGLM @@ -21,14 +21,15 @@ ARG useProxyNetwork=proxychains # use python3 as the system default python RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.8 - +# 下载pytorch +RUN $useProxyNetwork python3 -m pip install torch --extra-index-url https://download.pytorch.org/whl/cu113 # 下载分支 WORKDIR /gpt 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 -RUN $useProxyNetwork python3 -m pip install torch --extra-index-url https://download.pytorch.org/whl/cu113 +RUN $useProxyNetwork python3 -m pip install -r request_llm/requirements_newbing.txt # 预热CHATGLM参数(非必要 可选步骤) RUN echo ' \n\ From bb788b9259b0150ade81d93392359fe0eaf3ca22 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Thu, 27 Apr 2023 11:33:37 +0800 Subject: [PATCH 59/66] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 6be368d..05c1872 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,8 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash ## 版本: +- version 3.4(Todo): 完善chatglm本地大模型的多线支持 +- version 3.3: +互联网信息综合功能 - version 3.2: 函数插件支持更多参数接口 (保存对话功能, 解读任意语言代码+同时询问任意的LLM组合) - version 3.1: 支持同时问询多个gpt模型!支持api2d,支持多个apikey负载均衡 - version 3.0: 对chatglm和其他小型llm的支持 @@ -208,6 +210,8 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash - version 2.0: 引入模块化函数插件 - version 1.0: 基础功能 +gpt_academic开发者QQ群:734063350 + ## 参考与学习 From 24bb174b63286ab9dd7a0a338df54b9945c18b6b Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Thu, 27 Apr 2023 11:35:53 +0800 Subject: [PATCH 60/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 05c1872..da66f8a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ > `pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/` > -# ChatGPT 学术优化 +# GPT 学术优化 (ChatGPT Academic) **如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发pull requests** From c295bb4f047aca3213ba38c90bd7e90faf59ac4b Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Thu, 27 Apr 2023 20:01:36 +0800 Subject: [PATCH 61/66] =?UTF-8?q?ChatGLM=E5=8A=A0=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=94=81=E6=8F=90=E9=AB=98=E5=B9=B6=E5=8F=91=E7=A8=B3=E5=AE=9A?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- version | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version b/version index 7a4a4bd..a21ca72 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { - "version": 3.3, + "version": 3.31, "show_feature": true, - "new_feature": "支持NewBing <-> Markdown翻译功能支持直接输入Readme文件网址 <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM和GPT-4 <-> 改进架构,支持与多个LLM模型同时对话 <-> 添加支持API2D(国内,可支持gpt4)" + "new_feature": "ChatGLM加线程锁提高并发稳定性 <-> 支持NewBing <-> Markdown翻译功能支持直接输入Readme文件网址 <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM" } From 8c16cda3e8ac619b275557aa287d1ab7a5b4d8e8 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Thu, 27 Apr 2023 20:07:33 +0800 Subject: [PATCH 62/66] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index da66f8a..f7f8b9a 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ docker run --rm -it --net=host --gpus=all gpt-academic bash ## 版本: +- version 3.5(Todo): 使用自然语言调用本项目的所有函数插件(高优先级) - version 3.4(Todo): 完善chatglm本地大模型的多线支持 - version 3.3: +互联网信息综合功能 - version 3.2: 函数插件支持更多参数接口 (保存对话功能, 解读任意语言代码+同时询问任意的LLM组合) From 86b654d6be1e8657185b488dd3422a68f2c86c2a Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Thu, 27 Apr 2023 20:30:03 +0800 Subject: [PATCH 63/66] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7f8b9a..831ef71 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ cd chatgpt_academic 3. 安装依赖 ```sh -# (选择I: 如熟悉python)推荐 +# (选择I: 如熟悉python)(python版本3.9以上,越新越好) python -m pip install -r requirements.txt # 备注:使用官方pip源或者阿里pip源,其他pip源(如一些大学的pip)有可能出问题,临时换源方法:python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ From 984c7e9e12c6e54160af3200917b0e087d21b4cf Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 27 Apr 2023 21:11:15 +0800 Subject: [PATCH 64/66] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- check_proxy.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/check_proxy.py b/check_proxy.py index 28711a8..a8e4b72 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -67,7 +67,11 @@ def patch_and_restart(path): print亮黄('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,', '另外您可以随时在history子文件夹下找回旧版的程序。') shutil.copyfile('config.py', 'config_private.py') - distutils.dir_util.copy_tree(path+'/chatgpt_academic-master', './') + try: + distutils.dir_util.copy_tree(path+'/gpt_academic-master', './') + except: + from distutils import dir_util + dir_util.copy_tree(path+'/gpt_academic-master', './') import subprocess print亮绿('代码已经更新,即将更新pip包依赖……') for i in reversed(range(5)): time.sleep(1); print(i) From dfa31a8c167ab1a90a6a8d86f888daaf7c1c7276 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 27 Apr 2023 21:15:22 +0800 Subject: [PATCH 65/66] 3.31 --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index a21ca72..85277aa 100644 --- a/version +++ b/version @@ -1,5 +1,5 @@ { "version": 3.31, "show_feature": true, - "new_feature": "ChatGLM加线程锁提高并发稳定性 <-> 支持NewBing <-> Markdown翻译功能支持直接输入Readme文件网址 <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM" + "new_feature": "我们发现了自动更新模块的BUG,此次更新可能需要您手动到Github下载新版程序并覆盖 <-> ChatGLM加线程锁提高并发稳定性 <-> 支持NewBing <-> Markdown翻译功能支持直接输入Readme文件网址 <-> 保存对话功能 <-> 解读任意语言代码+同时询问任意的LLM组合 <-> 添加联网(Google)回答问题插件 <-> 修复ChatGLM上下文BUG <-> 添加支持清华ChatGLM" } From a2195120452cc7054d964de2bd3c371154cbf0a1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 27 Apr 2023 21:26:01 +0800 Subject: [PATCH 66/66] fix auto upgrade issue --- check_proxy.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/check_proxy.py b/check_proxy.py index a8e4b72..754b5d3 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -56,26 +56,24 @@ def patch_and_restart(path): """ 一键更新协议:覆盖和重启 """ - import distutils + from distutils import dir_util import shutil import os import sys import time + import glob from colorful import print亮黄, print亮绿, print亮红 # if not using config_private, move origin config.py as config_private.py if not os.path.exists('config_private.py'): print亮黄('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,', '另外您可以随时在history子文件夹下找回旧版的程序。') shutil.copyfile('config.py', 'config_private.py') - try: - distutils.dir_util.copy_tree(path+'/gpt_academic-master', './') - except: - from distutils import dir_util - dir_util.copy_tree(path+'/gpt_academic-master', './') - import subprocess + path_new_version = glob.glob(path + '/*-master')[0] + dir_util.copy_tree(path_new_version, './') print亮绿('代码已经更新,即将更新pip包依赖……') for i in reversed(range(5)): time.sleep(1); print(i) try: + import subprocess subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt']) except: print亮红('pip包依赖安装出现问题,需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。')