test_tracing.py 3.97 KB
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
2
import unittest

Liangsheng Yin's avatar
Liangsheng Yin committed
3
import sglang as sgl
Ying Sheng's avatar
Ying Sheng committed
4
from sglang.lang.backend.base_backend import BaseBackend
Lianmin Zheng's avatar
Lianmin Zheng committed
5
from sglang.lang.chat_template import get_chat_template
6
from sglang.test.test_utils import CustomTestCase
Lianmin Zheng's avatar
Lianmin Zheng committed
7
8


9
class TestTracing(CustomTestCase):
Lianmin Zheng's avatar
Lianmin Zheng committed
10
11
12
13
14
15
16
17
18
19
    def test_few_shot_qa(self):
        @sgl.function
        def few_shot_qa(s, question):
            s += "The following are questions with answers.\n\n"
            s += "Q: What is the capital of France?\n"
            s += "A: Paris\n"
            s += "Q: " + question + "\n"
            s += "A:" + sgl.gen("answer", stop="\n")

        tracer = few_shot_qa.trace()
20
        # print(tracer.last_node.print_graph_dfs() + "\n")
Lianmin Zheng's avatar
Lianmin Zheng committed
21
22
23
24
25
26
27
28
29

    def test_select(self):
        @sgl.function
        def capital(s):
            s += "The capital of France is"
            s += sgl.select("capital", ["Paris. ", "London. "])
            s += "It is a city" + sgl.gen("description", stop=".")

        tracer = capital.trace()
30
        # print(tracer.last_node.print_graph_dfs() + "\n")
Lianmin Zheng's avatar
Lianmin Zheng committed
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
61
62
63
64
65
66
67
68
69

    def test_raise_warning(self):
        @sgl.function
        def wrong(s, question):
            s += f"I want to ask {question}"

        try:
            tracer = wrong.trace()
            raised = False
        except TypeError:
            raised = True

        assert raised

    def test_multi_function(self):
        @sgl.function
        def expand(s, tip):
            s += (
                "Please expand the following tip into a detailed paragraph:"
                + tip
                + "\n"
            )
            s += sgl.gen("detailed_tip")

        @sgl.function
        def tip_suggestion(s, topic):
            s += "Here are 2 tips for " + topic + ".\n"

            s += "1." + sgl.gen("tip_1", stop=["\n", ":", "."]) + "\n"
            s += "2." + sgl.gen("tip_2", stop=["\n", ":", "."]) + "\n"

            branch1 = expand(tip=s["tip_1"])
            branch2 = expand(tip=s["tip_2"])

            s += "Tip 1: " + branch1["detailed_tip"] + "\n"
            s += "Tip 2: " + branch2["detailed_tip"] + "\n"
            s += "In summary" + sgl.gen("summary")

        compiled = tip_suggestion.compile()
70
        # compiled.print_graph()
Lianmin Zheng's avatar
Lianmin Zheng committed
71
72
73

        sgl.set_default_backend(sgl.OpenAI("gpt-3.5-turbo-instruct"))
        state = compiled.run(topic="staying healthy")
74
        # print(state.text() + "\n")
Lianmin Zheng's avatar
Lianmin Zheng committed
75
76
77
78
79
80
81
82
83

        states = compiled.run_batch(
            [
                {"topic": "staying healthy"},
                {"topic": "staying happy"},
                {"topic": "earning money"},
            ],
            temperature=0,
        )
84
85
        # for s in states:
        #     print(s.text() + "\n")
Lianmin Zheng's avatar
Lianmin Zheng committed
86
87
88
89
90
91
92
93
94
95
96
97
98

    def test_role(self):
        @sgl.function
        def multi_turn_chat(s):
            s += sgl.user("Who are you?")
            s += sgl.assistant(sgl.gen("answer_1"))
            s += sgl.user("Who created you?")
            s += sgl.assistant(sgl.gen("answer_2"))

        backend = BaseBackend()
        backend.chat_template = get_chat_template("llama-2-chat")

        compiled = multi_turn_chat.compile(backend=backend)
99
        # compiled.print_graph()
Lianmin Zheng's avatar
Lianmin Zheng committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113

    def test_fork(self):
        @sgl.function
        def tip_suggestion(s):
            s += (
                "Here are three tips for staying healthy: "
                "1. Balanced Diet; "
                "2. Regular Exercise; "
                "3. Adequate Sleep\n"
            )

            forks = s.fork(3)
            for i in range(3):
                forks[i] += f"Now, expand tip {i+1} into a paragraph:\n"
114
                forks[i] += sgl.gen(f"detailed_tip")
Lianmin Zheng's avatar
Lianmin Zheng committed
115
116
117
118
119
120
121

            s += "Tip 1:" + forks[0]["detailed_tip"] + "\n"
            s += "Tip 2:" + forks[1]["detailed_tip"] + "\n"
            s += "Tip 3:" + forks[2]["detailed_tip"] + "\n"
            s += "In summary" + sgl.gen("summary")

        tracer = tip_suggestion.trace()
122
        # print(tracer.last_node.print_graph_dfs())
Lianmin Zheng's avatar
Lianmin Zheng committed
123
124

        a = tip_suggestion.run(backend=sgl.OpenAI("gpt-3.5-turbo-instruct"))
125
        # print(a.text())
Lianmin Zheng's avatar
Lianmin Zheng committed
126
127
128


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