test_httpserver_decode_stream.py 2.25 KB
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
"""
2
Usage:
Lianmin Zheng's avatar
Lianmin Zheng committed
3
python3 -m sglang.launch_server --model-path TinyLlama/TinyLlama-1.1B-Chat-v0.4 --port 30000
4
python3 test_httpserver_decode_stream.py
Lianmin Zheng's avatar
Lianmin Zheng committed
5
6
7
8
9
10
11
12
13
14

Output:
The capital of France is Paris.\nThe capital of the United States is Washington, D.C.\nThe capital of Canada is Ottawa.\nThe capital of Japan is Tokyo
"""

import argparse
import json

import requests

15

Liangsheng Yin's avatar
Liangsheng Yin committed
16
def test_decode_stream(url, return_logprob, top_logprobs_num):
Lianmin Zheng's avatar
Lianmin Zheng committed
17
18
19
20
21
22
    response = requests.post(
        url + "/generate",
        json={
            "text": "The capital of France is",
            "sampling_params": {
                "temperature": 0,
Cody Yu's avatar
Cody Yu committed
23
                "max_new_tokens": 128,
Lianmin Zheng's avatar
Lianmin Zheng committed
24
25
            },
            "stream": True,
Cody Yu's avatar
Cody Yu committed
26
            "return_logprob": return_logprob,
Liangsheng Yin's avatar
Liangsheng Yin committed
27
28
            "top_logprobs_num": top_logprobs_num,
            "return_text_in_logprobs": True,
29
            "logprob_start_len": 0,
Lianmin Zheng's avatar
Lianmin Zheng committed
30
31
32
33
34
        },
        stream=True,
    )

    prev = 0
35
36
37
38
39
40
    for chunk in response.iter_lines(decode_unicode=False):
        chunk = chunk.decode("utf-8")
        if chunk and chunk.startswith("data:"):
            if chunk == "data: [DONE]":
                break
            data = json.loads(chunk[5:].strip("\n"))
Cody Yu's avatar
Cody Yu committed
41
42

            if return_logprob:
43
44
                assert data["meta_info"]["input_token_logprobs"] is not None
                assert data["meta_info"]["output_token_logprobs"] is not None
Cody Yu's avatar
Cody Yu committed
45
                assert data["meta_info"]["normalized_prompt_logprob"] is not None
Liangsheng Yin's avatar
Liangsheng Yin committed
46
                for logprob, token_id, token_text in data["meta_info"][
47
                    "output_token_logprobs"
Liangsheng Yin's avatar
Liangsheng Yin committed
48
49
                ][prev:]:
                    print(f"{token_text:12s}\t{logprob}\t{token_id}", flush=True)
50
                prev = len(data["meta_info"]["output_token_logprobs"])
Cody Yu's avatar
Cody Yu committed
51
52
53
54
            else:
                output = data["text"].strip()
                print(output[prev:], end="", flush=True)
                prev = len(output)
Liangsheng Yin's avatar
Liangsheng Yin committed
55
56

    print("=" * 100)
Cody Yu's avatar
Cody Yu committed
57

58

Cody Yu's avatar
Cody Yu committed
59
60
61
62
63
64
65
66
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default="http://127.0.0.1")
    parser.add_argument("--port", type=int, default=30000)
    args = parser.parse_args()

    url = f"{args.host}:{args.port}"

Liangsheng Yin's avatar
Liangsheng Yin committed
67
68
69
    test_decode_stream(url, False, 0)
    test_decode_stream(url, True, 0)
    test_decode_stream(url, True, 3)