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_async_chats.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3 -m pytest
  2. import pytest
  3. import asyncio
  4. import sys
  5. import os
  6. import autogen
  7. from autogen import AssistantAgent, UserProxyAgent
  8. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  9. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  10. from conftest import skip_openai # noqa: E402
  11. @pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
  12. @pytest.mark.asyncio
  13. async def test_async_chats():
  14. config_list = autogen.config_list_from_json(
  15. OAI_CONFIG_LIST,
  16. file_location=KEY_LOC,
  17. )
  18. financial_tasks = [
  19. """What are the full names of NVDA and TESLA.""",
  20. """Get their stock price.""",
  21. """Analyze pros and cons. Keep it short.""",
  22. ]
  23. writing_tasks = ["""Develop a short but engaging blog post using any information provided."""]
  24. financial_assistant_1 = AssistantAgent(
  25. name="Financial_assistant_1",
  26. llm_config={"config_list": config_list},
  27. system_message="You are a knowledgeable AI Assistant. Reply TERMINATE when everything is done.",
  28. )
  29. financial_assistant_2 = AssistantAgent(
  30. name="Financial_assistant_2",
  31. llm_config={"config_list": config_list},
  32. )
  33. writer = AssistantAgent(
  34. name="Writer",
  35. llm_config={"config_list": config_list},
  36. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  37. system_message="""
  38. You are a professional writer, known for
  39. your insightful and engaging articles.
  40. You transform complex concepts into compelling narratives.
  41. Reply "TERMINATE" in the end when everything is done.
  42. """,
  43. )
  44. user = UserProxyAgent(
  45. name="User",
  46. human_input_mode="NEVER",
  47. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  48. code_execution_config={
  49. "last_n_messages": 1,
  50. "work_dir": "tasks",
  51. "use_docker": False,
  52. }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
  53. )
  54. def my_summary_method(recipient, sender, summary_args):
  55. return recipient.chat_messages[sender][1].get("content", "")
  56. chat_res = await user.a_initiate_chats(
  57. [
  58. {
  59. "chat_id": 1,
  60. "recipient": financial_assistant_1,
  61. "message": financial_tasks[0],
  62. "silent": False,
  63. "summary_method": my_summary_method,
  64. "max_turns": 1,
  65. },
  66. {
  67. "chat_id": 2,
  68. "prerequisites": [1],
  69. "recipient": financial_assistant_2,
  70. "message": financial_tasks[1],
  71. "silent": True,
  72. "summary_method": "reflection_with_llm",
  73. "max_turns": 3,
  74. },
  75. {
  76. "chat_id": 3,
  77. "prerequisites": [1, 2],
  78. "recipient": financial_assistant_1,
  79. "message": financial_tasks[2],
  80. "summary_method": "last_msg",
  81. "clear_history": False,
  82. "max_turns": 1,
  83. },
  84. {
  85. "chat_id": 4,
  86. "prerequisites": [1, 2, 3],
  87. "recipient": writer,
  88. "message": writing_tasks[0],
  89. "carryover": "I want to include a figure or a table of data in the blogpost.",
  90. "summary_method": "last_msg",
  91. "max_turns": 2,
  92. },
  93. ]
  94. )
  95. last_chat_id = 4
  96. chat_w_writer = chat_res[last_chat_id]
  97. print(chat_w_writer.chat_history, chat_w_writer.summary, chat_w_writer.cost)
  98. all_res = user.get_chat_results()
  99. writer_res = user.get_chat_results(last_chat_id)
  100. # print(blogpost.summary, insights_and_blogpost)
  101. print(writer_res.summary, writer_res.cost)
  102. print(all_res[1].human_input)
  103. print(all_res[1].summary)
  104. print(all_res[1].chat_history)
  105. print(all_res[2].summary)
  106. if __name__ == "__main__":
  107. asyncio.run(test_async_chats())