@ -14,7 +14,7 @@ import os
import glob
import glob
def check_queue ( ) :
def check_queue ( ) :
queue_files = glob . glob ( " /queue/*.json" )
queue_files = glob . glob ( " interpreter /queue/*.json" )
if queue_files :
if queue_files :
with open ( queue_files [ 0 ] , ' r ' ) as file :
with open ( queue_files [ 0 ] , ' r ' ) as file :
data = json . load ( file )
data = json . load ( file )
@ -24,15 +24,15 @@ def check_queue():
return None
return None
def save_conversation ( messages ) :
def save_conversation ( messages ) :
with open ( ' /conversations/user.json' , ' w ' ) as file :
with open ( ' interpreter /conversations/user.json' , ' w ' ) as file :
json . dump ( messages , file )
json . dump ( messages , file )
def load_conversation ( ) :
def load_conversation ( ) :
try :
try :
with open ( ' /conversations/user.json' , ' r ' ) as file :
with open ( ' interpreter /conversations/user.json' , ' r ' ) as file :
messages = json . load ( file )
messages = json . load ( file )
return messages
return messages
except FileNotFoundError :
except ( FileNotFoundError , json . JSONDecodeError ) :
return [ ]
return [ ]
def main ( interpreter ) :
def main ( interpreter ) :
@ -42,55 +42,72 @@ def main(interpreter):
@app.websocket ( " / " )
@app.websocket ( " / " )
async def i_test ( websocket : WebSocket ) :
async def i_test ( websocket : WebSocket ) :
await websocket . accept ( )
await websocket . accept ( )
data = None
while True :
while True :
data = await websocket . receive_text ( )
# This is the task for waiting for the user to send any message at all.
while data . strip ( ) . lower ( ) != " stop " : # Stop command
task = asyncio . create_task ( websocket . receive_text ( ) )
task = asyncio . create_task ( websocket . receive_text ( ) )
if data == None : # Data will have stuff in it if we inturrupted it.
# This would be terrible for production. Just for testing.
while True :
try :
# Has the user sent a message?
data_dict = json . loads ( data )
if task . done ( ) :
if set ( data_dict . keys ( ) ) == { " role " , " content " , " type " } or set (
data = task . result ( )
data_dict . keys ( )
break
) == { " role " , " content " , " type " , " format " } :
data = data_dict
# Has the queue recieved a message?
except json . JSONDecodeError :
pass
for response in interpreter . chat (
message = data , stream = True , display = False
) :
# Check queue
queued_message = check_queue ( )
queued_message = check_queue ( )
if queued_message :
if queued_message :
data = queued_message
data = queued_message
break
break
if task . done ( ) :
data = task . result ( ) # Get the new message
break # Break the loop and start processing the new message
# Send out assistant message chunks
# Wait 0.2 seconds
if (
await asyncio . sleep ( 0.2 )
response . get ( " type " ) == " message "
and response [ " role " ] == " assistant "
### FOR DEV ONLY: SIMULATE LMC MESSAGES
and " content " in response
# This lets users simulate any kind of LMC message by passing a JSON into the textbox in index.html.
) :
await websocket . send_text ( response [ " content " ] )
try :
await asyncio . sleep ( 0.01 ) # Add a small delay
data_dict = json . loads ( data )
data = data_dict
# If it just finished sending an assistant message, send a newline. Otherwise it looks weird.
except json . JSONDecodeError :
if (
pass
response . get ( " type " ) == " message "
and response [ " role " ] == " assistant "
### CONVERSATION / DISC MANAGEMENT
and response . get ( " end " ) == True
user_message = { " role " : " user " , " type " : " message " , " content " : data }
) :
messages = load_conversation ( )
await websocket . send_text ( " \n " )
messages . append ( user_message )
await asyncio . sleep ( 0.01 ) # Add a small delay
save_conversation ( messages )
if not task . done ( ) :
### RESPONDING
data = (
await task
# This is the task for waiting for user inturruptions.
) # Wait for the next message if it hasn't arrived yet
task = asyncio . create_task ( websocket . receive_text ( ) )
for chunk in interpreter . chat (
messages , stream = True , display = True
) :
print ( chunk )
# Check queue
queued_message = check_queue ( )
if queued_message :
data = queued_message
break
# Check for new user messages
if task . done ( ) :
data = task . result ( ) # Get the new message
break # Break the loop and start processing the new message
# Send out chunks
await websocket . send_json ( chunk )
await asyncio . sleep ( 0.01 ) # Add a small delay
# If the interpreter just finished sending a message, save it
if " end " in chunk :
save_conversation ( interpreter . messages )
data = None
uvicorn . run ( app , host = " 0.0.0.0 " , port = 8000 )
uvicorn . run ( app , host = " 0.0.0.0 " , port = 8000 )