openai_speculative.py 1.7 KB
Newer Older
1
2
3
4
"""
Usage:
python3 openai_speculative.py
"""
zhyncs's avatar
zhyncs committed
5
6

from sglang import OpenAI, function, gen, set_default_backend
7
8


9
@function(num_api_spec_tokens=64)
10
11
12
13
14
15
16
17
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.\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") + "\n"


18
19
20
21
22
23
24
25
26
@function
def gen_character_no_spec(s):
    s += "Construct a character within the following format:\n"
    s += "Name: Steve Jobs.\nBirthday: February 24, 1955.\nJob: Apple CEO.\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") + "\n"


27
@function(num_api_spec_tokens=64)
28
29
30
31
32
33
34
def gen_character_spec_no_few_shot(s):
    # s += "Construct a character with name, birthday, and job:\n"
    s += "Construct a character:\n"
    s += "Name:" + gen("name", stop="\n") + "\nBirthday:" + gen("birthday", stop="\n")
    s += "\nJob:" + gen("job", stop="\n") + "\n"


35
36
37
if __name__ == "__main__":
    backend = OpenAI("gpt-3.5-turbo-instruct")
    set_default_backend(backend)
38

zhyncs's avatar
zhyncs committed
39
40
41
42
43
    for function in [
        gen_character_spec,
        gen_character_no_spec,
        gen_character_spec_no_few_shot,
    ]:
44
        backend.token_usage.reset()
45

46
        print(f"function: {function.func.__name__}")
47

48
        state = function.run()
49

50
51
52
53
        print("...name:", state["name"])
        print("...birthday:", state["birthday"])
        print("...job:", state["job"])
        print(backend.token_usage)
zhyncs's avatar
zhyncs committed
54
        print()