支持3.10以下的python版本使用newbing
This commit is contained in:
parent
518385dea2
commit
385c775aa5
@ -65,7 +65,7 @@ API_URL_REDIRECT = {}
|
|||||||
CUSTOM_PATH = "/"
|
CUSTOM_PATH = "/"
|
||||||
|
|
||||||
# 如果需要使用newbing,把newbing的长长的cookie放到这里
|
# 如果需要使用newbing,把newbing的长长的cookie放到这里
|
||||||
NEWBING_STYLE = "creative", # ["creative", "balanced", "precise"]
|
NEWBING_STYLE = "creative" # ["creative", "balanced", "precise"]
|
||||||
NEWBING_COOKIES = """
|
NEWBING_COOKIES = """
|
||||||
your bing cookies here
|
your bing cookies here
|
||||||
"""
|
"""
|
@ -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 argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
@ -28,13 +24,6 @@ from typing import Union
|
|||||||
import certifi
|
import certifi
|
||||||
import httpx
|
import httpx
|
||||||
import websockets.client as websockets
|
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"
|
DELIMITER = "\x1e"
|
||||||
|
|
||||||
@ -146,9 +135,9 @@ class _ChatHubRequest:
|
|||||||
|
|
||||||
def update(
|
def update(
|
||||||
self,
|
self,
|
||||||
prompt: str,
|
prompt,
|
||||||
conversation_style: CONVERSATION_STYLE_TYPE,
|
conversation_style,
|
||||||
options: list | None = None,
|
options,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Updates request object
|
Updates request object
|
||||||
@ -214,8 +203,8 @@ class _Conversation:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
cookies: dict,
|
cookies,
|
||||||
proxy: str | None = None,
|
proxy,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.struct: dict = {
|
self.struct: dict = {
|
||||||
"conversationId": None,
|
"conversationId": None,
|
||||||
@ -271,8 +260,8 @@ class _ChatHub:
|
|||||||
Chat API
|
Chat API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, conversation: _Conversation) -> None:
|
def __init__(self, conversation) -> None:
|
||||||
self.wss: websockets.WebSocketClientProtocol | None = None
|
self.wss = None
|
||||||
self.request: _ChatHubRequest
|
self.request: _ChatHubRequest
|
||||||
self.loop: bool
|
self.loop: bool
|
||||||
self.task: asyncio.Task
|
self.task: asyncio.Task
|
||||||
@ -351,21 +340,13 @@ class Chatbot:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
cookies: dict = None,
|
cookies,
|
||||||
proxy: str | None = None,
|
proxy
|
||||||
cookie_path: str = None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
if cookies is None:
|
if cookies is None:
|
||||||
cookies = {}
|
cookies = {}
|
||||||
if cookie_path is not None:
|
self.cookies = cookies
|
||||||
try:
|
self.proxy = proxy
|
||||||
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(
|
self.chat_hub: _ChatHub = _ChatHub(
|
||||||
_Conversation(self.cookies, self.proxy),
|
_Conversation(self.cookies, self.proxy),
|
||||||
)
|
)
|
||||||
@ -425,43 +406,6 @@ class Chatbot:
|
|||||||
self.chat_hub = _ChatHub(_Conversation(self.cookies))
|
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 = ""
|
load_message = ""
|
||||||
|
|
||||||
@ -470,7 +414,10 @@ 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):
|
class GetNewBingHandle(Process):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(daemon=True)
|
super().__init__(daemon=True)
|
||||||
@ -484,7 +431,6 @@ class GetNewBingHandle(Process):
|
|||||||
|
|
||||||
def check_dependency(self):
|
def check_dependency(self):
|
||||||
try:
|
try:
|
||||||
import rich
|
|
||||||
self.info = "依赖检测通过"
|
self.info = "依赖检测通过"
|
||||||
self.success = True
|
self.success = True
|
||||||
except:
|
except:
|
||||||
@ -538,6 +484,7 @@ class GetNewBingHandle(Process):
|
|||||||
try:
|
try:
|
||||||
asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history']))
|
asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history']))
|
||||||
except:
|
except:
|
||||||
|
tb_str = '```\n' + trimmed_format_exc() + '```'
|
||||||
self.child.send('[Local Message] Newbing失败.')
|
self.child.send('[Local Message] Newbing失败.')
|
||||||
self.child.send('[Finish]')
|
self.child.send('[Finish]')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user