openai_speculative.py 1.67 KB
Newer Older
1
2
3
4
"""
Usage:
python3 openai_speculative.py
"""
5
6
7
from sglang import function, gen, set_default_backend, OpenAI


8
@function(api_num_spec_tokens=64)
9
10
11
12
13
14
15
16
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"


17
18
19
20
21
22
23
24
25
@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"


26
27
28
29
30
31
32
33
@function(api_num_spec_tokens=64)
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"


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

38
39
    for function in [gen_character_spec, gen_character_no_spec, gen_character_spec_no_few_shot]:
        backend.token_usage.reset()
40

41
        print(f"function: {function.func.__name__}")
42

43
        state = function.run()
44

45
46
47
48
49
        print("...name:", state["name"])
        print("...birthday:", state["birthday"])
        print("...job:", state["job"])
        print(backend.token_usage)
        print()