@@ -60,7 +60,7 @@ def __init__(self, *args, **kwargs):
60
60
self .server = Server (self )
61
61
62
62
# For the 01. This lets the OAI compatible server accumulate context before responding.
63
- self .context_mode = True
63
+ self .context_mode = False
64
64
65
65
async def input (self , chunk ):
66
66
"""
@@ -723,7 +723,39 @@ class ChatCompletionRequest(BaseModel):
723
723
temperature : Optional [float ] = None
724
724
stream : Optional [bool ] = False
725
725
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
+
727
759
made_chunk = False
728
760
729
761
for message in [
@@ -740,6 +772,12 @@ async def openai_compatible_generator():
740
772
await asyncio .sleep (0 ) # Yield control to the event loop
741
773
made_chunk = True
742
774
775
+ if (
776
+ chunk ["type" ] == "confirmation"
777
+ and async_interpreter .auto_run == False
778
+ ):
779
+ break
780
+
743
781
if async_interpreter .stop_event .is_set ():
744
782
break
745
783
@@ -749,6 +787,10 @@ async def openai_compatible_generator():
749
787
output_content = chunk ["content" ]
750
788
if chunk ["type" ] == "code" and "start" in chunk :
751
789
output_content = " "
790
+ if chunk ["type" ] == "code" and "content" in chunk :
791
+ output_content = (
792
+ f"""<unvoiced code="{ chunk ["content" ]} "></unvoiced>"""
793
+ )
752
794
753
795
if output_content :
754
796
await asyncio .sleep (0 )
@@ -764,6 +806,18 @@ async def openai_compatible_generator():
764
806
if made_chunk :
765
807
break
766
808
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
+
767
821
@router .post ("/openai/chat/completions" )
768
822
async def chat_completion (request : ChatCompletionRequest ):
769
823
global last_start_time
@@ -776,6 +830,9 @@ async def chat_completion(request: ChatCompletionRequest):
776
830
777
831
if last_message .content == "{STOP}" :
778
832
# Handle special STOP token
833
+ async_interpreter .stop_event .set ()
834
+ time .sleep (5 )
835
+ async_interpreter .stop_event .clear ()
779
836
return
780
837
781
838
if last_message .content in ["{CONTEXT_MODE_ON}" , "{REQUIRE_START_ON}" ]:
@@ -786,6 +843,14 @@ async def chat_completion(request: ChatCompletionRequest):
786
843
async_interpreter .context_mode = False
787
844
return
788
845
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
+
789
854
if type (last_message .content ) == str :
790
855
async_interpreter .messages .append (
791
856
{
@@ -825,43 +890,49 @@ async def chat_completion(request: ChatCompletionRequest):
825
890
}
826
891
)
827
892
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
833
925
# Remove that {START} message that would have just been added
834
926
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
849
927
return
850
928
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
-
858
929
async_interpreter .stop_event .set ()
859
930
time .sleep (0.1 )
860
931
async_interpreter .stop_event .clear ()
861
932
862
933
if request .stream :
863
934
return StreamingResponse (
864
- openai_compatible_generator (), media_type = "application/x-ndjson"
935
+ openai_compatible_generator (run_code ), media_type = "application/x-ndjson"
865
936
)
866
937
else :
867
938
messages = async_interpreter .chat (message = "." , stream = False , display = True )
0 commit comments