James Zeng
commited on
Commit
·
275d493
1
Parent(s):
82aadb5
ask is merged into ask_stream
Browse files- src/EdgeGPT.py +4 -33
src/EdgeGPT.py
CHANGED
@@ -158,33 +158,6 @@ class ChatHub:
|
|
158 |
conversation_id=conversation.struct["conversationId"],
|
159 |
)
|
160 |
|
161 |
-
async def ask(self, prompt: str) -> dict:
|
162 |
-
"""
|
163 |
-
Ask a question to the bot
|
164 |
-
"""
|
165 |
-
# Check if websocket is closed
|
166 |
-
if self.wss:
|
167 |
-
if self.wss.closed:
|
168 |
-
self.wss = await websockets.connect(
|
169 |
-
"wss://sydney.bing.com/sydney/ChatHub"
|
170 |
-
)
|
171 |
-
await self.__initial_handshake()
|
172 |
-
else:
|
173 |
-
self.wss = await websockets.connect("wss://sydney.bing.com/sydney/ChatHub")
|
174 |
-
await self.__initial_handshake()
|
175 |
-
# Construct a ChatHub request
|
176 |
-
self.request.update(prompt=prompt)
|
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
|
184 |
-
response = json.loads(obj)
|
185 |
-
if response.get("type") == 2:
|
186 |
-
return response
|
187 |
-
|
188 |
async def ask_stream(self, prompt: str) -> str:
|
189 |
"""
|
190 |
Ask a question to the bot
|
@@ -241,7 +214,9 @@ class Chatbot:
|
|
241 |
"""
|
242 |
Ask a question to the bot
|
243 |
"""
|
244 |
-
|
|
|
|
|
245 |
|
246 |
async def ask_stream(self, prompt: str) -> str:
|
247 |
"""
|
@@ -312,11 +287,7 @@ async def main():
|
|
312 |
continue
|
313 |
print("Bot:")
|
314 |
if not args.stream:
|
315 |
-
print(
|
316 |
-
(await bot.ask(prompt=prompt))["item"]["messages"][1]["adaptiveCards"][0][
|
317 |
-
"body"
|
318 |
-
][0]["text"],
|
319 |
-
)
|
320 |
else:
|
321 |
wrote = 0
|
322 |
async for final, response in bot.ask_stream(prompt=prompt):
|
|
|
158 |
conversation_id=conversation.struct["conversationId"],
|
159 |
)
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
async def ask_stream(self, prompt: str) -> str:
|
162 |
"""
|
163 |
Ask a question to the bot
|
|
|
214 |
"""
|
215 |
Ask a question to the bot
|
216 |
"""
|
217 |
+
async for final, response in self.chat_hub.ask_stream(prompt=prompt):
|
218 |
+
if final:
|
219 |
+
return response
|
220 |
|
221 |
async def ask_stream(self, prompt: str) -> str:
|
222 |
"""
|
|
|
287 |
continue
|
288 |
print("Bot:")
|
289 |
if not args.stream:
|
290 |
+
print(await bot.ask(prompt=prompt))
|
|
|
|
|
|
|
|
|
291 |
else:
|
292 |
wrote = 0
|
293 |
async for final, response in bot.ask_stream(prompt=prompt):
|