Antonio Cheong commited on
Commit
7c908ce
·
0 Parent(s):

Initial commit

Browse files
.pre-commit-config.yaml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/asottile/reorder_python_imports
3
+ rev: v3.9.0
4
+ hooks:
5
+ - id: reorder-python-imports
6
+ args: [--py37-plus]
7
+ - repo: https://github.com/asottile/add-trailing-comma
8
+ rev: v2.3.0
9
+ hooks:
10
+ - id: add-trailing-comma
11
+ args: [--py36-plus]
12
+ - repo: https://github.com/asottile/pyupgrade
13
+ rev: v3.3.1
14
+ hooks:
15
+ - id: pyupgrade
16
+ args: [--py37-plus]
17
+
18
+ - repo: https://github.com/pre-commit/pre-commit-hooks
19
+ rev: v4.4.0
20
+ hooks:
21
+ - id: trailing-whitespace
22
+ - id: end-of-file-fixer
23
+ - id: check-yaml
24
+ - id: debug-statements
25
+ - id: double-quote-string-fixer
26
+ - id: name-tests-test
27
+ - id: requirements-txt-fixer
28
+ - repo: https://github.com/psf/black
29
+ rev: 22.10.0
30
+ hooks:
31
+ - id: black
.vscode/settings.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "cSpell.words": [
3
+ "websockets"
4
+ ]
5
+ }
docs.md ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # Sydney Project Documentation
2
+
3
+ ## Endpoints
4
+
5
+ Chat: `wss://sydney.bing.com/sydney/ChatHub`
6
+
7
+ Conversation creation: `https://www.bing.com/turing/conversation/create`
main.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Main.py
3
+ """
4
+ import uuid
5
+ import json
6
+ import time
7
+ import requests
8
+ from websocket import WebSocket
9
+ from threading import Thread
10
+
11
+
12
+ def append_identifier(msg: dict) -> str:
13
+ """
14
+ Appends special character to end of message to identify end of message
15
+ """
16
+ # Convert dict to json string
17
+ return json.dumps(msg) + ""
18
+
19
+
20
+ class ChatHubRequest:
21
+ """
22
+ Request object for ChatHub
23
+ """
24
+ def __init__(
25
+ self,
26
+ conversation_signature: str,
27
+ client_id: str,
28
+ conversation_id: str,
29
+ invocation_id: int,
30
+ ) -> None:
31
+ self.struct: dict
32
+
33
+ self.client_id: str = client_id
34
+ self.conversation_id: str = conversation_id
35
+ self.conversation_signature: str = conversation_signature
36
+ self.invocation_id: int = invocation_id
37
+ self.is_start_of_session: bool = True
38
+
39
+ self.update(prompt=None, conversation_signature=conversation_signature, client_id=client_id, conversation_id=conversation_id, invocation_id=invocation_id)
40
+
41
+ def update(
42
+ self,
43
+ prompt: str,
44
+ conversation_signature: str = None,
45
+ client_id: str = None,
46
+ conversation_id: str = None,
47
+ invocation_id: int = None,
48
+ ) -> None:
49
+ """
50
+ Updates request object
51
+ """
52
+ self.struct = {
53
+ "arguments": [
54
+ {
55
+ "source": "cib",
56
+ "optionsSets": [
57
+ "nlu_direct_response_filter",
58
+ "deepleo",
59
+ "enable_debug_commands",
60
+ "disable_emoji_spoken_text",
61
+ "responsible_ai_policy_235",
62
+ "enablemm",
63
+ ],
64
+ "isStartOfSession": self.is_start_of_session,
65
+ "message": {
66
+ "timestamp": "2023-02-09T13:26:58+08:00",
67
+ "author": "user",
68
+ "inputMethod": "Keyboard",
69
+ "text": prompt,
70
+ "messageType": "Chat",
71
+ },
72
+ "conversationSignature": conversation_signature or self.conversation_signature,
73
+ "participant": {"id": client_id or self.client_id},
74
+ "conversationId": conversation_id or self.conversation_id,
75
+ "previousMessages": [],
76
+ }
77
+ ],
78
+ "invocationId": str(invocation_id),
79
+ "target": "chat",
80
+ "type": 4,
81
+ }
82
+ self.is_start_of_session = False
83
+
84
+ class Conversation:
85
+ """
86
+ Conversation API
87
+ """
88
+
89
+ def __init__(self) -> None:
90
+ self.struct: dict = {'conversationId': None, 'clientId': None, 'conversationSignature': None, 'result': {'value': 'Success', 'message': None}}
91
+ self.__create()
92
+
93
+ def __create(self):
94
+ # Build request
95
+ headers = {
96
+ "accept": "application/json",
97
+ "accept-encoding": "gzip, deflate, br",
98
+ "accept-language": "en-US,en;q=0.9",
99
+ "content-type": "application/json",
100
+ "sec-ch-ua": '"Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
101
+ "sec-ch-ua-arch": '"x86"',
102
+ "sec-ch-ua-bitness": '"64"',
103
+ "sec-ch-ua-full-version": '"111.0.1652.0"',
104
+ "sec-ch-ua-full-version-list": '"Microsoft Edge";v="111.0.1652.0", "Not(A:Brand";v="8.0.0.0", "Chromium";v="111.0.5551.0"',
105
+ "sec-ch-ua-mobile": "?0",
106
+ "sec-ch-ua-model": "",
107
+ "sec-ch-ua-platform": '"Linux"',
108
+ "sec-ch-ua-platform-version": '"5.19.0"',
109
+ "sec-fetch-dest": "empty",
110
+ "sec-fetch-mode": "cors",
111
+ "sec-fetch-site": "same-origin",
112
+ "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.0.0",
113
+ "x-ms-client-request-id": str(uuid.uuid4()),
114
+ "x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Linuxx86_64",
115
+ }
116
+ # Create cookies
117
+ cookies = json.loads(
118
+ open("templates/cookies.json", "r", encoding="utf-8").read()
119
+ )
120
+ # Send GET request
121
+ response = requests.get(
122
+ "https://www.bing.com/turing/conversation/create",
123
+ headers=headers,
124
+ cookies=cookies,
125
+ timeout=30,
126
+ )
127
+ # Return response
128
+ self.struct = response.json()
129
+
130
+
131
+ class ChatHub:
132
+ """
133
+ Chat API
134
+ """
135
+
136
+ def __init__(self) -> None:
137
+ self.wss = WebSocket()
138
+ self.wss.connect(url="wss://sydney.bing.com/sydney/ChatHub")
139
+ self.__initial_handshake()
140
+ # Ping in another thread
141
+ self.thread = Thread(target=self.__ping)
142
+ self.thread.start()
143
+ self.stop_thread = False
144
+
145
+ def ask(self, prompt: str):
146
+ pass
147
+
148
+
149
+ def __initial_handshake(self):
150
+ self.wss.send(append_identifier({"protocol": "json", "version": 1}))
151
+ # Receive blank message
152
+ self.wss.recv()
153
+
154
+ def __ping(self):
155
+ timing = 10
156
+ while True:
157
+ if timing == 0:
158
+ self.wss.send(append_identifier({"type": 6}))
159
+ # Receive pong
160
+ self.wss.recv()
161
+ timing = 10
162
+ else:
163
+ timing -= 1
164
+ time.sleep(1)
165
+ if self.stop_thread:
166
+ break
167
+
168
+ def close(self):
169
+ """
170
+ Close all connections
171
+ """
172
+ self.wss.close()
173
+ self.stop_thread = True
174
+ self.thread.join()
175
+
176
+ async def main():
177
+ """
178
+ Main function
179
+ """
180
+ # Create conversation
181
+ conversation = Conversation()
182
+ print(conversation.struct)
183
+
184
+
185
+ if __name__ == "__main__":
186
+ import asyncio
187
+
188
+ asyncio.run(main())
parse_cookies.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+
2
+ cookies = {}
3
+ with open('cookies.txt') as f:
4
+ cookies = f.read().split("; ")
5
+ # Everything after the first "=" is the value (there may be multiple =)
6
+ cookies = {cookie.split("=")[0]: "=".join(cookie.split("=")[1:]) for cookie in cookies}
7
+
8
+ print(cookies)
templates/chathub_request.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "arguments": [
3
+ {
4
+ "source": "cib",
5
+ "optionsSets": [
6
+ "nlu_direct_response_filter",
7
+ "deepleo",
8
+ "enable_debug_commands",
9
+ "disable_emoji_spoken_text",
10
+ "responsible_ai_policy_235",
11
+ "enablemm"
12
+ ],
13
+ "allowedMessageTypes": [
14
+ "Chat",
15
+ "InternalSearchQuery",
16
+ "InternalSearchResult",
17
+ "InternalLoaderMessage",
18
+ "RenderCardRequest",
19
+ "AdsQuery",
20
+ "SemanticSerp"
21
+ ],
22
+ "sliceIds": [],
23
+ "traceId": "63e48421455345f0854475fe03c0c78a",
24
+ "isStartOfSession": true,
25
+ "message": {
26
+ "locale": "en-US",
27
+ "market": "en-US",
28
+ "region": "SG",
29
+ "location": "lat:0.000000;long:0.000000;re=0m;",
30
+ "locationHints": [
31
+ {
32
+ "country": "Singapore",
33
+ "timezoneoffset": 8,
34
+ "countryConfidence": 8,
35
+ "Center": { "Latitude": 0, "Longitude": 0 },
36
+ "RegionType": 2,
37
+ "SourceType": 1
38
+ }
39
+ ],
40
+ "timestamp": "2023-02-09T13:26:58+08:00",
41
+ "author": "user",
42
+ "inputMethod": "Keyboard",
43
+ "text": "Say this is a test",
44
+ "messageType": "Chat"
45
+ },
46
+ "conversationSignature": "<|CONVERSATION_SIGNATURE|>",
47
+ "participant": { "id": "<|CLIENT_ID|>" },
48
+ "conversationId": "<|CONVERSATION_ID|>",
49
+ "previousMessages": []
50
+ }
51
+ ],
52
+ "invocationId": "1",
53
+ "target": "chat",
54
+ "type": 4
55
+ }
templates/chathub_request_backup.json ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "arguments": [
3
+ {
4
+ "source": "cib",
5
+ "optionsSets": [
6
+ "nlu_direct_response_filter",
7
+ "deepleo",
8
+ "enable_debug_commands",
9
+ "disable_emoji_spoken_text",
10
+ "responsible_ai_policy_235",
11
+ "enablemm"
12
+ ],
13
+ "allowedMessageTypes": [
14
+ "Chat",
15
+ "InternalSearchQuery",
16
+ "InternalSearchResult",
17
+ "InternalLoaderMessage",
18
+ "RenderCardRequest",
19
+ "AdsQuery",
20
+ "SemanticSerp"
21
+ ],
22
+ "sliceIds": [],
23
+ "traceId": "63e48421455345f0854475fe03c0c78a",
24
+ "isStartOfSession": true,
25
+ "message": {
26
+ "locale": "en-US",
27
+ "market": "en-US",
28
+ "region": "SG",
29
+ "location": "lat:0.000000;long:0.000000;re=0m;",
30
+ "locationHints": [
31
+ {
32
+ "country": "Singapore",
33
+ "timezoneoffset": 8,
34
+ "countryConfidence": 8,
35
+ "Center": { "Latitude": 0, "Longitude": 0 },
36
+ "RegionType": 2,
37
+ "SourceType": 1
38
+ }
39
+ ],
40
+ "timestamp": "2023-02-09T13:26:58+08:00",
41
+ "author": "user",
42
+ "inputMethod": "Keyboard",
43
+ "text": "Say this is a test",
44
+ "messageType": "Chat"
45
+ },
46
+ "conversationSignature": "<|CONVERSATION_SIGNATURE|>",
47
+ "participant": { "id": "<|CLIENT_ID|>" },
48
+ "conversationId": "<|CONVERSATION_ID|>",
49
+ "previousMessages": [
50
+ {
51
+ "text": "Okay, I’ve cleared the slate for a fresh start. What can I help you explore now?",
52
+ "author": "bot",
53
+ "adaptiveCards": [],
54
+ "suggestedResponses": [
55
+ {
56
+ "text": "Teach me a new word",
57
+ "contentOrigin": "DeepLeo",
58
+ "messageType": "Suggestion",
59
+ "messageId": "0b483c19-409a-746c-9c44-2779234c04e4",
60
+ "offense": "Unknown"
61
+ },
62
+ {
63
+ "text": "Why do humans need sleep?",
64
+ "contentOrigin": "DeepLeo",
65
+ "messageType": "Suggestion",
66
+ "messageId": "b9e18704-b29d-25de-951d-c13aa1b0252c",
67
+ "offense": "Unknown"
68
+ },
69
+ {
70
+ "text": "I want to learn a new skill",
71
+ "contentOrigin": "DeepLeo",
72
+ "messageType": "Suggestion",
73
+ "messageId": "9092c1e7-4ddf-9726-3da6-efadaa8bc9cb",
74
+ "offense": "Unknown"
75
+ }
76
+ ],
77
+ "messageId": "57cda79f-4afa-3723-1bad-394e61f1e590",
78
+ "messageType": "Chat"
79
+ }
80
+ ]
81
+ }
82
+ ],
83
+ "invocationId": "2",
84
+ "target": "chat",
85
+ "type": 4
86
+ }
templates/cookies.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "_U": "1YEL2O07jUf0Rdw3Li02tXbvn47K5pNQcjqdjILEq-9hgsxvyYD9bMjIGxICr3nmVHIcWKgZLVGUVxhdQDHY-WWd8spn_8yNwT-pweGVb6115nmreE8zOa44HLPcHqzvaSOuVVWfip7itwiUOakv6_3SxK2k2YVb9fA-d88MAINsknY9mnaA3-fSXdMea5-K-"
3
+ }