From d1b6921b5fa3d9dbd7f1cce6e4f396744a7a2274 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 23 Aug 2023 02:16:35 +0200 Subject: Add create_async method --- g4f/Provider/Bing.py | 58 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'g4f/Provider') diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index 364e9c1d..b9b9e9a4 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -13,34 +13,54 @@ from .base_provider import BaseProvider class Bing(BaseProvider): url = "https://bing.com/chat" + working = True supports_gpt_4 = True - @staticmethod + @classmethod def create_completion( + cls, model: str, messages: list[dict[str, str]], stream: bool, **kwargs: Any ) -> CreateResult: - yield from run(create(messages, **kwargs)) - -def create( + if stream: + yield from run(cls.create_async_generator(model, messages, **kwargs)) + else: + yield asyncio.run(cls.create_async(model, messages, **kwargs)) + + @classmethod + async def create_async( + cls, + model: str, messages: list[dict[str, str]], - cookies: dict = {} - ): - if len(messages) < 2: - prompt = messages[0]["content"] - context = None - - else: - prompt = messages[-1]["content"] - context = convert(messages[:-1]) - - if not cookies: - for cookie in browser_cookie3.load(domain_name='.bing.com'): - cookies[cookie.name] = cookie.value - - return stream_generate(prompt, context, cookies) + **kwargs: Any, + ) -> str: + result = [] + async for chunk in cls.create_async_generator(model, messages, **kwargs): + result.append(chunk) + if result: + return "".join(result) + + @staticmethod + def create_async_generator( + model: str, + messages: list[dict[str, str]], + cookies: dict = {} + ) -> AsyncGenerator: + if len(messages) < 2: + prompt = messages[0]["content"] + context = None + + else: + prompt = messages[-1]["content"] + context = convert(messages[:-1]) + + if not cookies: + for cookie in browser_cookie3.load(domain_name='.bing.com'): + cookies[cookie.name] = cookie.value + + return stream_generate(prompt, context, cookies) def convert(messages: list[dict[str, str]]): context = "" -- cgit v1.2.3