From 252f595b6a1f0bd4068c1f2becd5a932e3345bb7 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 2 Oct 2023 22:43:36 +0200 Subject: Bing Updates: Add retry create conversation Removed get_cookies fallback Add creative tone parameter --- g4f/Provider/Bing.py | 78 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 33 deletions(-) (limited to 'g4f/Provider/Bing.py') diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index e4e56519..940b3a0b 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -6,8 +6,12 @@ import os import urllib.parse from aiohttp import ClientSession, ClientTimeout from ..typing import AsyncGenerator -from .base_provider import AsyncGeneratorProvider, get_cookies +from .base_provider import AsyncGeneratorProvider +class Tones(): + creative = "Creative" + balanced = "Balanced" + precise = "Precise" class Bing(AsyncGeneratorProvider): url = "https://bing.com/chat" @@ -16,12 +20,13 @@ class Bing(AsyncGeneratorProvider): @staticmethod def create_async_generator( - model: str, - messages: list[dict[str, str]], - cookies: dict = None, **kwargs) -> AsyncGenerator: + model: str, + messages: list[dict[str, str]], + cookies: dict = None, + tone: str = Tones.creative, + **kwargs + ) -> AsyncGenerator: - if not cookies: - cookies = get_cookies(".bing.com") if len(messages) < 2: prompt = messages[0]["content"] context = None @@ -38,7 +43,7 @@ class Bing(AsyncGeneratorProvider): 'SRCHUSR' : '', 'SRCHHPGUSR' : '', } - return stream_generate(prompt, context, cookies) + return stream_generate(prompt, tone, context, cookies) def create_context(messages: list[dict[str, str]]): context = "".join(f"[{message['role']}](#message)\n{message['content']}\n\n" for message in messages) @@ -64,6 +69,14 @@ async def create_conversation(session: ClientSession) -> Conversation: return Conversation(conversationId, clientId, conversationSignature) +async def retry_conversation(session: ClientSession) -> Conversation: + for _ in range(5): + try: + return await create_conversation(session) + except: + session.cookie_jar.clear() + return await create_conversation(session) + async def list_conversations(session: ClientSession) -> list: url = "https://www.bing.com/turing/conversation/chats" async with session.get(url) as response: @@ -162,34 +175,32 @@ class Defaults: 'x-forwarded-for': ip_address, } - optionsSets = { - "optionsSets": [ - 'saharasugg', - 'enablenewsfc', - 'clgalileo', - 'gencontentv3', - "nlu_direct_response_filter", - "deepleo", - "disable_emoji_spoken_text", - "responsible_ai_policy_235", - "enablemm", - "h3precise" - "dtappid", - "cricinfo", - "cricinfov2", - "dv3sugg", - "nojbfedge" - ] - } + optionsSets = [ + 'saharasugg', + 'enablenewsfc', + 'clgalileo', + 'gencontentv3', + "nlu_direct_response_filter", + "deepleo", + "disable_emoji_spoken_text", + "responsible_ai_policy_235", + "enablemm", + "h3precise" + "dtappid", + "cricinfo", + "cricinfov2", + "dv3sugg", + "nojbfedge" + ] def format_message(msg: dict) -> str: return json.dumps(msg, ensure_ascii=False) + Defaults.delimiter -def create_message(conversation: Conversation, prompt: str, context: str=None) -> str: +def create_message(conversation: Conversation, prompt: str, tone: str, context: str=None) -> str: struct = { 'arguments': [ { - **Defaults.optionsSets, + 'optionsSets': Defaults.optionsSets, 'source': 'cib', 'allowedMessageTypes': Defaults.allowedMessageTypes, 'sliceIds': Defaults.sliceIds, @@ -201,6 +212,7 @@ def create_message(conversation: Conversation, prompt: str, context: str=None) - 'text': prompt, 'messageType': 'Chat' }, + 'tone': tone, 'conversationSignature': conversation.conversationSignature, 'participant': { 'id': conversation.clientId @@ -225,15 +237,16 @@ def create_message(conversation: Conversation, prompt: str, context: str=None) - async def stream_generate( prompt: str, + tone: str, context: str=None, - cookies: dict=None + cookies: dict=None, ): async with ClientSession( timeout=ClientTimeout(total=900), cookies=cookies, headers=Defaults.headers, ) as session: - conversation = await create_conversation(session) + conversation = await retry_conversation(session) try: async with session.ws_connect( 'wss://sydney.bing.com/sydney/ChatHub', @@ -243,7 +256,7 @@ async def stream_generate( await wss.send_str(format_message({'protocol': 'json', 'version': 1})) msg = await wss.receive(timeout=900) - await wss.send_str(create_message(conversation, prompt, context)) + await wss.send_str(create_message(conversation, prompt, tone, context)) response_txt = '' returned_text = '' @@ -281,7 +294,6 @@ async def stream_generate( result = response['item']['result'] if result.get('error'): raise Exception(f"{result['value']}: {result['message']}") - final = True - break + return finally: await delete_conversation(session, conversation) \ No newline at end of file -- cgit v1.2.3