|
|
|
@ -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)
|
|
|
|
|
await delete_conversation(session, conversation, proxy)
|