Antonio Cheong
commited on
Commit
·
bf42215
1
Parent(s):
f61bbb2
format
Browse files- README.md +1 -1
- src/BingGPT.py +15 -8
README.md
CHANGED
@@ -1 +1 @@
|
|
1 |
-
# Hello world
|
|
|
1 |
+
# Hello world
|
src/BingGPT.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
"""
|
2 |
Main.py
|
3 |
"""
|
4 |
-
import
|
5 |
import json
|
|
|
6 |
import uuid
|
7 |
|
8 |
-
import asyncio
|
9 |
import requests
|
10 |
import websockets.client as websockets
|
11 |
|
@@ -68,7 +68,7 @@ class ChatHubRequest:
|
|
68 |
"id": self.client_id,
|
69 |
},
|
70 |
"conversationId": self.conversation_id,
|
71 |
-
}
|
72 |
],
|
73 |
"invocationId": str(self.invocation_id),
|
74 |
"target": "chat",
|
@@ -113,7 +113,7 @@ class Conversation:
|
|
113 |
}
|
114 |
# Create cookies
|
115 |
cookies = {
|
116 |
-
"_U": os.environ.get("BING_U")
|
117 |
}
|
118 |
# Send GET request
|
119 |
response = requests.get(
|
@@ -127,6 +127,7 @@ class Conversation:
|
|
127 |
# Return response
|
128 |
self.struct = response.json()
|
129 |
|
|
|
130 |
class ChatHub:
|
131 |
"""
|
132 |
Chat API
|
@@ -153,7 +154,6 @@ class ChatHub:
|
|
153 |
# Make async ping loop (long running)
|
154 |
self.task = asyncio.create_task(self.__ping())
|
155 |
|
156 |
-
|
157 |
async def ask(self, prompt: str) -> str:
|
158 |
"""
|
159 |
Ask a question to the bot
|
@@ -182,7 +182,7 @@ class ChatHub:
|
|
182 |
except asyncio.CancelledError:
|
183 |
break
|
184 |
await self.wss.send(append_identifier({"type": 6}))
|
185 |
-
|
186 |
async def close(self):
|
187 |
"""
|
188 |
Close the connection
|
@@ -196,9 +196,11 @@ class Chatbot:
|
|
196 |
"""
|
197 |
Combines everything to make it seamless
|
198 |
"""
|
|
|
199 |
def __init__(self) -> None:
|
200 |
self.conversation: Conversation
|
201 |
self.chat_hub: ChatHub
|
|
|
202 |
async def a_start(self) -> None:
|
203 |
"""
|
204 |
Separate initialization to allow async
|
@@ -212,6 +214,7 @@ class Chatbot:
|
|
212 |
Ask a question to the bot
|
213 |
"""
|
214 |
return await self.chat_hub.ask(prompt=prompt)
|
|
|
215 |
async def a_close(self):
|
216 |
"""
|
217 |
Close the connection
|
@@ -242,6 +245,7 @@ def get_input(prompt):
|
|
242 |
# Return the input
|
243 |
return user_input
|
244 |
|
|
|
245 |
async def main():
|
246 |
"""
|
247 |
Main function
|
@@ -257,13 +261,16 @@ async def main():
|
|
257 |
print((await bot.a_ask(prompt=prompt))["item"]["messages"][1]["text"])
|
258 |
await bot.a_close()
|
259 |
|
|
|
260 |
if __name__ == "__main__":
|
261 |
-
print(
|
|
|
262 |
BingGPT - A demo of reverse engineering the Bing GPT chatbot
|
263 |
Repo: github.com/acheong08/BingGPT
|
264 |
By: Antonio Cheong
|
265 |
|
266 |
Type !exit to exit
|
267 |
Enter twice to send message
|
268 |
-
"""
|
|
|
269 |
asyncio.run(main())
|
|
|
1 |
"""
|
2 |
Main.py
|
3 |
"""
|
4 |
+
import asyncio
|
5 |
import json
|
6 |
+
import os
|
7 |
import uuid
|
8 |
|
|
|
9 |
import requests
|
10 |
import websockets.client as websockets
|
11 |
|
|
|
68 |
"id": self.client_id,
|
69 |
},
|
70 |
"conversationId": self.conversation_id,
|
71 |
+
},
|
72 |
],
|
73 |
"invocationId": str(self.invocation_id),
|
74 |
"target": "chat",
|
|
|
113 |
}
|
114 |
# Create cookies
|
115 |
cookies = {
|
116 |
+
"_U": os.environ.get("BING_U"),
|
117 |
}
|
118 |
# Send GET request
|
119 |
response = requests.get(
|
|
|
127 |
# Return response
|
128 |
self.struct = response.json()
|
129 |
|
130 |
+
|
131 |
class ChatHub:
|
132 |
"""
|
133 |
Chat API
|
|
|
154 |
# Make async ping loop (long running)
|
155 |
self.task = asyncio.create_task(self.__ping())
|
156 |
|
|
|
157 |
async def ask(self, prompt: str) -> str:
|
158 |
"""
|
159 |
Ask a question to the bot
|
|
|
182 |
except asyncio.CancelledError:
|
183 |
break
|
184 |
await self.wss.send(append_identifier({"type": 6}))
|
185 |
+
|
186 |
async def close(self):
|
187 |
"""
|
188 |
Close the connection
|
|
|
196 |
"""
|
197 |
Combines everything to make it seamless
|
198 |
"""
|
199 |
+
|
200 |
def __init__(self) -> None:
|
201 |
self.conversation: Conversation
|
202 |
self.chat_hub: ChatHub
|
203 |
+
|
204 |
async def a_start(self) -> None:
|
205 |
"""
|
206 |
Separate initialization to allow async
|
|
|
214 |
Ask a question to the bot
|
215 |
"""
|
216 |
return await self.chat_hub.ask(prompt=prompt)
|
217 |
+
|
218 |
async def a_close(self):
|
219 |
"""
|
220 |
Close the connection
|
|
|
245 |
# Return the input
|
246 |
return user_input
|
247 |
|
248 |
+
|
249 |
async def main():
|
250 |
"""
|
251 |
Main function
|
|
|
261 |
print((await bot.a_ask(prompt=prompt))["item"]["messages"][1]["text"])
|
262 |
await bot.a_close()
|
263 |
|
264 |
+
|
265 |
if __name__ == "__main__":
|
266 |
+
print(
|
267 |
+
"""
|
268 |
BingGPT - A demo of reverse engineering the Bing GPT chatbot
|
269 |
Repo: github.com/acheong08/BingGPT
|
270 |
By: Antonio Cheong
|
271 |
|
272 |
Type !exit to exit
|
273 |
Enter twice to send message
|
274 |
+
"""
|
275 |
+
)
|
276 |
asyncio.run(main())
|