`core.py`
This commit is contained in:
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 = ""
|
|
||||||
|
|
||||||
for chunk in interpreter.chat(messages):
|
# Check 10x a second for new messages
|
||||||
|
message = None
|
||||||
|
while message is None:
|
||||||
|
message = r.lpop('to_core')
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
# Custom stop message will halt us
|
||||||
|
if message.get("content") and message.get("content").lower().strip(".,!") == "stop":
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Load, append, and save conversation history
|
||||||
|
with open("conversations/user.json", "r") as file:
|
||||||
|
messages = json.load(file)
|
||||||
|
messages.append(message)
|
||||||
|
with open("conversations/user.json", "w") as file:
|
||||||
|
json.dump(messages, file)
|
||||||
|
|
||||||
if queue_length() > 0:
|
for chunk in interpreter.chat(messages):
|
||||||
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):
|
# If we have a new message, save our progress and go back to the top
|
||||||
audio = tts(sentence)
|
if r.llen('to_main') > 0:
|
||||||
sentence = ""
|
with open("conversations/user.json", "w") as file:
|
||||||
send_to_io(audio)
|
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 = []
|
||||||
|
@ -75,6 +71,9 @@ def main():
|
||||||
# Send message to core and websocket
|
# Send message to core and websocket
|
||||||
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')
|
||||||
|
|
Loading…
Reference in New Issue