summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Bing.py
diff options
context:
space:
mode:
authorCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-10 14:15:12 +0200
committerCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-10 14:15:12 +0200
commit0e4297494dd46533738f786e4ac675541586177a (patch)
tree6c2999e14ed831f7f37cc60991571cd040772918 /g4f/Provider/Bing.py
parentadd cool testing for gpt-3.5 and and gpt-4 (diff)
parentUpdate Aivvm.py (diff)
downloadgpt4free-0e4297494dd46533738f786e4ac675541586177a.tar
gpt4free-0e4297494dd46533738f786e4ac675541586177a.tar.gz
gpt4free-0e4297494dd46533738f786e4ac675541586177a.tar.bz2
gpt4free-0e4297494dd46533738f786e4ac675541586177a.tar.lz
gpt4free-0e4297494dd46533738f786e4ac675541586177a.tar.xz
gpt4free-0e4297494dd46533738f786e4ac675541586177a.tar.zst
gpt4free-0e4297494dd46533738f786e4ac675541586177a.zip
Diffstat (limited to 'g4f/Provider/Bing.py')
-rw-r--r--g4f/Provider/Bing.py31
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