pull/3/head
killian 11 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:
# Check 10x a second for new messages
message = 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
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)
save_conversation_history(message)
sentence = ""
with open("conversations/user.json", "w") as file:
json.dump(messages, file)
for chunk in interpreter.chat(messages):
if queue_length() > 0:
save_conversation_history(interpreter.messages)
break
send_to_io(chunk)
# Send it to the interface
r.rpush('to_interface', chunk)
sentence += chunk
if is_full_sentence(sentence):
audio = tts(sentence)
sentence = ""
send_to_io(audio)
# If we have a new message, save our progress and go back to the top
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
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
websocket = websockets.connect('ws://localhost:8765')
@ -54,9 +50,9 @@ def main():
# If the button is pushed down
if not GPIO.input(18):
# Send start message to core and websocket
r.rpush('to_core', user_start_message)
send_to_websocket(user_start_message)
# Tell websocket and core that the user is speaking
send_to_websocket({"role": "user", "type": "message", "start": True}) # Standard start flag, required per streaming LMC protocol (https://docs.openinterpreter.com/guides/streaming-response)
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
audio_chunks = []
@ -76,6 +72,9 @@ def main():
r.rpush('to_core', 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
chunk = r.lpop('to_interface')
if chunk:

Loading…
Cancel
Save