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

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