test_srt_endpoint.py 1.73 KB
Newer Older
1
2
3
4
5
6
import json
import unittest

import requests

from sglang.srt.utils import kill_child_process
Ying Sheng's avatar
Ying Sheng committed
7
from sglang.test.test_utils import DEFAULT_MODEL_NAME_FOR_TEST, popen_launch_server
8
9
10
11
12
13


class TestSRTEndpoint(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
Ying Sheng's avatar
Ying Sheng committed
14
        cls.model = DEFAULT_MODEL_NAME_FOR_TEST
15
        cls.base_url = "http://127.0.0.1:8157"
16
        cls.process = popen_launch_server(cls.model, cls.base_url, timeout=300)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

    @classmethod
    def tearDownClass(cls):
        kill_child_process(cls.process.pid)

    def run_decode(
        self, return_logprob=False, top_logprobs_num=0, return_text=False, n=1
    ):
        response = requests.post(
            self.base_url + "/generate",
            json={
                "text": "The capital of France is",
                "sampling_params": {
                    "temperature": 0 if n == 1 else 0.5,
                    "max_new_tokens": 32,
                    "n": n,
                },
                "stream": False,
                "return_logprob": return_logprob,
                "top_logprobs_num": top_logprobs_num,
                "return_text_in_logprobs": return_text,
                "logprob_start_len": 0,
            },
        )
        print(json.dumps(response.json()))
        print("=" * 100)

    def test_simple_decode(self):
        self.run_decode()

    def test_parallel_sample(self):
        self.run_decode(n=3)

    def test_logprob(self):
        for top_logprobs_num in [0, 3]:
            for return_text in [True, False]:
                self.run_decode(
                    return_logprob=True,
                    top_logprobs_num=top_logprobs_num,
                    return_text=return_text,
                )


if __name__ == "__main__":
Lianmin Zheng's avatar
Lianmin Zheng committed
61
    unittest.main()