Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	支持3.10以下的python版本使用newbing
Browse files- config.py +1 -1
- request_llm/bridge_newbing.py +17 -70
    	
        config.py
    CHANGED
    
    | @@ -65,7 +65,7 @@ API_URL_REDIRECT = {} | |
| 65 | 
             
            CUSTOM_PATH = "/"
         | 
| 66 |  | 
| 67 | 
             
            # 如果需要使用newbing,把newbing的长长的cookie放到这里
         | 
| 68 | 
            -
            NEWBING_STYLE = "creative" | 
| 69 | 
             
            NEWBING_COOKIES = """
         | 
| 70 | 
             
            your bing cookies here
         | 
| 71 | 
             
            """
         | 
|  | |
| 65 | 
             
            CUSTOM_PATH = "/"
         | 
| 66 |  | 
| 67 | 
             
            # 如果需要使用newbing,把newbing的长长的cookie放到这里
         | 
| 68 | 
            +
            NEWBING_STYLE = "creative"  # ["creative", "balanced", "precise"]
         | 
| 69 | 
             
            NEWBING_COOKIES = """
         | 
| 70 | 
             
            your bing cookies here
         | 
| 71 | 
             
            """
         | 
    	
        request_llm/bridge_newbing.py
    CHANGED
    
    | @@ -5,11 +5,7 @@ https://github.com/acheong08/EdgeGPT | |
| 5 | 
             
            ========================================================================
         | 
| 6 | 
             
            """
         | 
| 7 |  | 
| 8 | 
            -
             | 
| 9 | 
            -
            import time
         | 
| 10 | 
            -
            import importlib
         | 
| 11 | 
            -
            from toolbox import update_ui, get_conf
         | 
| 12 | 
            -
            from multiprocessing import Process, Pipe
         | 
| 13 | 
             
            import argparse
         | 
| 14 | 
             
            import asyncio
         | 
| 15 | 
             
            import json
         | 
| @@ -28,13 +24,6 @@ from typing import Union | |
| 28 | 
             
            import certifi
         | 
| 29 | 
             
            import httpx
         | 
| 30 | 
             
            import websockets.client as websockets
         | 
| 31 | 
            -
            from prompt_toolkit import PromptSession
         | 
| 32 | 
            -
            from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
         | 
| 33 | 
            -
            from prompt_toolkit.completion import WordCompleter
         | 
| 34 | 
            -
            from prompt_toolkit.history import InMemoryHistory
         | 
| 35 | 
            -
            from prompt_toolkit.key_binding import KeyBindings
         | 
| 36 | 
            -
            from rich.live import Live
         | 
| 37 | 
            -
            from rich.markdown import Markdown
         | 
| 38 |  | 
| 39 | 
             
            DELIMITER = "\x1e"
         | 
| 40 |  | 
| @@ -146,9 +135,9 @@ class _ChatHubRequest: | |
| 146 |  | 
| 147 | 
             
                def update(
         | 
| 148 | 
             
                    self,
         | 
| 149 | 
            -
                    prompt | 
| 150 | 
            -
                    conversation_style | 
| 151 | 
            -
                    options | 
| 152 | 
             
                ) -> None:
         | 
| 153 | 
             
                    """
         | 
| 154 | 
             
                    Updates request object
         | 
| @@ -214,8 +203,8 @@ class _Conversation: | |
| 214 |  | 
| 215 | 
             
                def __init__(
         | 
| 216 | 
             
                    self,
         | 
| 217 | 
            -
                    cookies | 
| 218 | 
            -
                    proxy | 
| 219 | 
             
                ) -> None:
         | 
| 220 | 
             
                    self.struct: dict = {
         | 
| 221 | 
             
                        "conversationId": None,
         | 
| @@ -271,8 +260,8 @@ class _ChatHub: | |
| 271 | 
             
                Chat API
         | 
| 272 | 
             
                """
         | 
| 273 |  | 
| 274 | 
            -
                def __init__(self, conversation | 
| 275 | 
            -
                    self.wss | 
| 276 | 
             
                    self.request: _ChatHubRequest
         | 
| 277 | 
             
                    self.loop: bool
         | 
| 278 | 
             
                    self.task: asyncio.Task
         | 
| @@ -351,21 +340,13 @@ class Chatbot: | |
| 351 |  | 
| 352 | 
             
                def __init__(
         | 
| 353 | 
             
                    self,
         | 
| 354 | 
            -
                    cookies | 
| 355 | 
            -
                    proxy | 
| 356 | 
            -
                    cookie_path: str = None,
         | 
| 357 | 
             
                ) -> None:
         | 
| 358 | 
             
                    if cookies is None:
         | 
| 359 | 
             
                        cookies = {}
         | 
| 360 | 
            -
                     | 
| 361 | 
            -
             | 
| 362 | 
            -
                            with open(cookie_path, encoding="utf-8") as f:
         | 
| 363 | 
            -
                                self.cookies = json.load(f)
         | 
| 364 | 
            -
                        except FileNotFoundError as exc:
         | 
| 365 | 
            -
                            raise FileNotFoundError("Cookie file not found") from exc
         | 
| 366 | 
            -
                    else:
         | 
| 367 | 
            -
                        self.cookies = cookies
         | 
| 368 | 
            -
                    self.proxy: str | None = proxy
         | 
| 369 | 
             
                    self.chat_hub: _ChatHub = _ChatHub(
         | 
| 370 | 
             
                        _Conversation(self.cookies, self.proxy),
         | 
| 371 | 
             
                    )
         | 
| @@ -425,43 +406,6 @@ class Chatbot: | |
| 425 | 
             
                    self.chat_hub = _ChatHub(_Conversation(self.cookies))
         | 
| 426 |  | 
| 427 |  | 
| 428 | 
            -
            async def _get_input_async(
         | 
| 429 | 
            -
                session: PromptSession = None,
         | 
| 430 | 
            -
                completer: WordCompleter = None,
         | 
| 431 | 
            -
            ) -> str:
         | 
| 432 | 
            -
                """
         | 
| 433 | 
            -
                Multiline input function.
         | 
| 434 | 
            -
                """
         | 
| 435 | 
            -
                return await session.prompt_async(
         | 
| 436 | 
            -
                    completer=completer,
         | 
| 437 | 
            -
                    multiline=True,
         | 
| 438 | 
            -
                    auto_suggest=AutoSuggestFromHistory(),
         | 
| 439 | 
            -
                )
         | 
| 440 | 
            -
             | 
| 441 | 
            -
             | 
| 442 | 
            -
            def _create_session() -> PromptSession:
         | 
| 443 | 
            -
                kb = KeyBindings()
         | 
| 444 | 
            -
             | 
| 445 | 
            -
                @kb.add("enter")
         | 
| 446 | 
            -
                def _(event):
         | 
| 447 | 
            -
                    buffer_text = event.current_buffer.text
         | 
| 448 | 
            -
                    if buffer_text.startswith("!"):
         | 
| 449 | 
            -
                        event.current_buffer.validate_and_handle()
         | 
| 450 | 
            -
                    else:
         | 
| 451 | 
            -
                        event.current_buffer.insert_text("\n")
         | 
| 452 | 
            -
             | 
| 453 | 
            -
                @kb.add("escape")
         | 
| 454 | 
            -
                def _(event):
         | 
| 455 | 
            -
                    if event.current_buffer.complete_state:
         | 
| 456 | 
            -
                        # event.current_buffer.cancel_completion()
         | 
| 457 | 
            -
                        event.current_buffer.text = ""
         | 
| 458 | 
            -
             | 
| 459 | 
            -
                return PromptSession(key_bindings=kb, history=InMemoryHistory())
         | 
| 460 | 
            -
             | 
| 461 | 
            -
             | 
| 462 | 
            -
            def _create_completer(commands: list, pattern_str: str = "$"):
         | 
| 463 | 
            -
                return WordCompleter(words=commands, pattern=re.compile(pattern_str))
         | 
| 464 | 
            -
             | 
| 465 |  | 
| 466 | 
             
            load_message = ""
         | 
| 467 |  | 
| @@ -470,7 +414,10 @@ load_message = "" | |
| 470 | 
             
            第二部分:子进程Worker
         | 
| 471 | 
             
            ========================================================================
         | 
| 472 | 
             
            """
         | 
| 473 | 
            -
             | 
|  | |
|  | |
|  | |
| 474 | 
             
            class GetNewBingHandle(Process):
         | 
| 475 | 
             
                def __init__(self):
         | 
| 476 | 
             
                    super().__init__(daemon=True)
         | 
| @@ -484,7 +431,6 @@ class GetNewBingHandle(Process): | |
| 484 |  | 
| 485 | 
             
                def check_dependency(self):
         | 
| 486 | 
             
                    try:
         | 
| 487 | 
            -
                        import rich
         | 
| 488 | 
             
                        self.info = "依赖检测通过"
         | 
| 489 | 
             
                        self.success = True
         | 
| 490 | 
             
                    except:
         | 
| @@ -538,6 +484,7 @@ class GetNewBingHandle(Process): | |
| 538 | 
             
                        try:
         | 
| 539 | 
             
                            asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history']))
         | 
| 540 | 
             
                        except:
         | 
|  | |
| 541 | 
             
                            self.child.send('[Local Message] Newbing失败.')
         | 
| 542 | 
             
                        self.child.send('[Finish]')
         | 
| 543 |  | 
|  | |
| 5 | 
             
            ========================================================================
         | 
| 6 | 
             
            """
         | 
| 7 |  | 
| 8 | 
            +
             | 
|  | |
|  | |
|  | |
|  | |
| 9 | 
             
            import argparse
         | 
| 10 | 
             
            import asyncio
         | 
| 11 | 
             
            import json
         | 
|  | |
| 24 | 
             
            import certifi
         | 
| 25 | 
             
            import httpx
         | 
| 26 | 
             
            import websockets.client as websockets
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 27 |  | 
| 28 | 
             
            DELIMITER = "\x1e"
         | 
| 29 |  | 
|  | |
| 135 |  | 
| 136 | 
             
                def update(
         | 
| 137 | 
             
                    self,
         | 
| 138 | 
            +
                    prompt,
         | 
| 139 | 
            +
                    conversation_style,
         | 
| 140 | 
            +
                    options,
         | 
| 141 | 
             
                ) -> None:
         | 
| 142 | 
             
                    """
         | 
| 143 | 
             
                    Updates request object
         | 
|  | |
| 203 |  | 
| 204 | 
             
                def __init__(
         | 
| 205 | 
             
                    self,
         | 
| 206 | 
            +
                    cookies,
         | 
| 207 | 
            +
                    proxy,
         | 
| 208 | 
             
                ) -> None:
         | 
| 209 | 
             
                    self.struct: dict = {
         | 
| 210 | 
             
                        "conversationId": None,
         | 
|  | |
| 260 | 
             
                Chat API
         | 
| 261 | 
             
                """
         | 
| 262 |  | 
| 263 | 
            +
                def __init__(self, conversation) -> None:
         | 
| 264 | 
            +
                    self.wss = None
         | 
| 265 | 
             
                    self.request: _ChatHubRequest
         | 
| 266 | 
             
                    self.loop: bool
         | 
| 267 | 
             
                    self.task: asyncio.Task
         | 
|  | |
| 340 |  | 
| 341 | 
             
                def __init__(
         | 
| 342 | 
             
                    self,
         | 
| 343 | 
            +
                    cookies,
         | 
| 344 | 
            +
                    proxy
         | 
|  | |
| 345 | 
             
                ) -> None:
         | 
| 346 | 
             
                    if cookies is None:
         | 
| 347 | 
             
                        cookies = {}
         | 
| 348 | 
            +
                    self.cookies = cookies
         | 
| 349 | 
            +
                    self.proxy = proxy
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 350 | 
             
                    self.chat_hub: _ChatHub = _ChatHub(
         | 
| 351 | 
             
                        _Conversation(self.cookies, self.proxy),
         | 
| 352 | 
             
                    )
         | 
|  | |
| 406 | 
             
                    self.chat_hub = _ChatHub(_Conversation(self.cookies))
         | 
| 407 |  | 
| 408 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 409 |  | 
| 410 | 
             
            load_message = ""
         | 
| 411 |  | 
|  | |
| 414 | 
             
            第二部分:子进程Worker
         | 
| 415 | 
             
            ========================================================================
         | 
| 416 | 
             
            """
         | 
| 417 | 
            +
            import time
         | 
| 418 | 
            +
            import importlib
         | 
| 419 | 
            +
            from toolbox import update_ui, get_conf, trimmed_format_exc
         | 
| 420 | 
            +
            from multiprocessing import Process, Pipe
         | 
| 421 | 
             
            class GetNewBingHandle(Process):
         | 
| 422 | 
             
                def __init__(self):
         | 
| 423 | 
             
                    super().__init__(daemon=True)
         | 
|  | |
| 431 |  | 
| 432 | 
             
                def check_dependency(self):
         | 
| 433 | 
             
                    try:
         | 
|  | |
| 434 | 
             
                        self.info = "依赖检测通过"
         | 
| 435 | 
             
                        self.success = True
         | 
| 436 | 
             
                    except:
         | 
|  | |
| 484 | 
             
                        try:
         | 
| 485 | 
             
                            asyncio.run(self.async_run(question=kwargs['query'], history=kwargs['history']))
         | 
| 486 | 
             
                        except:
         | 
| 487 | 
            +
                            tb_str = '```\n' + trimmed_format_exc() + '```'
         | 
| 488 | 
             
                            self.child.send('[Local Message] Newbing失败.')
         | 
| 489 | 
             
                        self.child.send('[Finish]')
         | 
| 490 |  |