readme_examples.py 2.59 KB
Newer Older
1
2
3
4
5
"""
Usage:
python -m sglang.launch_server --model-path meta-llama/Llama-2-7b-chat-hf --port 30000
python readme_examples.py
"""
Lianmin Zheng's avatar
Lianmin Zheng committed
6
7
8
9
10
import sglang as sgl


@sgl.function
def tool_use(s, question):
11
12
13
    s += "To answer this question: " + question + ". "
    s += "I need to use a " + sgl.gen("tool", choices=["calculator", "search engine"]) + ". "

Lianmin Zheng's avatar
Lianmin Zheng committed
14
15
    if s["tool"] == "calculator":
        s += "The math expression is" + sgl.gen("expression")
16
17
    elif s["tool"] == "search engine":
        s += "The key word to search is" + sgl.gen("word")
Lianmin Zheng's avatar
Lianmin Zheng committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


@sgl.function
def tip_suggestion(s):
    s += (
        "Here are two tips for staying healthy: "
        "1. Balanced Diet. 2. Regular Exercise.\n\n"
    )

    forks = s.fork(2)
    for i, f in enumerate(forks):
        f += f"Now, expand tip {i+1} into a paragraph:\n"
        f += sgl.gen(f"detailed_tip", max_tokens=256, stop="\n\n")

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


37
38
39
40
41
42
43
44
45
46
@sgl.function
def regular_expression_gen(s):
    s += "Q: What is the IP address of the Google DNS servers?\n"
    s += "A: " + sgl.gen(
        "answer",
        temperature=0,
        regex=r"((25[0-5]|2[0-4]\d|[01]?\d\d?).){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)",
    )


Lianmin Zheng's avatar
Lianmin Zheng committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@sgl.function
def text_qa(s, question):
    s += "Q: " + question + "\n"
    s += "A:" + sgl.gen("answer", stop="\n")


def driver_tool_use():
    state = tool_use.run(question="What is the capital of the United States?")
    print(state.text())
    print("\n")


def driver_tip_suggestion():
    state = tip_suggestion.run()
    print(state.text())
    print("\n")


65
66
67
68
69
70
def driver_regex():
    state = regular_expression_gen.run()
    print(state.text())
    print("\n")


Lianmin Zheng's avatar
Lianmin Zheng committed
71
72
73
74
75
76
77
def driver_batching():
    states = text_qa.run_batch(
        [
            {"question": "What is the capital of the United Kingdom?"},
            {"question": "What is the capital of France?"},
            {"question": "What is the capital of Japan?"},
        ],
Lianmin Zheng's avatar
Lianmin Zheng committed
78
        progress_bar=True
Lianmin Zheng's avatar
Lianmin Zheng committed
79
80
81
82
83
84
85
86
87
88
    )

    for s in states:
        print(s.text())
    print("\n")


def driver_stream():
    state = text_qa.run(
        question="What is the capital of France?",
Lianmin Zheng's avatar
Lianmin Zheng committed
89
90
91
        temperature=0.1,
        stream=True
    )
Lianmin Zheng's avatar
Lianmin Zheng committed
92
93
94
95
96
97
98

    for out in state.text_iter():
        print(out, end="", flush=True)
    print("\n")


if __name__ == "__main__":
99
100
    #sgl.set_default_backend(sgl.OpenAI("gpt-3.5-turbo-instruct"))
    sgl.set_default_backend(sgl.RuntimeEndpoint("http://localhost:30000"))
Lianmin Zheng's avatar
Lianmin Zheng committed
101
102
103

    driver_tool_use()
    driver_tip_suggestion()
104
    driver_regex()
Lianmin Zheng's avatar
Lianmin Zheng committed
105
106
    driver_batching()
    driver_stream()