33from core import TEMPLATE , URL , build_request_payload , send_request
44
55
6+ def _strip_logits_stats (obj ):
7+ """Recursively remove 'logits_stats' keys from logprobs response."""
8+ if isinstance (obj , dict ):
9+ obj .pop ("logits_stats" , None )
10+ for v in obj .values ():
11+ _strip_logits_stats (v )
12+ elif isinstance (obj , list ):
13+ for item in obj :
14+ _strip_logits_stats (item )
15+
16+
617def test_unstream_with_logprobs ():
718 """
819 测试非流式响应开启 logprobs 后,返回的 token 概率信息是否正确。
@@ -21,6 +32,7 @@ def test_unstream_with_logprobs():
2132 response = send_request (URL , payload )
2233 print (json .dumps (response .json (), indent = 2 , ensure_ascii = False ))
2334 resp_json = response .json ()
35+ _strip_logits_stats (resp_json )
2436
2537 # 校验返回内容与概率信息
2638 assert resp_json ["choices" ][0 ]["message" ]["content" ] == "牛顿的"
@@ -99,6 +111,7 @@ def test_stream_with_logprobs():
99111 print (json .dumps (result_chunk , indent = 2 , ensure_ascii = False ))
100112 break
101113
114+ _strip_logits_stats (result_chunk )
102115 # 校验概率字段
103116 assert result_chunk ["choices" ][0 ]["delta" ]["content" ] == "牛顿"
104117 assert result_chunk ["choices" ][0 ]["logprobs" ]["content" ][0 ]["token" ] == "牛顿"
@@ -184,6 +197,7 @@ def test_stream_with_temp_scaled_logprobs():
184197 print (json .dumps (result_chunk , indent = 2 , ensure_ascii = False ))
185198 break
186199
200+ _strip_logits_stats (result_chunk )
187201 # 校验概率字段
188202 assert result_chunk ["choices" ][0 ]["delta" ]["content" ] == "牛顿"
189203 assert result_chunk ["choices" ][0 ]["logprobs" ]["content" ][0 ]["token" ] == "牛顿"
@@ -229,6 +243,7 @@ def test_stream_with_top_p_normalized_logprobs():
229243 print (json .dumps (result_chunk , indent = 2 , ensure_ascii = False ))
230244 break
231245
246+ _strip_logits_stats (result_chunk )
232247 # 校验概率字段
233248 assert result_chunk ["choices" ][0 ]["delta" ]["content" ] == "牛顿"
234249 assert result_chunk ["choices" ][0 ]["logprobs" ]["content" ][0 ]["token" ] == "牛顿"
0 commit comments