Hansimov commited on
Commit
c05a7fe
1 Parent(s): 8d91150

:recycle: [Refactor] Modularize Conversation Creator and Connector

Browse files
__init__.py ADDED
File without changes
chathub_request_constructor.py CHANGED
@@ -26,7 +26,7 @@ class ChathubRequestConstructor:
26
  return "".join(random.choice("0123456789abcdef") for _ in range(length))
27
 
28
  def construct(self):
29
- self.request_message = {
30
  "arguments": [
31
  {
32
  "source": "cib",
 
26
  return "".join(random.choice("0123456789abcdef") for _ in range(length))
27
 
28
  def construct(self):
29
+ self.request_payload = {
30
  "arguments": [
31
  {
32
  "source": "cib",
conversation_creater.py → conversation_connector.py RENAMED
@@ -5,6 +5,7 @@ import json
5
  import pprint
6
  import urllib
7
 
 
8
  from chathub_request_constructor import ChathubRequestConstructor
9
  from logger.logger import logger
10
 
@@ -12,30 +13,6 @@ from logger.logger import logger
12
  http_proxy = "http://localhost:11111" # Replace with yours
13
 
14
 
15
- class ConversationCreator:
16
- conversation_create_url = "https://www.bing.com/turing/conversation/create"
17
-
18
- def __init__(self, cookies={}):
19
- self.cookies = cookies
20
- self.construct_cookies()
21
-
22
- def construct_cookies(self):
23
- self.httpx_cookies = httpx.Cookies()
24
- for key, val in self.cookies.items():
25
- self.httpx_cookies.set(key, val)
26
-
27
- def create(self, proxy=None):
28
- self.response = httpx.get(
29
- self.conversation_create_url,
30
- proxies=http_proxy if proxy is None else proxy,
31
- cookies=self.httpx_cookies,
32
- )
33
- self.response_content = json.loads(self.response.content.decode("utf-8"))
34
- self.response_headers = dict(self.response.headers)
35
- pprint.pprint(self.response_content)
36
- # pprint.pprint(self.response_headers)
37
-
38
-
39
  def serialize_websocket_message(msg: dict) -> str:
40
  return json.dumps(msg, ensure_ascii=False) + "\x1e"
41
 
@@ -98,7 +75,7 @@ class ConversationConnector:
98
  chathub_request_constructor.construct()
99
 
100
  await self.wss.send_str(
101
- serialize_websocket_message(chathub_request_constructor.request_message)
102
  )
103
 
104
  delta_content_pointer = 0
@@ -160,6 +137,7 @@ class ConversationConnector:
160
  # message_text = message["text"]
161
  elif data.get("type") == 3:
162
  logger.success("[Finished]")
 
163
  await self.wss.close()
164
  await self.aiohttp_session.close()
165
  break
@@ -174,7 +152,7 @@ if __name__ == "__main__":
174
  creator = ConversationCreator()
175
  creator.create()
176
 
177
- conversation_connector = ConversationConnector(
178
  sec_access_token=creator.response_headers[
179
  "x-sydney-encryptedconversationsignature"
180
  ],
@@ -187,5 +165,5 @@ if __name__ == "__main__":
187
  logger.mesg(f"{prompt}")
188
  logger.success(f"\n[Bing]:")
189
  loop = asyncio.get_event_loop()
190
- loop.run_until_complete(conversation_connector.stream_chat(prompt=prompt))
191
  loop.close()
 
5
  import pprint
6
  import urllib
7
 
8
+ from conversation_creator import ConversationCreator
9
  from chathub_request_constructor import ChathubRequestConstructor
10
  from logger.logger import logger
11
 
 
13
  http_proxy = "http://localhost:11111" # Replace with yours
14
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def serialize_websocket_message(msg: dict) -> str:
17
  return json.dumps(msg, ensure_ascii=False) + "\x1e"
18
 
 
75
  chathub_request_constructor.construct()
76
 
77
  await self.wss.send_str(
78
+ serialize_websocket_message(chathub_request_constructor.request_payload)
79
  )
80
 
81
  delta_content_pointer = 0
 
137
  # message_text = message["text"]
138
  elif data.get("type") == 3:
139
  logger.success("[Finished]")
140
+ self.invocation_id += 1
141
  await self.wss.close()
142
  await self.aiohttp_session.close()
143
  break
 
152
  creator = ConversationCreator()
153
  creator.create()
154
 
155
+ connector = ConversationConnector(
156
  sec_access_token=creator.response_headers[
157
  "x-sydney-encryptedconversationsignature"
158
  ],
 
165
  logger.mesg(f"{prompt}")
166
  logger.success(f"\n[Bing]:")
167
  loop = asyncio.get_event_loop()
168
+ loop.run_until_complete(connector.stream_chat(prompt=prompt))
169
  loop.close()
conversation_creator.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ import json
3
+ import pprint
4
+
5
+ http_proxy = "http://localhost:11111" # Replace with yours
6
+
7
+
8
+ class ConversationCreator:
9
+ conversation_create_url = "https://www.bing.com/turing/conversation/create"
10
+
11
+ def __init__(self, cookies={}):
12
+ self.cookies = cookies
13
+ self.construct_cookies()
14
+
15
+ def construct_cookies(self):
16
+ self.httpx_cookies = httpx.Cookies()
17
+ for key, val in self.cookies.items():
18
+ self.httpx_cookies.set(key, val)
19
+
20
+ def create(self, proxy=None):
21
+ self.response = httpx.get(
22
+ self.conversation_create_url,
23
+ proxies=http_proxy if proxy is None else proxy,
24
+ cookies=self.httpx_cookies,
25
+ )
26
+ self.response_content = json.loads(self.response.content.decode("utf-8"))
27
+ self.response_headers = dict(self.response.headers)
28
+ pprint.pprint(self.response_content)
29
+ # pprint.pprint(self.response_headers)
30
+
31
+
32
+ if __name__ == "__main__":
33
+ creator = ConversationCreator()
34
+ creator.create()