diff options
author | Heiner Lohaus <heiner@lohaus.eu> | 2023-10-09 10:22:17 +0200 |
---|---|---|
committer | Heiner Lohaus <heiner@lohaus.eu> | 2023-10-09 10:22:17 +0200 |
commit | e46b5fe043ffcdb87ddb97db14a7e699dd8a2690 (patch) | |
tree | f162aa3ea3c746dd297c1c07c4c1d9237090a5a0 /g4f/Provider/Bing.py | |
parent | Merge branch 'main' of https://github.com/xtekky/gpt4free (diff) | |
download | gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.tar gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.tar.gz gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.tar.bz2 gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.tar.lz gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.tar.xz gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.tar.zst gpt4free-e46b5fe043ffcdb87ddb97db14a7e699dd8a2690.zip |
Diffstat (limited to 'g4f/Provider/Bing.py')
-rw-r--r-- | g4f/Provider/Bing.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index f4275a5f..f8c6a87a 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -7,7 +7,7 @@ import os import uuid import urllib.parse from aiohttp import ClientSession, ClientTimeout -from ..typing import AsyncGenerator +from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider class Tones(): @@ -32,11 +32,12 @@ class Bing(AsyncGeneratorProvider): @staticmethod def create_async_generator( model: str, - messages: list[dict[str, str]], + messages: Messages, + proxy: str = None, cookies: dict = None, tone: str = Tones.creative, **kwargs - ) -> AsyncGenerator: + ) -> AsyncResult: if len(messages) < 2: prompt = messages[0]["content"] context = None @@ -46,9 +47,9 @@ class Bing(AsyncGeneratorProvider): if not cookies or "SRCHD" not in cookies: cookies = default_cookies - return stream_generate(prompt, tone, context, cookies) + return stream_generate(prompt, tone, context, proxy, cookies) -def create_context(messages: list[dict[str, str]]): +def create_context(messages: Messages): context = "".join(f"[{message['role']}](#message)\n{message['content']}\n\n" for message in messages) return context @@ -59,10 +60,10 @@ class Conversation(): self.clientId = clientId self.conversationSignature = conversationSignature -async def create_conversation(session: ClientSession) -> Conversation: +async def create_conversation(session: ClientSession, proxy: str = None) -> Conversation: url = 'https://www.bing.com/turing/conversation/create?bundleVersion=1.1150.3' - async with await session.get(url) as response: + async with await session.get(url, proxy=proxy) as response: data = await response.json() conversationId = data.get('conversationId') @@ -80,7 +81,7 @@ async def list_conversations(session: ClientSession) -> list: response = await response.json() return response["chats"] -async def delete_conversation(session: ClientSession, conversation: Conversation) -> list: +async def delete_conversation(session: ClientSession, conversation: Conversation, proxy: str = None) -> list: url = "https://sydney.bing.com/sydney/DeleteSingleConversation" json = { "conversationId": conversation.conversationId, @@ -89,7 +90,7 @@ async def delete_conversation(session: ClientSession, conversation: Conversation "source": "cib", "optionsSets": ["autosave"] } - async with session.post(url, json=json) as response: + async with session.post(url, json=json, proxy=proxy) as response: response = await response.json() return response["result"]["value"] == "Success" @@ -239,20 +240,22 @@ def create_message(conversation: Conversation, prompt: str, tone: str, context: async def stream_generate( prompt: str, tone: str, - context: str=None, - cookies: dict=None, + context: str = None, + proxy: str = 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 create_conversation(session, proxy) try: async with session.ws_connect( f'wss://sydney.bing.com/sydney/ChatHub', autoping=False, - params={'sec_access_token': conversation.conversationSignature} + params={'sec_access_token': conversation.conversationSignature}, + proxy=proxy ) as wss: await wss.send_str(format_message({'protocol': 'json', 'version': 1})) @@ -297,4 +300,4 @@ async def stream_generate( raise Exception(f"{result['value']}: {result['message']}") return finally: - await delete_conversation(session, conversation)
\ No newline at end of file + await delete_conversation(session, conversation, proxy)
\ No newline at end of file |