File size: 10,528 Bytes
7c908ce
 
 
f71f123
bf42215
7c908ce
bf42215
e097d14
ef4de38
7c908ce
9f7a351
7c908ce
8532036
 
867d2ff
 
 
 
 
 
 
7c908ce
85dc37d
44edfa0
 
 
 
7c908ce
 
 
 
 
8532036
7c908ce
 
 
 
 
 
ef4de38
7c908ce
 
 
 
 
9f7a351
7c908ce
 
 
 
 
 
 
ef4de38
7c908ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f7a351
7c908ce
 
 
 
 
 
9f7a351
 
 
 
 
bf42215
7c908ce
9f7a351
7c908ce
 
 
9f7a351
7c908ce
ef4de38
7c908ce
 
 
 
 
 
ef4de38
 
 
 
 
 
867d2ff
7c908ce
bcd6bf2
7b516d5
 
 
 
 
 
 
 
 
 
 
85dc37d
 
 
 
 
7b516d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e24a429
 
bcd6bf2
e24a429
 
7b516d5
 
 
 
 
867d2ff
7b516d5
 
 
9ce9327
 
44edfa0
 
 
0795fe7
f71f123
41a5bc2
7c908ce
bf42215
7c908ce
 
 
 
 
ccd7677
 
9f7a351
 
4af2909
9f7a351
 
 
 
 
 
bcd6bf2
 
 
 
 
 
 
 
f71f123
867d2ff
 
bcd6bf2
 
 
85dc37d
 
 
 
 
bcd6bf2
 
 
 
 
8532036
 
 
bcd6bf2
 
 
 
 
f71f123
 
 
bcd6bf2
8532036
4332868
bcd6bf2
9f7a351
 
3334976
bf42215
9f7a351
7c908ce
9f7a351
7c908ce
c7d7628
 
d9fec1d
ef4de38
e6fd6e9
9f7a351
7c908ce
9f7a351
7c908ce
bf42215
9f7a351
ccd7677
7c908ce
72e3a11
9f7a351
 
 
275d493
 
 
f71f123
bcd6bf2
 
 
 
 
 
bf42215
e097d14
9f7a351
 
 
 
3334976
e097d14
 
 
 
 
ccd7677
7c908ce
 
af09b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf42215
9f7a351
af09b23
 
 
9f7a351
 
 
af09b23
9f7a351
 
e097d14
3334976
 
e097d14
 
 
0795fe7
3334976
e097d14
 
 
 
f61bbb2
d7360ed
f71f123
 
 
127a2a5
f71f123
bcd6bf2
8532036
 
 
 
 
 
bcd6bf2
 
e097d14
af09b23
bf42215
9f7a351
bf42215
 
3de061f
 
af09b23
 
e097d14
 
af09b23
 
0795fe7
bf42215
bcd6bf2
d7360ed
85dc37d
bcd6bf2
 
 
7c908ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""
Main.py
"""
import argparse
import asyncio
import json
import os
import sys

import requests
import websockets.client as websockets

DELIMITER = "\x1e"

headers = {
    "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.41",
    "origin": "https://www.bing.com",
    "referer": "https://www.bing.com/",
    "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
    "sec-ch-ua-platform": "Windows",
}


class NotAllowedToAccess(Exception):
    pass


def append_identifier(msg: dict) -> str:
    """
    Appends special character to end of message to identify end of message
    """
    # Convert dict to json string
    return json.dumps(msg) + DELIMITER


class ChatHubRequest:
    """
    Request object for ChatHub
    """

    def __init__(
        self,
        conversation_signature: str,
        client_id: str,
        conversation_id: str,
        invocation_id: int = 0,
    ) -> None:
        self.struct: dict

        self.client_id: str = client_id
        self.conversation_id: str = conversation_id
        self.conversation_signature: str = conversation_signature
        self.invocation_id: int = invocation_id

    def update(
        self,
        prompt: str,
    ) -> None:
        """
        Updates request object
        """
        self.struct = {
            "arguments": [
                {
                    "source": "cib",
                    "optionsSets": [
                        "nlu_direct_response_filter",
                        "deepleo",
                        "enable_debug_commands",
                        "disable_emoji_spoken_text",
                        "responsible_ai_policy_235",
                        "enablemm",
                    ],
                    "isStartOfSession": self.invocation_id == 0,
                    "message": {
                        "author": "user",
                        "inputMethod": "Keyboard",
                        "text": prompt,
                        "messageType": "Chat",
                    },
                    "conversationSignature": self.conversation_signature,
                    "participant": {
                        "id": self.client_id,
                    },
                    "conversationId": self.conversation_id,
                },
            ],
            "invocationId": str(self.invocation_id),
            "target": "chat",
            "type": 4,
        }
        self.invocation_id += 1


class Conversation:
    """
    Conversation API
    """

    def __init__(self) -> None:
        self.struct: dict = {
            "conversationId": None,
            "clientId": None,
            "conversationSignature": None,
            "result": {"value": "Success", "message": None},
        }
        # POST request to get token
        # Create cookies
        if os.environ.get("BING_U") is None:
            home = os.path.expanduser("~")
            # Check if token exists
            token_path = f"{home}/.config/bing_token"
            # Make .config directory if it doesn't exist
            if not os.path.exists(f"{home}/.config"):
                os.mkdir(f"{home}/.config")
            if os.path.exists(token_path):
                with open(token_path, "r", encoding="utf-8") as file:
                    token = file.read()
            else:
                url = "https://images.duti.tech/allow"
                response = requests.post(
                    url,
                    timeout=10,
                    headers=headers,
                )
                if response.status_code != 200:
                    raise Exception("Authentication failed")
                token = response.json()["token"]
                # Save token
                with open(token_path, "w", encoding="utf-8") as file:
                    file.write(token)
            url = "https://images.duti.tech/auth"
            # Send GET request
            response = requests.get(
                url,
                headers=headers,
                timeout=10,
            )
            if response.status_code != 200:
                raise Exception("Authentication failed")

        else:
            cookies = {
                "_U": os.environ.get("BING_U"),
            }
            url = "https://www.bing.com/turing/conversation/create"
            # Send GET request
            response = requests.get(
                url,
                cookies=cookies,
                timeout=30,
                headers=headers,
            )
            if response.status_code != 200:
                raise Exception("Authentication failed")
        try:
            self.struct = response.json()
            if self.struct["result"]["value"] == "UnauthorizedRequest":
                raise NotAllowedToAccess(self.struct["result"]["message"])
        except (json.decoder.JSONDecodeError, NotAllowedToAccess) as exc:
            raise Exception(
                "Authentication failed. You have not been accepted into the beta.",
            ) from exc


class ChatHub:
    """
    Chat API
    """

    def __init__(self, conversation: Conversation) -> None:
        self.wss: websockets.WebSocketClientProtocol = None
        self.request: ChatHubRequest
        self.loop: bool
        self.task: asyncio.Task
        self.request = ChatHubRequest(
            conversation_signature=conversation.struct["conversationSignature"],
            client_id=conversation.struct["clientId"],
            conversation_id=conversation.struct["conversationId"],
        )

    async def ask_stream(self, prompt: str) -> str:
        """
        Ask a question to the bot
        """
        # Check if websocket is closed
        if self.wss:
            if self.wss.closed:
                self.wss = await websockets.connect(
                    "wss://sydney.bing.com/sydney/ChatHub",
                    extra_headers=headers,
                    max_size=None,
                )
                await self.__initial_handshake()
        else:
            self.wss = await websockets.connect(
                "wss://sydney.bing.com/sydney/ChatHub",
                extra_headers=headers,
                max_size=None,
            )
            await self.__initial_handshake()
        # Construct a ChatHub request
        self.request.update(prompt=prompt)
        # Send request
        await self.wss.send(append_identifier(self.request.struct))
        final = False
        while not final:
            objects = str(await self.wss.recv()).split(DELIMITER)
            for obj in objects:
                if obj is None or obj == "":
                    continue
                response = json.loads(obj)
                if response.get("type") == 1:
                    yield False, response["arguments"][0]["messages"][0][
                        "adaptiveCards"
                    ][0]["body"][0]["text"]
                elif response.get("type") == 2:
                    final = True
                    yield True, response

    async def __initial_handshake(self):
        await self.wss.send(append_identifier({"protocol": "json", "version": 1}))
        await self.wss.recv()

    async def close(self):
        """
        Close the connection
        """
        if self.wss:
            if not self.wss.closed:
                await self.wss.close()


class Chatbot:
    """
    Combines everything to make it seamless
    """

    def __init__(self) -> None:
        self.chat_hub: ChatHub = ChatHub(Conversation())

    async def ask(self, prompt: str) -> dict:
        """
        Ask a question to the bot
        """
        async for final, response in self.chat_hub.ask_stream(prompt=prompt):
            if final:
                return response

    async def ask_stream(self, prompt: str) -> str:
        """
        Ask a question to the bot
        """
        async for response in self.chat_hub.ask_stream(prompt=prompt):
            yield response

    async def close(self):
        """
        Close the connection
        """
        await self.chat_hub.close()

    async def reset(self):
        """
        Reset the conversation
        """
        await self.close()
        self.chat_hub = ChatHub(Conversation())


def get_input(prompt):
    """
    Multi-line input function
    """
    # Display the prompt
    print(prompt, end="")

    # Initialize an empty list to store the input lines
    lines = []

    # Read lines of input until the user enters an empty line
    while True:
        line = input()
        if line == "":
            break
        lines.append(line)

    # Join the lines, separated by newlines, and store the result
    user_input = "\n".join(lines)

    # Return the input
    return user_input


async def main():
    """
    Main function
    """
    print("Initializing...")
    bot = Chatbot()
    while True:
        prompt = get_input("\nYou:\n")
        if prompt == "!exit":
            break
        elif prompt == "!help":
            print(
                """
            !help - Show this help message
            !exit - Exit the program
            !reset - Reset the conversation
            """,
            )
            continue
        elif prompt == "!reset":
            await bot.reset()
            continue
        print("Bot:")
        if args.no_stream:
            print(
                (await bot.ask(prompt=prompt))["item"]["messages"][1]["adaptiveCards"][
                    0
                ]["body"][0]["text"],
            )
        else:
            wrote = 0
            async for final, response in bot.ask_stream(prompt=prompt):
                if not final:
                    print(response[wrote:], end="")
                    wrote = len(response)
                    sys.stdout.flush()
            print()
        sys.stdout.flush()
    await bot.close()


if __name__ == "__main__":
    print(
        """
        EdgeGPT - A demo of reverse engineering the Bing GPT chatbot
        Repo: github.com/acheong08/EdgeGPT
        By: Antonio Cheong

        !help for help

        Type !exit to exit
        Enter twice to send message
    """,
    )
    parser = argparse.ArgumentParser()
    parser.add_argument("--no-stream", action="store_true")
    parser.add_argument("--bing-cookie", type=str, default="", required=True)
    args = parser.parse_args()
    if args.bing_cookie:
        os.environ["BING_U"] = args.bing_cookie
    asyncio.run(main())