Skip to content

Commit 21920d1

Browse files
committed
fix bad_words test
1 parent 1ace375 commit 21920d1

File tree

1 file changed

+51
-106
lines changed

1 file changed

+51
-106
lines changed

test/ci_use/EB_Lite/test_EB_Lite_serving.py

Lines changed: 51 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -784,158 +784,103 @@ def test_non_streaming_chat_with_bad_words(openai_client, capsys):
784784
"""
785785
Test bad_words option in non-streaming chat functionality with the local service
786786
"""
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"
787792
response_0 = openai_client.chat.completions.create(
788793
model="default",
789794
messages=[{"role": "user", "content": "Hello, how are you?"}],
790795
temperature=1,
791796
top_p=0.0,
792-
max_tokens=10,
797+
max_tokens=20,
793798
stream=False,
799+
extra_body={"return_token_ids": True},
794800
)
795-
output_0 = []
801+
796802
assert hasattr(response_0, "choices")
797803
assert len(response_0.choices) > 0
798804
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)
800807

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)
804816

805817
# add bad words
818+
bad_tokens = output_tokens_0[6:10]
819+
bad_token_ids = output_ids_0[6:10]
806820
response_1 = openai_client.chat.completions.create(
807821
model="default",
808822
messages=[{"role": "user", "content": "Hello, how are you?"}],
809823
temperature=1,
810824
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},
813827
stream=False,
814828
)
815-
output_1 = []
816829
assert hasattr(response_1, "choices")
817830
assert len(response_1.choices) > 0
818831
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)
864835

865836

866837
def test_non_streaming_completion_with_bad_words(openai_client, capsys):
867838
"""
868839
Test bad_words option in non-streaming completion functionality with the local service
869840
"""
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+
870847
response_0 = openai_client.completions.create(
871848
model="default",
872849
prompt="Hello, how are you?",
873850
temperature=1,
874851
top_p=0.0,
875-
max_tokens=10,
852+
max_tokens=20,
876853
stream=False,
854+
extra_body={"return_token_ids": True},
877855
)
878-
output_0 = []
879856
assert hasattr(response_0, "choices")
880857
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)
885869

886870
# add bad words
871+
bad_tokens = output_tokens_0[6:10]
872+
bad_token_ids = output_ids_0[6:10]
887873
response_1 = openai_client.completions.create(
888874
model="default",
889875
prompt="Hello, how are you?",
890876
temperature=1,
891877
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},
894880
stream=False,
895881
)
896-
output_1 = []
897882
assert hasattr(response_1, "choices")
898883
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

Comments
 (0)