forked from microsoft/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_token_count.py
executable file
·89 lines (77 loc) · 2.43 KB
/
test_token_count.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3 -m pytest
import pytest
from autogen.token_count_utils import (
count_token,
get_max_token_limit,
num_tokens_from_functions,
percentile_used,
token_left,
)
func1 = {
"name": "sh",
"description": "run a shell script and return the execution result.",
"parameters": {
"type": "object",
"properties": {
"script": {
"type": "string",
"description": "Valid shell script to execute.",
}
},
"required": ["script"],
},
}
func2 = {
"name": "query_wolfram",
"description": "Return the API query result from the Wolfram Alpha. the return is a tuple of (result, is_success).",
"parameters": {
"type": "object",
"properties": {},
},
}
func3 = {
"name": "python",
"description": "run cell in ipython and return the execution result.",
"parameters": {
"type": "object",
"properties": {
"cell": {
"type": "string",
"description": "Valid Python cell to execute.",
}
},
"required": ["cell"],
},
}
@pytest.mark.parametrize(
"input_functions, expected_count", [([func1], 44), ([func2], 46), ([func3], 45), ([func1, func2], 78)]
)
def test_num_tokens_from_functions(input_functions, expected_count):
assert num_tokens_from_functions(input_functions) == expected_count
def test_count_token():
messages = [
{
"role": "system",
"content": "you are a helpful assistant. af3758 *3 33(3)",
},
{
"role": "user",
"content": "hello asdfjj qeweee",
},
]
assert count_token(messages) == 34
assert percentile_used(messages) == 34 / 4096
assert token_left(messages) == 4096 - 34
text = "I'm sorry, but I'm not able to"
assert count_token(text) == 10
assert token_left(text) == 4096 - 10
assert percentile_used(text) == 10 / 4096
def test_model_aliases():
assert get_max_token_limit("gpt35-turbo") == get_max_token_limit("gpt-3.5-turbo")
assert get_max_token_limit("gpt-35-turbo") == get_max_token_limit("gpt-3.5-turbo")
assert get_max_token_limit("gpt4") == get_max_token_limit("gpt-4")
assert get_max_token_limit("gpt4-32k") == get_max_token_limit("gpt-4-32k")
if __name__ == "__main__":
# test_num_tokens_from_functions()
# test_count_token()
test_model_aliases()