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

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