Skip to content

Commit 71018fb

Browse files
xjkmfaxujing43
andauthored
【CI case】include total_tokens in the last packet of completion interface stream output (#3279)
* Add ci case for min token and max token * 【CI case】include total_tokens in the last packet of completion interface stream output --------- Co-authored-by: xujing43 <[email protected]>
1 parent 0b77d39 commit 71018fb

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/ce/server/test_completions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @author xujing43
4+
# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python
5+
6+
"""
7+
Checking for /v1/completions parameters
8+
"""
9+
10+
import json
11+
12+
from core import (
13+
TEMPLATE,
14+
URL,
15+
build_request_payload,
16+
send_request,
17+
)
18+
19+
URL = URL.replace("/v1/chat/completions", "/v1/completions")
20+
21+
def test_completion_total_tokens():
22+
data = {
23+
"prompt": "你是谁",
24+
"stream": True,
25+
"stream_options": {"include_usage": True, "continuous_usage_stats": True},
26+
}
27+
28+
payload = build_request_payload(TEMPLATE, data)
29+
resp = send_request(URL, payload, stream=True)
30+
last_data = None
31+
for line in resp.iter_lines(decode_unicode=True):
32+
if line.strip() == "data: [DONE]":
33+
break
34+
if line.strip() == "" or not line.startswith("data: "):
35+
continue
36+
line = line[len("data: "):]
37+
last_data = json.loads(line)
38+
usage = last_data["usage"]
39+
total_tokens = usage["completion_tokens"] + usage["prompt_tokens"]
40+
assert "total_tokens" in usage, "total_tokens 不存在"
41+
assert usage["total_tokens"]== total_tokens, "total_tokens计数不正确"
42+

0 commit comments

Comments
 (0)