@@ -60,7 +60,7 @@ def __init__(self, *args, **kwargs):
6060 self .server = Server (self )
6161
6262 # For the 01. This lets the OAI compatible server accumulate context before responding.
63- self .context_mode = True
63+ self .context_mode = False
6464
6565 async def input (self , chunk ):
6666 """
@@ -723,7 +723,39 @@ class ChatCompletionRequest(BaseModel):
723723 temperature : Optional [float ] = None
724724 stream : Optional [bool ] = False
725725
726- async def openai_compatible_generator ():
726+ async def openai_compatible_generator (run_code ):
727+ if run_code :
728+ print ("Running code.\n " )
729+ for i , chunk in enumerate (async_interpreter ._respond_and_store ()):
730+ if "content" in chunk :
731+ print (chunk ["content" ], end = "" ) # Sorry! Shitty display for now
732+ if "start" in chunk :
733+ print ("\n " )
734+
735+ output_content = None
736+
737+ if chunk ["type" ] == "message" and "content" in chunk :
738+ output_content = chunk ["content" ]
739+ if chunk ["type" ] == "code" and "start" in chunk :
740+ output_content = " "
741+ if chunk ["type" ] == "code" and "content" in chunk :
742+ output_content = (
743+ f"""<unvoiced code="{ chunk ["content" ]} "></unvoiced>"""
744+ )
745+
746+ if output_content :
747+ await asyncio .sleep (0 )
748+ output_chunk = {
749+ "id" : i ,
750+ "object" : "chat.completion.chunk" ,
751+ "created" : time .time (),
752+ "model" : "open-interpreter" ,
753+ "choices" : [{"delta" : {"content" : output_content }}],
754+ }
755+ yield f"data: { json .dumps (output_chunk )} \n \n "
756+
757+ return
758+
727759 made_chunk = False
728760
729761 for message in [
@@ -740,6 +772,12 @@ async def openai_compatible_generator():
740772 await asyncio .sleep (0 ) # Yield control to the event loop
741773 made_chunk = True
742774
775+ if (
776+ chunk ["type" ] == "confirmation"
777+ and async_interpreter .auto_run == False
778+ ):
779+ break
780+
743781 if async_interpreter .stop_event .is_set ():
744782 break
745783
@@ -749,6 +787,10 @@ async def openai_compatible_generator():
749787 output_content = chunk ["content" ]
750788 if chunk ["type" ] == "code" and "start" in chunk :
751789 output_content = " "
790+ if chunk ["type" ] == "code" and "content" in chunk :
791+ output_content = (
792+ f"""<unvoiced code="{ chunk ["content" ]} "></unvoiced>"""
793+ )
752794
753795 if output_content :
754796 await asyncio .sleep (0 )
@@ -764,6 +806,18 @@ async def openai_compatible_generator():
764806 if made_chunk :
765807 break
766808
809+ if async_interpreter .messages [- 1 ]["type" ] == "code" :
810+ await asyncio .sleep (0 )
811+ output_content = "{CODE_FINISHED}"
812+ output_chunk = {
813+ "id" : i ,
814+ "object" : "chat.completion.chunk" ,
815+ "created" : time .time (),
816+ "model" : "open-interpreter" ,
817+ "choices" : [{"delta" : {"content" : output_content }}],
818+ }
819+ yield f"data: { json .dumps (output_chunk )} \n \n "
820+
767821 @router .post ("/openai/chat/completions" )
768822 async def chat_completion (request : ChatCompletionRequest ):
769823 global last_start_time
@@ -776,6 +830,9 @@ async def chat_completion(request: ChatCompletionRequest):
776830
777831 if last_message .content == "{STOP}" :
778832 # Handle special STOP token
833+ async_interpreter .stop_event .set ()
834+ time .sleep (5 )
835+ async_interpreter .stop_event .clear ()
779836 return
780837
781838 if last_message .content in ["{CONTEXT_MODE_ON}" , "{REQUIRE_START_ON}" ]:
@@ -786,6 +843,14 @@ async def chat_completion(request: ChatCompletionRequest):
786843 async_interpreter .context_mode = False
787844 return
788845
846+ if last_message .content == "{AUTO_RUN_ON}" :
847+ async_interpreter .auto_run = True
848+ return
849+
850+ if last_message .content == "{AUTO_RUN_OFF}" :
851+ async_interpreter .auto_run = False
852+ return
853+
789854 if type (last_message .content ) == str :
790855 async_interpreter .messages .append (
791856 {
@@ -825,43 +890,49 @@ async def chat_completion(request: ChatCompletionRequest):
825890 }
826891 )
827892
828- if async_interpreter .context_mode :
829- # In context mode, we only respond if we recieved a {START} message
830- # Otherwise, we're just accumulating context
831- if last_message .content == "{START}" :
832- if async_interpreter .messages [- 1 ]["content" ] == "{START}" :
893+ run_code = False
894+ if last_message .content == "{RUN}" :
895+ run_code = True
896+ # Remove that {RUN} message that would have just been added
897+ async_interpreter .messages = async_interpreter .messages [:- 1 ]
898+ else :
899+ if async_interpreter .context_mode :
900+ # In context mode, we only respond if we recieved a {START} message
901+ # Otherwise, we're just accumulating context
902+ if last_message .content == "{START}" :
903+ if async_interpreter .messages [- 1 ]["content" ] == "{START}" :
904+ # Remove that {START} message that would have just been added
905+ async_interpreter .messages = async_interpreter .messages [:- 1 ]
906+ last_start_time = time .time ()
907+ if (
908+ async_interpreter .messages
909+ and async_interpreter .messages [- 1 ].get ("role" ) != "user"
910+ ):
911+ return
912+ else :
913+ # Check if we're within 6 seconds of last_start_time
914+ current_time = time .time ()
915+ if current_time - last_start_time <= 6 :
916+ # Continue processing
917+ pass
918+ else :
919+ # More than 6 seconds have passed, so return
920+ return
921+
922+ else :
923+ if last_message .content == "{START}" :
924+ # This just sometimes happens I guess
833925 # Remove that {START} message that would have just been added
834926 async_interpreter .messages = async_interpreter .messages [:- 1 ]
835- last_start_time = time .time ()
836- if (
837- async_interpreter .messages
838- and async_interpreter .messages [- 1 ].get ("role" ) != "user"
839- ):
840- return
841- else :
842- # Check if we're within 6 seconds of last_start_time
843- current_time = time .time ()
844- if current_time - last_start_time <= 6 :
845- # Continue processing
846- pass
847- else :
848- # More than 6 seconds have passed, so return
849927 return
850928
851- else :
852- if last_message .content == "{START}" :
853- # This just sometimes happens I guess
854- # Remove that {START} message that would have just been added
855- async_interpreter .messages = async_interpreter .messages [:- 1 ]
856- return
857-
858929 async_interpreter .stop_event .set ()
859930 time .sleep (0.1 )
860931 async_interpreter .stop_event .clear ()
861932
862933 if request .stream :
863934 return StreamingResponse (
864- openai_compatible_generator (), media_type = "application/x-ndjson"
935+ openai_compatible_generator (run_code ), media_type = "application/x-ndjson"
865936 )
866937 else :
867938 messages = async_interpreter .chat (message = "." , stream = False , display = True )
0 commit comments