1
0
Fork 0
GPT4FREE/unfinished/bing/__ini__.py

109 lines
3.5 KiB
Python
Raw Normal View History

2023-04-27 16:17:07 +00:00
# Import necessary libraries
2023-04-27 14:43:59 +00:00
import asyncio
2023-04-27 16:17:07 +00:00
from json import dumps, loads
2023-04-27 14:43:59 +00:00
from ssl import create_default_context
2023-04-16 16:43:26 +00:00
import websockets
2023-04-27 14:43:59 +00:00
from browser_cookie3 import edge
from certifi import where
from requests import get
2023-04-16 16:43:26 +00:00
2023-04-27 16:17:07 +00:00
# Set up SSL context
2023-04-16 16:43:26 +00:00
ssl_context = create_default_context()
ssl_context.load_verify_locations(where())
2023-04-27 16:17:07 +00:00
2023-04-16 16:43:26 +00:00
def format(msg: dict) -> str:
2023-04-27 16:17:07 +00:00
"""Format message as JSON string with delimiter."""
2023-04-16 16:43:26 +00:00
return dumps(msg) + '\x1e'
2023-04-27 16:17:07 +00:00
def get_token():
"""Retrieve token from browser cookies."""
2023-04-16 16:43:26 +00:00
cookies = {c.name: c.value for c in edge(domain_name='bing.com')}
return cookies['_U']
class AsyncCompletion:
async def create(
2023-04-27 14:43:59 +00:00
prompt: str = 'hello world',
optionSets: list = [
'deepleo',
'enable_debug_commands',
'disable_emoji_spoken_text',
'enablemm',
'h3relaxedimg'
],
2023-04-27 16:17:07 +00:00
token: str = get_token()):
"""Create a connection to Bing AI and send the prompt."""
# Send create request
create = get('https://edgeservices.bing.com/edgesvc/turing/conversation/create',
headers={
'host': 'edgeservices.bing.com',
'authority': 'edgeservices.bing.com',
'cookie': f'_U={token}',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69',
}
)
# Extract conversation data
conversationId = create.json()['conversationId']
clientId = create.json()['clientId']
2023-04-16 16:43:26 +00:00
conversationSignature = create.json()['conversationSignature']
2023-04-27 16:17:07 +00:00
# Connect to WebSocket
wss = await websockets.connect('wss://sydney.bing.com/sydney/ChatHub', max_size=None, ssl=ssl_context,
extra_headers={
# Add necessary headers
}
)
2023-04-16 16:43:26 +00:00
2023-04-27 16:17:07 +00:00
# Send JSON protocol version
2023-04-16 16:43:26 +00:00
await wss.send(format({'protocol': 'json', 'version': 1}))
await wss.recv()
2023-04-27 16:17:07 +00:00
# Define message structure
2023-04-16 16:43:26 +00:00
struct = {
2023-04-27 16:17:07 +00:00
# Add necessary message structure
2023-04-16 16:43:26 +00:00
}
2023-04-27 16:17:07 +00:00
# Send message
2023-04-16 16:43:26 +00:00
await wss.send(format(struct))
2023-04-27 16:17:07 +00:00
# Process responses
2023-04-16 16:43:26 +00:00
base_string = ''
final = False
while not final:
objects = str(await wss.recv()).split('\x1e')
for obj in objects:
if obj is None or obj == '':
continue
2023-04-27 16:17:07 +00:00
2023-04-16 16:43:26 +00:00
response = loads(obj)
2023-04-27 14:43:59 +00:00
if response.get('type') == 1 and response['arguments'][0].get('messages', ):
2023-04-27 16:17:07 +00:00
response_text = response['arguments'][0]['messages'][0]['adaptiveCards'][0]['body'][0].get(
'text')
2023-04-16 16:43:26 +00:00
yield (response_text.replace(base_string, ''))
base_string = response_text
2023-04-27 16:17:07 +00:00
2023-04-16 16:43:26 +00:00
elif response.get('type') == 2:
final = True
2023-04-27 16:17:07 +00:00
2023-04-16 16:43:26 +00:00
await wss.close()
2023-04-27 16:17:07 +00:00
2023-04-16 16:43:26 +00:00
async def run():
2023-04-27 16:17:07 +00:00
"""Run the async completion and print the result."""
2023-04-16 16:43:26 +00:00
async for value in AsyncCompletion.create(
2023-04-27 14:43:59 +00:00
prompt='summarize cinderella with each word beginning with a consecutive letter of the alphabet, a-z',
optionSets=[
"galileo",
]
2023-04-16 16:43:26 +00:00
):
2023-04-27 16:17:07 +00:00
print(value, end='', flush=True)
2023-04-16 16:43:26 +00:00
2023-04-27 14:43:59 +00:00
2023-04-27 16:17:07 +00:00
asyncio.run(run())