You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_token_count.py 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3 -m pytest
  2. from autogen.token_count_utils import (
  3. count_token,
  4. num_tokens_from_functions,
  5. token_left,
  6. percentile_used,
  7. get_max_token_limit,
  8. )
  9. import pytest
  10. func1 = {
  11. "name": "sh",
  12. "description": "run a shell script and return the execution result.",
  13. "parameters": {
  14. "type": "object",
  15. "properties": {
  16. "script": {
  17. "type": "string",
  18. "description": "Valid shell script to execute.",
  19. }
  20. },
  21. "required": ["script"],
  22. },
  23. }
  24. func2 = {
  25. "name": "query_wolfram",
  26. "description": "Return the API query result from the Wolfram Alpha. the return is a tuple of (result, is_success).",
  27. "parameters": {
  28. "type": "object",
  29. "properties": {},
  30. },
  31. }
  32. func3 = {
  33. "name": "python",
  34. "description": "run cell in ipython and return the execution result.",
  35. "parameters": {
  36. "type": "object",
  37. "properties": {
  38. "cell": {
  39. "type": "string",
  40. "description": "Valid Python cell to execute.",
  41. }
  42. },
  43. "required": ["cell"],
  44. },
  45. }
  46. @pytest.mark.parametrize(
  47. "input_functions, expected_count", [([func1], 44), ([func2], 46), ([func3], 45), ([func1, func2], 78)]
  48. )
  49. def test_num_tokens_from_functions(input_functions, expected_count):
  50. assert num_tokens_from_functions(input_functions) == expected_count
  51. def test_count_token():
  52. messages = [
  53. {
  54. "role": "system",
  55. "content": "you are a helpful assistant. af3758 *3 33(3)",
  56. },
  57. {
  58. "role": "user",
  59. "content": "hello asdfjj qeweee",
  60. },
  61. ]
  62. assert count_token(messages) == 34
  63. assert percentile_used(messages) == 34 / 4096
  64. assert token_left(messages) == 4096 - 34
  65. text = "I'm sorry, but I'm not able to"
  66. assert count_token(text) == 10
  67. assert token_left(text) == 4096 - 10
  68. assert percentile_used(text) == 10 / 4096
  69. def test_model_aliases():
  70. assert get_max_token_limit("gpt35-turbo") == get_max_token_limit("gpt-3.5-turbo")
  71. assert get_max_token_limit("gpt-35-turbo") == get_max_token_limit("gpt-3.5-turbo")
  72. assert get_max_token_limit("gpt4") == get_max_token_limit("gpt-4")
  73. assert get_max_token_limit("gpt4-32k") == get_max_token_limit("gpt-4-32k")
  74. if __name__ == "__main__":
  75. # test_num_tokens_from_functions()
  76. # test_count_token()
  77. test_model_aliases()