James Zeng
commited on
Commit
·
8532036
1
Parent(s):
295085f
streaming behavior change, and not more non-printable char in the code
Browse files- src/EdgeGPT.py +16 -16
src/EdgeGPT.py
CHANGED
@@ -11,13 +11,15 @@ import argparse
|
|
11 |
import requests
|
12 |
import websockets.client as websockets
|
13 |
|
|
|
|
|
14 |
|
15 |
def append_identifier(msg: dict) -> str:
|
16 |
"""
|
17 |
Appends special character to end of message to identify end of message
|
18 |
"""
|
19 |
# Convert dict to json string
|
20 |
-
return json.dumps(msg) +
|
21 |
|
22 |
|
23 |
class ChatHubRequest:
|
@@ -175,7 +177,7 @@ class ChatHub:
|
|
175 |
# Send request
|
176 |
await self.wss.send(append_identifier(self.request.struct))
|
177 |
while True:
|
178 |
-
objects = str(await self.wss.recv()).split(
|
179 |
for obj in objects:
|
180 |
if obj is None or obj == "":
|
181 |
continue
|
@@ -201,21 +203,18 @@ class ChatHub:
|
|
201 |
self.request.update(prompt=prompt)
|
202 |
# Send request
|
203 |
await self.wss.send(append_identifier(self.request.struct))
|
204 |
-
|
205 |
-
while
|
206 |
-
objects = str(await self.wss.recv()).split(
|
207 |
for obj in objects:
|
208 |
if obj is None or obj == "":
|
209 |
continue
|
210 |
response = json.loads(obj)
|
211 |
if response.get("type") == 1:
|
212 |
-
|
213 |
-
adaptive_card = response["arguments"][0]["messages"][0]["adaptiveCards"][0]["body"][0]["text"]
|
214 |
-
stream_cache = adaptive_card
|
215 |
-
adaptive_card = adaptive_card[cache_len:]
|
216 |
-
yield adaptive_card
|
217 |
elif response.get("type") == 2:
|
218 |
-
|
|
|
219 |
|
220 |
async def __initial_handshake(self):
|
221 |
await self.wss.send(append_identifier({"protocol": "json", "version": 1}))
|
@@ -319,11 +318,12 @@ async def main():
|
|
319 |
][0]["text"],
|
320 |
)
|
321 |
else:
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
|
|
327 |
print()
|
328 |
sys.stdout.flush()
|
329 |
await bot.close()
|
|
|
11 |
import requests
|
12 |
import websockets.client as websockets
|
13 |
|
14 |
+
DELIMITER = "\x1e"
|
15 |
+
|
16 |
|
17 |
def append_identifier(msg: dict) -> str:
|
18 |
"""
|
19 |
Appends special character to end of message to identify end of message
|
20 |
"""
|
21 |
# Convert dict to json string
|
22 |
+
return json.dumps(msg) + DELIMITER
|
23 |
|
24 |
|
25 |
class ChatHubRequest:
|
|
|
177 |
# Send request
|
178 |
await self.wss.send(append_identifier(self.request.struct))
|
179 |
while True:
|
180 |
+
objects = str(await self.wss.recv()).split(DELIMITER)
|
181 |
for obj in objects:
|
182 |
if obj is None or obj == "":
|
183 |
continue
|
|
|
203 |
self.request.update(prompt=prompt)
|
204 |
# Send request
|
205 |
await self.wss.send(append_identifier(self.request.struct))
|
206 |
+
final = False
|
207 |
+
while not final:
|
208 |
+
objects = str(await self.wss.recv()).split(DELIMITER)
|
209 |
for obj in objects:
|
210 |
if obj is None or obj == "":
|
211 |
continue
|
212 |
response = json.loads(obj)
|
213 |
if response.get("type") == 1:
|
214 |
+
yield False, response["arguments"][0]["messages"][0]["adaptiveCards"][0]["body"][0]["text"]
|
|
|
|
|
|
|
|
|
215 |
elif response.get("type") == 2:
|
216 |
+
final = True
|
217 |
+
yield True, response["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"]
|
218 |
|
219 |
async def __initial_handshake(self):
|
220 |
await self.wss.send(append_identifier({"protocol": "json", "version": 1}))
|
|
|
318 |
][0]["text"],
|
319 |
)
|
320 |
else:
|
321 |
+
wrote = 0
|
322 |
+
async for final, response in bot.ask_stream(prompt=prompt):
|
323 |
+
if not final:
|
324 |
+
print(response[wrote:], end="")
|
325 |
+
wrote = len(response)
|
326 |
+
sys.stdout.flush()
|
327 |
print()
|
328 |
sys.stdout.flush()
|
329 |
await bot.close()
|