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 8.1 kB

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