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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. )
  75. assistant = AssistantAgent(
  76. "assistant",
  77. system_message="You are a helpful assistant.",
  78. llm_config={
  79. "timeout": 600,
  80. "cache_seed": None,
  81. "config_list": config_list,
  82. "model": "gpt-3.5-turbo-0613",
  83. },
  84. )
  85. ai_user_proxy = UserProxyAgent(
  86. name="ai_user",
  87. human_input_mode="NEVER",
  88. max_consecutive_auto_reply=1,
  89. code_execution_config=False,
  90. llm_config={
  91. "config_list": config_list,
  92. "model": "gpt-3.5-turbo-0613",
  93. },
  94. # In the system message the "user" always refers to the other agent.
  95. system_message="You ask a user for help. You check the answer from the user and provide feedback.",
  96. )
  97. math_problem = "$x^3=125$. What is x?"
  98. res = ai_user_proxy.initiate_chat(
  99. assistant,
  100. message=math_problem,
  101. summary_method="reflection_with_llm",
  102. )
  103. print("Result summary:", res.summary)
  104. # test print
  105. captured_output = io.StringIO()
  106. with redirect_stdout(captured_output):
  107. ai_user_proxy.print_usage_summary()
  108. output = captured_output.getvalue()
  109. assert "Usage summary excluding cached usage:" in output
  110. captured_output = io.StringIO()
  111. with redirect_stdout(captured_output):
  112. assistant.print_usage_summary()
  113. output = captured_output.getvalue()
  114. assert "All completions are non-cached:" in output
  115. # test get
  116. print("Actual usage summary (excluding completion from cache):", assistant.get_actual_usage())
  117. print("Total usage summary (including completion from cache):", assistant.get_total_usage())
  118. print("Actual usage summary (excluding completion from cache):", ai_user_proxy.get_actual_usage())
  119. print("Total usage summary (including completion from cache):", ai_user_proxy.get_total_usage())
  120. if __name__ == "__main__":
  121. test_gathering()
  122. test_agent_usage()