Spaces:
Paused
Paused
:recycle: [Refactor] Re-organize project with folders
Browse files- conversations/__init__.py +3 -0
- conversation_connector.py β conversations/conversation_connector.py +10 -29
- conversation_creator.py β conversations/conversation_creator.py +0 -0
- conversation_session.py β conversations/conversation_session.py +2 -3
- main.py +10 -0
- networks/__init__.py +6 -0
- chathub_request_constructor.py β networks/chathub_request_payload_constructor.py +1 -1
- networks/conversation_request_headers_constructor.py +18 -0
- cookies_constructor.py β networks/cookies_constructor.py +0 -0
- message_parser.py β networks/message_parser.py +1 -3
- {logger β utils}/__init_.py +0 -0
- {logger β utils}/logger.py +0 -0
conversations/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .conversation_connector import ConversationConnector
|
| 2 |
+
from .conversation_creator import ConversationCreator
|
| 3 |
+
from .conversation_session import ConversationSession
|
conversation_connector.py β conversations/conversation_connector.py
RENAMED
|
@@ -4,35 +4,16 @@ import httpx
|
|
| 4 |
import json
|
| 5 |
import urllib
|
| 6 |
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
http_proxy = "http://localhost:11111" # Replace with yours
|
| 14 |
|
| 15 |
|
| 16 |
-
class ConversationConnectRequestHeadersConstructor:
|
| 17 |
-
def __init__(self):
|
| 18 |
-
self.construct()
|
| 19 |
-
|
| 20 |
-
def construct(self):
|
| 21 |
-
self.request_headers = {
|
| 22 |
-
"Accept-Encoding": " gzip, deflate, br",
|
| 23 |
-
"Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
|
| 24 |
-
"Cache-Control": "no-cache",
|
| 25 |
-
"Connection": "Upgrade",
|
| 26 |
-
"Host": "sydney.bing.com",
|
| 27 |
-
"Origin": "https://www.bing.com",
|
| 28 |
-
"Pragma": "no-cache",
|
| 29 |
-
"Sec-Websocket-Extensions": "permessage-deflate; client_max_window_bits",
|
| 30 |
-
"Sec-Websocket-Version": "13",
|
| 31 |
-
"Upgrade": "websocket",
|
| 32 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
class ConversationConnector:
|
| 37 |
def __init__(
|
| 38 |
self,
|
|
@@ -66,23 +47,23 @@ class ConversationConnector:
|
|
| 66 |
f"?sec_access_token={self.quotelized_sec_access_token}"
|
| 67 |
)
|
| 68 |
self.aiohttp_session = aiohttp.ClientSession(cookies=self.cookies)
|
| 69 |
-
|
| 70 |
self.wss = await self.aiohttp_session.ws_connect(
|
| 71 |
self.ws_url,
|
| 72 |
-
headers=
|
| 73 |
proxy=http_proxy,
|
| 74 |
)
|
| 75 |
await self.init_handshake()
|
| 76 |
|
| 77 |
async def send_chathub_request(self, prompt):
|
| 78 |
-
|
| 79 |
prompt=prompt,
|
| 80 |
conversation_style=self.conversation_style,
|
| 81 |
client_id=self.client_id,
|
| 82 |
conversation_id=self.conversation_id,
|
| 83 |
invocation_id=self.invocation_id,
|
| 84 |
)
|
| 85 |
-
self.connect_request_payload =
|
| 86 |
await self.wss_send(self.connect_request_payload)
|
| 87 |
|
| 88 |
async def stream_chat(self, prompt=""):
|
|
|
|
| 4 |
import json
|
| 5 |
import urllib
|
| 6 |
|
| 7 |
+
from networks import (
|
| 8 |
+
ChathubRequestPayloadConstructor,
|
| 9 |
+
ConversationRequestHeadersConstructor,
|
| 10 |
+
)
|
| 11 |
+
from networks import MessageParser
|
| 12 |
+
from utils.logger import logger
|
| 13 |
|
| 14 |
http_proxy = "http://localhost:11111" # Replace with yours
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class ConversationConnector:
|
| 18 |
def __init__(
|
| 19 |
self,
|
|
|
|
| 47 |
f"?sec_access_token={self.quotelized_sec_access_token}"
|
| 48 |
)
|
| 49 |
self.aiohttp_session = aiohttp.ClientSession(cookies=self.cookies)
|
| 50 |
+
headers_constructor = ConversationRequestHeadersConstructor()
|
| 51 |
self.wss = await self.aiohttp_session.ws_connect(
|
| 52 |
self.ws_url,
|
| 53 |
+
headers=headers_constructor.request_headers,
|
| 54 |
proxy=http_proxy,
|
| 55 |
)
|
| 56 |
await self.init_handshake()
|
| 57 |
|
| 58 |
async def send_chathub_request(self, prompt):
|
| 59 |
+
payload_constructor = ChathubRequestPayloadConstructor(
|
| 60 |
prompt=prompt,
|
| 61 |
conversation_style=self.conversation_style,
|
| 62 |
client_id=self.client_id,
|
| 63 |
conversation_id=self.conversation_id,
|
| 64 |
invocation_id=self.invocation_id,
|
| 65 |
)
|
| 66 |
+
self.connect_request_payload = payload_constructor.request_payload
|
| 67 |
await self.wss_send(self.connect_request_payload)
|
| 68 |
|
| 69 |
async def stream_chat(self, prompt=""):
|
conversation_creator.py β conversations/conversation_creator.py
RENAMED
|
File without changes
|
conversation_session.py β conversations/conversation_session.py
RENAMED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import asyncio
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
from logger.logger import logger
|
| 5 |
|
| 6 |
|
| 7 |
class ConversationSession:
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
from conversations import ConversationCreator, ConversationConnector
|
| 3 |
+
from utils.logger import logger
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class ConversationSession:
|
main.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from conversations import ConversationSession
|
| 2 |
+
|
| 3 |
+
if __name__ == "__main__":
|
| 4 |
+
prompts = [
|
| 5 |
+
"Today's weather of California",
|
| 6 |
+
"Please summarize your previous answer in table format",
|
| 7 |
+
]
|
| 8 |
+
with ConversationSession("precise") as session:
|
| 9 |
+
for prompt in prompts:
|
| 10 |
+
session.chat(prompt)
|
networks/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .chathub_request_payload_constructor import ChathubRequestPayloadConstructor
|
| 2 |
+
from .cookies_constructor import CookiesConstructor
|
| 3 |
+
from .conversation_request_headers_constructor import (
|
| 4 |
+
ConversationRequestHeadersConstructor,
|
| 5 |
+
)
|
| 6 |
+
from .message_parser import MessageParser
|
chathub_request_constructor.py β networks/chathub_request_payload_constructor.py
RENAMED
|
@@ -2,7 +2,7 @@ import random
|
|
| 2 |
import uuid
|
| 3 |
|
| 4 |
|
| 5 |
-
class
|
| 6 |
def __init__(
|
| 7 |
self,
|
| 8 |
prompt,
|
|
|
|
| 2 |
import uuid
|
| 3 |
|
| 4 |
|
| 5 |
+
class ChathubRequestPayloadConstructor:
|
| 6 |
def __init__(
|
| 7 |
self,
|
| 8 |
prompt,
|
networks/conversation_request_headers_constructor.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class ConversationRequestHeadersConstructor:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.construct()
|
| 4 |
+
|
| 5 |
+
def construct(self):
|
| 6 |
+
self.request_headers = {
|
| 7 |
+
"Accept-Encoding": "gzip, deflate, br",
|
| 8 |
+
"Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
|
| 9 |
+
"Cache-Control": "no-cache",
|
| 10 |
+
"Connection": "Upgrade",
|
| 11 |
+
"Host": "sydney.bing.com",
|
| 12 |
+
"Origin": "https://www.bing.com",
|
| 13 |
+
"Pragma": "no-cache",
|
| 14 |
+
"Sec-Websocket-Extensions": "permessage-deflate; client_max_window_bits",
|
| 15 |
+
"Sec-Websocket-Version": "13",
|
| 16 |
+
"Upgrade": "websocket",
|
| 17 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
|
| 18 |
+
}
|
cookies_constructor.py β networks/cookies_constructor.py
RENAMED
|
File without changes
|
message_parser.py β networks/message_parser.py
RENAMED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from
|
| 2 |
|
| 3 |
|
| 4 |
class MessageParser:
|
|
@@ -7,11 +7,9 @@ class MessageParser:
|
|
| 7 |
|
| 8 |
def parse(self, data):
|
| 9 |
arguments = data["arguments"][0]
|
| 10 |
-
|
| 11 |
if arguments.get("throttling"):
|
| 12 |
throttling = arguments.get("throttling")
|
| 13 |
# pprint.pprint(throttling)
|
| 14 |
-
|
| 15 |
if arguments.get("messages"):
|
| 16 |
for message in arguments.get("messages"):
|
| 17 |
message_type = message.get("messageType")
|
|
|
|
| 1 |
+
from utils.logger import logger
|
| 2 |
|
| 3 |
|
| 4 |
class MessageParser:
|
|
|
|
| 7 |
|
| 8 |
def parse(self, data):
|
| 9 |
arguments = data["arguments"][0]
|
|
|
|
| 10 |
if arguments.get("throttling"):
|
| 11 |
throttling = arguments.get("throttling")
|
| 12 |
# pprint.pprint(throttling)
|
|
|
|
| 13 |
if arguments.get("messages"):
|
| 14 |
for message in arguments.get("messages"):
|
| 15 |
message_type = message.get("messageType")
|
{logger β utils}/__init_.py
RENAMED
|
File without changes
|
{logger β utils}/logger.py
RENAMED
|
File without changes
|