Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
working websockets only
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Jun 17, 2023
1 parent 30e166e commit 552b607
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/EdgeGPT/chathub.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ async def ask_stream(

# Check if websocket is closed
async with connect(
wss_link or "wss://sydney.bing.com/sydney/ChatHub", extra_headers=HEADERS
wss_link or "wss://sydney.bing.com/sydney/ChatHub",
extra_headers=HEADERS,
max_size=None,
ssl=ssl_context,
) as wss:
await self._initial_handshake(wss)
# Construct a ChatHub request
Expand All @@ -125,8 +128,8 @@ async def ask_stream(
if retry_count == 0:
raise Exception("No response from server")
continue
if isinstance(msg.data, str):
objects = msg.data.split(DELIMITER)
if isinstance(msg, str):
objects = msg.split(DELIMITER)
else:
continue
for obj in objects:
Expand Down
38 changes: 21 additions & 17 deletions src/EdgeGPT/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import base64
import random
import secrets

sec_websocket_key = secrets.token_urlsafe(16)
sec_websocket_key = base64.b64encode(sec_websocket_key.encode()).decode()
import uuid

DELIMITER = "\x1e"

Expand All @@ -14,18 +10,26 @@
)

HEADERS = {
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "no-cache",
"Connection": "Upgrade",
"Host": "sydney.bing.com",
"Origin": "https://www.bing.com",
"Pragma": "no-cache",
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
"Sec-WebSocket-Key": sec_websocket_key,
"Sec-WebSocket-Version": "13",
"Upgrade": "websocket",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.46",
"accept": "application/json",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/json",
"sec-ch-ua": '"Not_A Brand";v="99", Microsoft Edge";v="110", "Chromium";v="110"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"109.0.1518.78"',
"sec-ch-ua-full-version-list": '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": "",
"sec-ch-ua-platform": '"Windows"',
"sec-ch-ua-platform-version": '"15.0.0"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-ms-client-request-id": str(uuid.uuid4()),
"x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32",
"Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx",
"Referrer-Policy": "origin-when-cross-origin",
"x-forwarded-for": FORWARDED_IP,
}

HEADERS_INIT_CONVER = {
Expand Down

0 comments on commit 552b607

Please sign in to comment.