"vscode:/vscode.git/clone" did not exist on "de2dd73831bdd22b5f2946909e6827ece4712ba8"
test_bind_cache.py 1.43 KB
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
2
3
4
5
6
7
8
import unittest

import sglang as sgl


class TestBind(unittest.TestCase):
    backend = None

9
10
11
12
    @classmethod
    def setUpClass(cls):
        cls.backend = sgl.Runtime(model_path="meta-llama/Meta-Llama-3-8B-Instruct")
        sgl.set_default_backend(cls.backend)
Lianmin Zheng's avatar
Lianmin Zheng committed
13

14
15
16
    @classmethod
    def tearDownClass(cls):
        cls.backend.shutdown()
Lianmin Zheng's avatar
Lianmin Zheng committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

    def test_bind(self):
        @sgl.function
        def few_shot_qa(s, prompt, question):
            s += prompt
            s += "Q: What is the capital of France?\n"
            s += "A: Paris\n"
            s += "Q: " + question + "\n"
            s += "A:" + sgl.gen("answer", stop="\n")

        few_shot_qa_2 = few_shot_qa.bind(
            prompt="The following are questions with answers.\n\n"
        )

        tracer = few_shot_qa_2.trace()
        print(tracer.last_node.print_graph_dfs() + "\n")

34
    def test_cache(self):
Lianmin Zheng's avatar
Lianmin Zheng committed
35
36
37
38
39
40
41
42
43
44
45
        @sgl.function
        def few_shot_qa(s, prompt, question):
            s += prompt
            s += "Q: What is the capital of France?\n"
            s += "A: Paris\n"
            s += "Q: " + question + "\n"
            s += "A:" + sgl.gen("answer", stop="\n")

        few_shot_qa_2 = few_shot_qa.bind(
            prompt="Answer the following questions as if you were a 5-year-old kid.\n\n"
        )
46
        few_shot_qa_2.cache(self.backend)
Lianmin Zheng's avatar
Lianmin Zheng committed
47
48
49
50
51
52


if __name__ == "__main__":
    unittest.main(warnings="ignore")

    # t = TestBind()
53
    # t.setUpClass()
54
    # t.test_cache()