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_agent_usage.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3 -m pytest
  2. import io
  3. import os
  4. import sys
  5. from contextlib import redirect_stdout
  6. import pytest
  7. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  8. import autogen
  9. from autogen import AssistantAgent, UserProxyAgent, gather_usage_summary
  10. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  11. from conftest import reason, skip_openai # noqa: E402
  12. @pytest.mark.skipif(skip_openai, reason=reason)
  13. def test_gathering():
  14. config_list = autogen.config_list_from_json(
  15. OAI_CONFIG_LIST,
  16. file_location=KEY_LOC,
  17. )
  18. assistant1 = AssistantAgent(
  19. "assistant",
  20. system_message="You are a helpful assistant.",
  21. llm_config={
  22. "config_list": config_list,
  23. "model": "gpt-3.5-turbo-0613",
  24. },
  25. )
  26. assistant2 = AssistantAgent(
  27. "assistant",
  28. system_message="You are a helpful assistant.",
  29. llm_config={
  30. "config_list": config_list,
  31. "model": "gpt-3.5-turbo-0613",
  32. },
  33. )
  34. assistant3 = AssistantAgent(
  35. "assistant",
  36. system_message="You are a helpful assistant.",
  37. llm_config={
  38. "config_list": config_list,
  39. "model": "gpt-3.5-turbo-0613",
  40. },
  41. )
  42. assistant1.client.total_usage_summary = {
  43. "total_cost": 0.1,
  44. "gpt-35-turbo": {"cost": 0.1, "prompt_tokens": 100, "completion_tokens": 200, "total_tokens": 300},
  45. }
  46. assistant2.client.total_usage_summary = {
  47. "total_cost": 0.2,
  48. "gpt-35-turbo": {"cost": 0.2, "prompt_tokens": 100, "completion_tokens": 200, "total_tokens": 300},
  49. }
  50. assistant3.client.total_usage_summary = {
  51. "total_cost": 0.3,
  52. "gpt-4": {"cost": 0.3, "prompt_tokens": 100, "completion_tokens": 200, "total_tokens": 300},
  53. }
  54. total_usage = gather_usage_summary([assistant1, assistant2, assistant3])
  55. assert round(total_usage["usage_including_cached_inference"]["total_cost"], 8) == 0.6
  56. assert round(total_usage["usage_including_cached_inference"]["gpt-35-turbo"]["cost"], 8) == 0.3
  57. assert round(total_usage["usage_including_cached_inference"]["gpt-4"]["cost"], 8) == 0.3
  58. # test when agent doesn't have client
  59. user_proxy = UserProxyAgent(
  60. name="ai_user",
  61. human_input_mode="NEVER",
  62. max_consecutive_auto_reply=2,
  63. code_execution_config=False,
  64. default_auto_reply="That's all. Thank you.",
  65. )
  66. total_usage = gather_usage_summary([user_proxy])
  67. total_usage_summary = total_usage["usage_including_cached_inference"]
  68. print("Total usage summary:", total_usage_summary)
  69. @pytest.mark.skipif(skip_openai, reason=reason)
  70. def test_agent_usage():
  71. config_list = autogen.config_list_from_json(
  72. OAI_CONFIG_LIST,
  73. file_location=KEY_LOC,
  74. filter_dict={"tags": ["gpt-3.5-turbo"]},
  75. )
  76. assistant = AssistantAgent(
  77. "assistant",
  78. system_message="You are a helpful assistant.",
  79. llm_config={
  80. "cache_seed": None,
  81. "config_list": config_list,
  82. },
  83. )
  84. ai_user_proxy = UserProxyAgent(
  85. name="ai_user",
  86. human_input_mode="NEVER",
  87. max_consecutive_auto_reply=1,
  88. code_execution_config=False,
  89. llm_config={
  90. "config_list": config_list,
  91. },
  92. # In the system message the "user" always refers to the other agent.
  93. system_message="You ask a user for help. You check the answer from the user and provide feedback.",
  94. )
  95. math_problem = "$x^3=125$. What is x?"
  96. res = ai_user_proxy.initiate_chat(
  97. assistant,
  98. message=math_problem,
  99. summary_method="reflection_with_llm",
  100. )
  101. print("Result summary:", res.summary)
  102. # test print
  103. captured_output = io.StringIO()
  104. with redirect_stdout(captured_output):
  105. ai_user_proxy.print_usage_summary()
  106. output = captured_output.getvalue()
  107. assert "Usage summary excluding cached usage:" in output
  108. captured_output = io.StringIO()
  109. with redirect_stdout(captured_output):
  110. assistant.print_usage_summary()
  111. output = captured_output.getvalue()
  112. assert "All completions are non-cached:" in output
  113. # test get
  114. print("Actual usage summary (excluding completion from cache):", assistant.get_actual_usage())
  115. print("Total usage summary (including completion from cache):", assistant.get_total_usage())
  116. print("Actual usage summary (excluding completion from cache):", ai_user_proxy.get_actual_usage())
  117. print("Total usage summary (including completion from cache):", ai_user_proxy.get_total_usage())
  118. if __name__ == "__main__":
  119. # test_gathering()
  120. test_agent_usage()