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_cache_agent.py 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import os
  2. import sys
  3. import time
  4. import pytest
  5. import autogen
  6. from autogen.agentchat import AssistantAgent, UserProxyAgent
  7. from autogen.cache import Cache
  8. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST, here
  9. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  10. from conftest import skip_openai, skip_redis # noqa: E402
  11. try:
  12. from openai import OpenAI
  13. except ImportError:
  14. skip_openai_tests = True
  15. else:
  16. skip_openai_tests = False or skip_openai
  17. try:
  18. import redis
  19. except ImportError:
  20. skip_redis_tests = True
  21. else:
  22. skip_redis_tests = False or skip_redis
  23. @pytest.mark.skipif(skip_openai_tests, reason="openai not installed OR requested to skip")
  24. def test_legacy_disk_cache():
  25. random_cache_seed = int.from_bytes(os.urandom(2), "big")
  26. start_time = time.time()
  27. cold_cache_messages = run_conversation(
  28. cache_seed=random_cache_seed,
  29. )
  30. end_time = time.time()
  31. duration_with_cold_cache = end_time - start_time
  32. start_time = time.time()
  33. warm_cache_messages = run_conversation(
  34. cache_seed=random_cache_seed,
  35. )
  36. end_time = time.time()
  37. duration_with_warm_cache = end_time - start_time
  38. assert cold_cache_messages == warm_cache_messages
  39. assert duration_with_warm_cache < duration_with_cold_cache
  40. @pytest.mark.skipif(skip_openai_tests or skip_redis_tests, reason="redis not installed OR requested to skip")
  41. def test_redis_cache():
  42. random_cache_seed = int.from_bytes(os.urandom(2), "big")
  43. redis_url = os.getenv("REDIS_URL", "redis://localhost:6379/0")
  44. start_time = time.time()
  45. with Cache.redis(random_cache_seed, redis_url) as cache_client:
  46. cold_cache_messages = run_conversation(cache_seed=None, cache=cache_client)
  47. end_time = time.time()
  48. duration_with_cold_cache = end_time - start_time
  49. start_time = time.time()
  50. warm_cache_messages = run_conversation(cache_seed=None, cache=cache_client)
  51. end_time = time.time()
  52. duration_with_warm_cache = end_time - start_time
  53. assert cold_cache_messages == warm_cache_messages
  54. assert duration_with_warm_cache < duration_with_cold_cache
  55. random_cache_seed = int.from_bytes(os.urandom(2), "big")
  56. with Cache.redis(random_cache_seed, redis_url) as cache_client:
  57. cold_cache_messages = run_groupchat_conversation(cache=cache_client)
  58. end_time = time.time()
  59. duration_with_cold_cache = end_time - start_time
  60. start_time = time.time()
  61. warm_cache_messages = run_groupchat_conversation(cache=cache_client)
  62. end_time = time.time()
  63. duration_with_warm_cache = end_time - start_time
  64. assert cold_cache_messages == warm_cache_messages
  65. assert duration_with_warm_cache < duration_with_cold_cache
  66. @pytest.mark.skipif(skip_openai_tests, reason="openai not installed OR requested to skip")
  67. def test_disk_cache():
  68. random_cache_seed = int.from_bytes(os.urandom(2), "big")
  69. start_time = time.time()
  70. with Cache.disk(random_cache_seed) as cache_client:
  71. cold_cache_messages = run_conversation(cache_seed=None, cache=cache_client)
  72. end_time = time.time()
  73. duration_with_cold_cache = end_time - start_time
  74. start_time = time.time()
  75. warm_cache_messages = run_conversation(cache_seed=None, cache=cache_client)
  76. end_time = time.time()
  77. duration_with_warm_cache = end_time - start_time
  78. assert cold_cache_messages == warm_cache_messages
  79. assert duration_with_warm_cache < duration_with_cold_cache
  80. random_cache_seed = int.from_bytes(os.urandom(2), "big")
  81. with Cache.disk(random_cache_seed) as cache_client:
  82. cold_cache_messages = run_groupchat_conversation(cache=cache_client)
  83. end_time = time.time()
  84. duration_with_cold_cache = end_time - start_time
  85. start_time = time.time()
  86. warm_cache_messages = run_groupchat_conversation(cache=cache_client)
  87. end_time = time.time()
  88. duration_with_warm_cache = end_time - start_time
  89. assert cold_cache_messages == warm_cache_messages
  90. assert duration_with_warm_cache < duration_with_cold_cache
  91. def run_conversation(cache_seed, human_input_mode="NEVER", max_consecutive_auto_reply=5, cache=None):
  92. config_list = autogen.config_list_from_json(
  93. OAI_CONFIG_LIST,
  94. file_location=KEY_LOC,
  95. filter_dict={
  96. "tags": ["gpt-3.5-turbo", "gpt-3.5-turbo-16k"],
  97. },
  98. )
  99. llm_config = {
  100. "cache_seed": cache_seed,
  101. "config_list": config_list,
  102. "max_tokens": 1024,
  103. }
  104. assistant = AssistantAgent(
  105. "coding_agent",
  106. llm_config=llm_config,
  107. )
  108. user = UserProxyAgent(
  109. "user",
  110. human_input_mode=human_input_mode,
  111. is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
  112. max_consecutive_auto_reply=max_consecutive_auto_reply,
  113. code_execution_config={
  114. "work_dir": f"{here}/test_agent_scripts",
  115. "use_docker": "python:3",
  116. "timeout": 60,
  117. },
  118. llm_config=llm_config,
  119. system_message="""Is code provided but not enclosed in ``` blocks?
  120. If so, remind that code blocks need to be enclosed in ``` blocks.
  121. Reply TERMINATE to end the conversation if the task is finished. Don't say appreciation.
  122. If "Thank you" or "You\'re welcome" are said in the conversation, then say TERMINATE and that is your last message.""",
  123. )
  124. user.initiate_chat(assistant, message="TERMINATE", cache=cache)
  125. # should terminate without sending any message
  126. assert assistant.last_message()["content"] == assistant.last_message(user)["content"] == "TERMINATE"
  127. coding_task = "Print hello world to a file called hello.txt"
  128. # track how long this takes
  129. user.initiate_chat(assistant, message=coding_task, cache=cache)
  130. return user.chat_messages[assistant]
  131. def run_groupchat_conversation(cache, human_input_mode="NEVER", max_consecutive_auto_reply=5):
  132. KEY_LOC = "notebook"
  133. OAI_CONFIG_LIST = "OAI_CONFIG_LIST"
  134. here = os.path.abspath(os.path.dirname(__file__))
  135. config_list = autogen.config_list_from_json(
  136. OAI_CONFIG_LIST,
  137. file_location=KEY_LOC,
  138. filter_dict={
  139. "tags": ["gpt-3.5-turbo", "gpt-3.5-turbo-16k"],
  140. },
  141. )
  142. llm_config = {
  143. "cache_seed": None,
  144. "config_list": config_list,
  145. "max_tokens": 1024,
  146. }
  147. assistant = AssistantAgent(
  148. "coding_agent",
  149. llm_config=llm_config,
  150. )
  151. planner = AssistantAgent(
  152. "planner",
  153. llm_config=llm_config,
  154. )
  155. user = UserProxyAgent(
  156. "user",
  157. human_input_mode=human_input_mode,
  158. is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
  159. max_consecutive_auto_reply=max_consecutive_auto_reply,
  160. code_execution_config={
  161. "work_dir": f"{here}/test_agent_scripts",
  162. "use_docker": "python:3",
  163. "timeout": 60,
  164. },
  165. system_message="""Is code provided but not enclosed in ``` blocks?
  166. If so, remind that code blocks need to be enclosed in ``` blocks.
  167. Reply TERMINATE to end the conversation if the task is finished. Don't say appreciation.
  168. If "Thank you" or "You\'re welcome" are said in the conversation, then say TERMINATE and that is your last message.""",
  169. )
  170. group_chat = autogen.GroupChat(
  171. agents=[planner, assistant, user],
  172. messages=[],
  173. max_round=4,
  174. speaker_selection_method="round_robin",
  175. )
  176. manager = autogen.GroupChatManager(groupchat=group_chat, llm_config=llm_config)
  177. coding_task = "Print hello world to a file called hello.txt"
  178. user.initiate_chat(manager, message=coding_task, cache=cache)
  179. return user.chat_messages[list(user.chat_messages.keys())[-0]]