diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-10-10 00:18:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-10 00:18:17 +0200 |
commit | 8a5c23f693c1ad92b5d5259201bb99807f76ea2a (patch) | |
tree | 26a25d7b5bee6ba1e2ac484e1ad124a8e52b0b43 /g4f/Provider/Bing.py | |
parent | Merge branch 'main' of https://github.com/xtekky/gpt4free (diff) | |
parent | Add Proxy Support and Create Provider to Readme (diff) | |
download | gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.tar gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.tar.gz gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.tar.bz2 gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.tar.lz gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.tar.xz gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.tar.zst gpt4free-8a5c23f693c1ad92b5d5259201bb99807f76ea2a.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 |