diff --git a/OS/01/conversations/user.json b/OS/01/conversations/user.json index 32c2cb4..8885753 100644 --- a/OS/01/conversations/user.json +++ b/OS/01/conversations/user.json @@ -1 +1 @@ -[{"role": "user", "type": "message", "content": "Yeah, it's explaining why you have to be a paramedic.\n"}, {"role": "user", "type": "message", "content": "\uc5b4\ub9b4\ub54c \uad1c\ucc2e\uc558\ub294\ub370 \uc544 \uadf8\ub798\uc11c \uc544\uce68\uc5d0 \uc9c4\uc9dc \uc548\uac00\uc9c0\uace0 \uc654\ub098\ubd10\uc694 \uc57c \ub098 \uc6ec\ub9cc\ud07c \ub9db\uc788\ub294\ub370\n"}, {"role": "user", "type": "message", "content": "Like, you'd have to go, like, out of houses.\n"}] \ No newline at end of file +[{"role": "user", "type": "message", "content": "Hi, please just immediately run print hello world, don't do anything else.\n"}, {"role": "user", "type": "message", "content": "Can you calculate something like 1,933 times 2,345? Again, please just do it immediately. Don't do anything else. Just please run the code. Don't send a message to me.\n"}] \ No newline at end of file diff --git a/OS/01/device.py b/OS/01/device.py index 080cb54..299dc04 100644 --- a/OS/01/device.py +++ b/OS/01/device.py @@ -137,11 +137,15 @@ async def websocket_communication(WS_URL): message_so_far = {"role": None, "type": None, "format": None, "content": None} - async for message in websocket: + while True: + message = await websocket.recv() - print(message) + print("Got this message from the server:", type(message), message) - if "content" in message_so_far: + if type(message) == str: + message = json.loads(message) + + if "content" in message: if any(message_so_far[key] != message[key] for key in message_so_far): message_so_far = message else: diff --git a/OS/01/server.py b/OS/01/server.py index ad31c9c..10142d2 100644 --- a/OS/01/server.py +++ b/OS/01/server.py @@ -109,7 +109,7 @@ async def receive_messages(websocket: WebSocket): async def send_messages(websocket: WebSocket): while True: message = await to_device.get() - print(message) + print("Sending to the device:", type(message), message) await websocket.send_json(message) async def user_listener(): @@ -117,6 +117,9 @@ async def user_listener(): while True: message = await from_user.get() + if type(message) == str: + message = json.loads(message) + # Hold the audio in a buffer. If it's ready (we got end flag, stt it) if message["type"] == "audio": if "content" in message: @@ -131,7 +134,7 @@ async def user_listener(): continue # Ignore flags, we only needed them for audio ^ - if "content" not in message: + if "content" not in message or message["content"] == None: continue # Custom stop message will halt us @@ -147,10 +150,14 @@ async def user_listener(): accumulated_text = "" - for chunk in interpreter.chat(messages, stream=True): + for chunk in interpreter.chat(messages, stream=True, display=False): + + print("Got chunk:", chunk) # Send it to the user await to_device.put(chunk) + # Yield to the event loop, so you actually send it out + await asyncio.sleep(0.01) # Speak full sentences out loud if chunk["role"] == "assistant" and "content" in chunk: @@ -167,8 +174,21 @@ async def user_listener(): # If we have a new message, save our progress and go back to the top if not from_user.empty(): + + # Check if it's just an end flag. We ignore those. + temp_message = await from_user.get() + + if temp_message == {'role': 'user', 'type': 'message', 'end': True}: + # Yup. False alarm. + continue + else: + # Whoops! Put that back + await from_user.put(temp_message) + with open(conversation_history_path, 'w') as file: json.dump(interpreter.messages, file) + + print("New message recieved. Breaking.") break async def stream_or_play_tts(sentence):