@@ -784,158 +784,103 @@ def test_non_streaming_chat_with_bad_words(openai_client, capsys):
784
784
"""
785
785
Test bad_words option in non-streaming chat functionality with the local service
786
786
"""
787
+ base_path = os .getenv ("MODEL_PATH" )
788
+ if base_path :
789
+ model_path = os .path .join (base_path , "ernie-4_5-21b-a3b-bf16-paddle" )
790
+ else :
791
+ model_path = "./ernie-4_5-21b-a3b-bf16-paddle"
787
792
response_0 = openai_client .chat .completions .create (
788
793
model = "default" ,
789
794
messages = [{"role" : "user" , "content" : "Hello, how are you?" }],
790
795
temperature = 1 ,
791
796
top_p = 0.0 ,
792
- max_tokens = 10 ,
797
+ max_tokens = 20 ,
793
798
stream = False ,
799
+ extra_body = {"return_token_ids" : True },
794
800
)
795
- output_0 = []
801
+
796
802
assert hasattr (response_0 , "choices" )
797
803
assert len (response_0 .choices ) > 0
798
804
assert hasattr (response_0 .choices [0 ], "message" )
799
- assert hasattr (response_0 .choices [0 ].message , "content" )
805
+ assert hasattr (response_0 .choices [0 ].message , "completion_token_ids" )
806
+ assert isinstance (response_0 .choices [0 ].message .completion_token_ids , list )
800
807
801
- text_split = response_0 .choices [0 ].message .content .split (" " )
802
- for text in text_split :
803
- output_0 .append (text )
808
+ from fastdeploy .input .ernie_tokenizer import ErnieBotTokenizer
809
+
810
+ tokenizer = ErnieBotTokenizer .from_pretrained (model_path , trust_remote_code = True )
811
+ output_tokens_0 = []
812
+ output_ids_0 = []
813
+ for ids in response_0 .choices [0 ].message .completion_token_ids :
814
+ output_tokens_0 .append (tokenizer .decode (ids ))
815
+ output_ids_0 .append (ids )
804
816
805
817
# add bad words
818
+ bad_tokens = output_tokens_0 [6 :10 ]
819
+ bad_token_ids = output_ids_0 [6 :10 ]
806
820
response_1 = openai_client .chat .completions .create (
807
821
model = "default" ,
808
822
messages = [{"role" : "user" , "content" : "Hello, how are you?" }],
809
823
temperature = 1 ,
810
824
top_p = 0.0 ,
811
- max_tokens = 10 ,
812
- extra_body = {"bad_words" : output_0 [ - 5 :] },
825
+ max_tokens = 20 ,
826
+ extra_body = {"bad_words" : bad_tokens [ - 10 :], "return_token_ids" : True },
813
827
stream = False ,
814
828
)
815
- output_1 = []
816
829
assert hasattr (response_1 , "choices" )
817
830
assert len (response_1 .choices ) > 0
818
831
assert hasattr (response_1 .choices [0 ], "message" )
819
- assert hasattr (response_1 .choices [0 ].message , "content" )
820
- text_split = response_1 .choices [0 ].message .content .split (" " )
821
- for text in text_split :
822
- output_1 .append (text )
823
- assert output_0 not in output_1
824
-
825
-
826
- def test_streaming_chat_with_bad_words (openai_client , capsys ):
827
- """
828
- Test bad_words option in streaming chat functionality with the local service
829
- """
830
- response_0 = openai_client .chat .completions .create (
831
- model = "default" ,
832
- messages = [{"role" : "user" , "content" : "Hello, how are you?" }],
833
- temperature = 1 ,
834
- top_p = 0.0 ,
835
- max_tokens = 10 ,
836
- stream = True ,
837
- )
838
- output_0 = []
839
- for chunk in response_0 :
840
- assert hasattr (chunk , "choices" )
841
- assert len (chunk .choices ) > 0
842
- assert hasattr (chunk .choices [0 ], "delta" )
843
- assert hasattr (chunk .choices [0 ].delta , "content" )
844
- output_0 .append (chunk .choices [0 ].delta .content )
845
-
846
- # add bad words
847
- response_1 = openai_client .chat .completions .create (
848
- model = "default" ,
849
- messages = [{"role" : "user" , "content" : "Hello, how are you?" }],
850
- temperature = 1 ,
851
- top_p = 0.0 ,
852
- max_tokens = 10 ,
853
- extra_body = {"bad_words" : output_0 [- 5 :]},
854
- stream = True ,
855
- )
856
- output_1 = []
857
- for chunk in response_1 :
858
- assert hasattr (chunk , "choices" )
859
- assert len (chunk .choices ) > 0
860
- assert hasattr (chunk .choices [0 ], "delta" )
861
- assert hasattr (chunk .choices [0 ].delta , "content" )
862
- output_1 .append (chunk .choices [0 ].delta .content )
863
- assert output_0 not in output_1
832
+ assert hasattr (response_1 .choices [0 ].message , "completion_token_ids" )
833
+ assert isinstance (response_1 .choices [0 ].message .completion_token_ids , list )
834
+ assert not any (ids in response_1 .choices [0 ].message .completion_token_ids for ids in bad_token_ids )
864
835
865
836
866
837
def test_non_streaming_completion_with_bad_words (openai_client , capsys ):
867
838
"""
868
839
Test bad_words option in non-streaming completion functionality with the local service
869
840
"""
841
+ base_path = os .getenv ("MODEL_PATH" )
842
+ if base_path :
843
+ model_path = os .path .join (base_path , "ernie-4_5-21b-a3b-bf16-paddle" )
844
+ else :
845
+ model_path = "./ernie-4_5-21b-a3b-bf16-paddle"
846
+
870
847
response_0 = openai_client .completions .create (
871
848
model = "default" ,
872
849
prompt = "Hello, how are you?" ,
873
850
temperature = 1 ,
874
851
top_p = 0.0 ,
875
- max_tokens = 10 ,
852
+ max_tokens = 20 ,
876
853
stream = False ,
854
+ extra_body = {"return_token_ids" : True },
877
855
)
878
- output_0 = []
879
856
assert hasattr (response_0 , "choices" )
880
857
assert len (response_0 .choices ) > 0
881
- assert hasattr (response_0 .choices [0 ], "text" )
882
- text_split = response_0 .choices [0 ].text .split (" " )
883
- for text in text_split :
884
- output_0 .append (text )
858
+ assert hasattr (response_0 .choices [0 ], "completion_token_ids" )
859
+ assert isinstance (response_0 .choices [0 ].completion_token_ids , list )
860
+
861
+ from fastdeploy .input .ernie_tokenizer import ErnieBotTokenizer
862
+
863
+ tokenizer = ErnieBotTokenizer .from_pretrained (model_path , trust_remote_code = True )
864
+ output_tokens_0 = []
865
+ output_ids_0 = []
866
+ for ids in response_0 .choices [0 ].completion_token_ids :
867
+ output_tokens_0 .append (tokenizer .decode (ids ))
868
+ output_ids_0 .append (ids )
885
869
886
870
# add bad words
871
+ bad_tokens = output_tokens_0 [6 :10 ]
872
+ bad_token_ids = output_ids_0 [6 :10 ]
887
873
response_1 = openai_client .completions .create (
888
874
model = "default" ,
889
875
prompt = "Hello, how are you?" ,
890
876
temperature = 1 ,
891
877
top_p = 0.0 ,
892
- max_tokens = 10 ,
893
- extra_body = {"bad_words" : output_0 [ - 5 :] },
878
+ max_tokens = 20 ,
879
+ extra_body = {"bad_words" : bad_tokens , "return_token_ids" : True },
894
880
stream = False ,
895
881
)
896
- output_1 = []
897
882
assert hasattr (response_1 , "choices" )
898
883
assert len (response_1 .choices ) > 0
899
- assert hasattr (response_1 .choices [0 ], "text" )
900
- text_split = response_1 .choices [0 ].text .split (" " )
901
- for text in text_split :
902
- output_1 .append (text )
903
- assert output_0 not in output_1
904
-
905
-
906
- def test_streaming_completion_with_bad_words (openai_client , capsys ):
907
- """
908
- Test bad_words option in streaming completion functionality with the local service
909
- """
910
- response_0 = openai_client .completions .create (
911
- model = "default" ,
912
- prompt = "Hello, how are you?" ,
913
- temperature = 1 ,
914
- top_p = 0.0 ,
915
- max_tokens = 10 ,
916
- stream = True ,
917
- )
918
- output_0 = []
919
- for chunk in response_0 :
920
- assert hasattr (chunk , "choices" )
921
- assert len (chunk .choices ) > 0
922
- assert hasattr (chunk .choices [0 ], "text" )
923
- output_0 .append (chunk .choices [0 ].text )
924
-
925
- # add bad words
926
- response_1 = openai_client .completions .create (
927
- model = "default" ,
928
- prompt = "Hello, how are you?" ,
929
- temperature = 1 ,
930
- top_p = 0.0 ,
931
- max_tokens = 10 ,
932
- extra_body = {"bad_words" : output_0 [- 5 :]},
933
- stream = True ,
934
- )
935
- output_1 = []
936
- for chunk in response_1 :
937
- assert hasattr (chunk , "choices" )
938
- assert len (chunk .choices ) > 0
939
- assert hasattr (chunk .choices [0 ], "text" )
940
- output_1 .append (chunk .choices [0 ].text )
941
- assert output_0 not in output_1
884
+ assert hasattr (response_0 .choices [0 ], "completion_token_ids" )
885
+ assert isinstance (response_0 .choices [0 ].completion_token_ids , list )
886
+ assert not any (ids in response_1 .choices [0 ].completion_token_ids for ids in bad_token_ids )
0 commit comments