test_openai_spec.py 2.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
61
62
63
64
65
66
67
68
from sglang import OpenAI, function, gen, set_default_backend


@function()
def gen_character_default(s):
    s += "Construct a character within the following format:\n"
    s += "Name: Steve Jobs.\nBirthday: February 24, 1955.\nJob: Apple CEO.\nWelcome.\n"
    s += "\nPlease generate new Name, Birthday and Job.\n"
    s += "Name:" + gen("name", stop="\n") + "\nBirthday:" + gen("birthday", stop="\n")
    s += "\nJob:" + gen("job", stop="\n") + "\nWelcome.\n"


@function(api_num_spec_tokens=512)
def gen_character_spec(s):
    s += "Construct a character within the following format:\n"
    s += "Name: Steve Jobs.\nBirthday: February 24, 1955.\nJob: Apple CEO.\nWelcome.\n"
    s += "\nPlease generate new Name, Birthday and Job.\n"
    s += "Name:" + gen("name", stop="\n") + "\nBirthday:" + gen("birthday", stop="\n")
    s += "\nJob:" + gen("job", stop="\n") + "\nWelcome.\n"


@function(api_num_spec_tokens=512)
def gen_character_no_stop(s):
    s += "Construct a character within the following format:\n"
    s += "Name: Steve Jobs.\nBirthday: February 24, 1955.\nJob: Apple CEO.\nWelcome.\n"
    s += "\nPlease generate new Name, Birthday and Job.\n"
    s += "Name:" + gen("name") + "\nBirthday:" + gen("birthday")
    s += "\nJob:" + gen("job") + "\nWelcome.\n"


@function(api_num_spec_tokens=512)
def gen_character_multi_stop(s):
    s += "Construct a character within the following format:\n"
    s += (
        "Name: Steve Jobs.###Birthday: February 24, 1955.###Job: Apple CEO.\nWelcome.\n"
    )
    s += "\nPlease generate new Name, Birthday and Job.\n"
    s += "Name:" + gen("name", stop=["\n", "###"])
    s += "###Birthday:" + gen("birthday", stop=["\n", "###"])
    s += "###Job:" + gen("job", stop=["\n", "###"]) + "\nWelcome.\n"


set_default_backend(OpenAI("gpt-3.5-turbo-instruct"))

state = gen_character_default.run()
print(state.text())

print("=" * 60)

state = gen_character_no_stop.run()

print("name###", state["name"])
print("birthday###:", state["birthday"])
print("job###", state["job"])

print("=" * 60)

state = gen_character_multi_stop.run()
print(state.text())

print("=" * 60)

state = gen_character_spec.run()
print(state.text())

print("name###", state["name"])
print("birthday###", state["birthday"])
print("job###", state["job"])