pull/3/head
killian 12 months ago
parent 3f92df120b
commit 9359f1dd91

@ -1,28 +1,38 @@
import redis
import json
import time
# Set up Redis connection
r = redis.Redis(host='localhost', port=6379, db=0)
def main(interpreter):
while True: while True:
# Check 10x a second for new messages
message = None message = None
while message is None: while message is None:
message = get_from_queue('to_main') message = r.lpop('to_core')
time.sleep(0.1)
if message == user_start_message: # Custom stop message will halt us
if message.get("content") and message.get("content").lower().strip(".,!") == "stop":
continue continue
messages = get_conversation_history() # Load, append, and save conversation history
with open("conversations/user.json", "r") as file:
messages = json.load(file)
messages.append(message) messages.append(message)
save_conversation_history(message) with open("conversations/user.json", "w") as file:
json.dump(messages, file)
sentence = ""
for chunk in interpreter.chat(messages): for chunk in interpreter.chat(messages):
if queue_length() > 0: # Send it to the interface
save_conversation_history(interpreter.messages) r.rpush('to_interface', chunk)
break
send_to_io(chunk)
sentence += chunk # If we have a new message, save our progress and go back to the top
if is_full_sentence(sentence): if r.llen('to_main') > 0:
audio = tts(sentence) with open("conversations/user.json", "w") as file:
sentence = "" json.dump(interpreter.messages, file)
send_to_io(audio) break

@ -25,10 +25,6 @@ sample_rate = 44100 # Hz
# Set up Redis connection # Set up Redis connection
r = redis.Redis(host='localhost', port=6379, db=0) r = redis.Redis(host='localhost', port=6379, db=0)
# Define some standard, useful messages
user_start_message = {"role": "user", "type": "message", "start": True}
user_start_message = {"role": "user", "type": "message", "start": True}
# Set up websocket connection # Set up websocket connection
websocket = websockets.connect('ws://localhost:8765') websocket = websockets.connect('ws://localhost:8765')
@ -54,9 +50,9 @@ def main():
# If the button is pushed down # If the button is pushed down
if not GPIO.input(18): if not GPIO.input(18):
# Send start message to core and websocket # Tell websocket and core that the user is speaking
r.rpush('to_core', user_start_message) send_to_websocket({"role": "user", "type": "message", "start": True}) # Standard start flag, required per streaming LMC protocol (https://docs.openinterpreter.com/guides/streaming-response)
send_to_websocket(user_start_message) r.rpush('to_core', {"role": "user", "type": "message", "content": "stop"}) # Custom stop message. Core is not streaming LMC (it's static LMC) so doesn't require that ^ flag
# Record audio from the microphone in chunks # Record audio from the microphone in chunks
audio_chunks = [] audio_chunks = []
@ -76,6 +72,9 @@ def main():
r.rpush('to_core', message) r.rpush('to_core', message)
send_to_websocket(message) send_to_websocket(message)
# Send user message end flag to websocket, required per streaming LMC protocol
send_to_websocket({"role": "user", "type": "message", "end": True})
# Send out anything in the to_interface queue # Send out anything in the to_interface queue
chunk = r.lpop('to_interface') chunk = r.lpop('to_interface')
if chunk: if chunk:

Loading…
Cancel
Save