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

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