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_nested.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3 -m pytest
  2. import os
  3. import sys
  4. import pytest
  5. import autogen
  6. sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
  7. sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))
  8. from conftest import reason, skip_openai # noqa: E402
  9. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402
  10. @pytest.mark.skipif(skip_openai, reason=reason)
  11. def test_nested():
  12. config_list = autogen.config_list_from_json(env_or_file=OAI_CONFIG_LIST, file_location=KEY_LOC)
  13. llm_config = {"config_list": config_list}
  14. tasks = [
  15. """What's the date today?""",
  16. """Make a pleasant joke about it.""",
  17. ]
  18. inner_assistant = autogen.AssistantAgent(
  19. "Inner-assistant",
  20. llm_config=llm_config,
  21. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  22. )
  23. inner_code_interpreter = autogen.UserProxyAgent(
  24. "Inner-code-interpreter",
  25. human_input_mode="NEVER",
  26. code_execution_config={
  27. "work_dir": "coding",
  28. "use_docker": False,
  29. },
  30. default_auto_reply="",
  31. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  32. )
  33. groupchat = autogen.GroupChat(
  34. agents=[inner_assistant, inner_code_interpreter],
  35. messages=[],
  36. speaker_selection_method="round_robin", # With two agents, this is equivalent to a 1:1 conversation.
  37. allow_repeat_speaker=False,
  38. max_round=8,
  39. )
  40. manager = autogen.GroupChatManager(
  41. groupchat=groupchat,
  42. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  43. llm_config=llm_config,
  44. code_execution_config={
  45. "work_dir": "coding",
  46. "use_docker": False,
  47. },
  48. )
  49. assistant = autogen.AssistantAgent(
  50. name="Assistant",
  51. llm_config={"config_list": config_list},
  52. # is_termination_msg=lambda x: x.get("content", "") == "",
  53. )
  54. assistant_2 = autogen.AssistantAgent(
  55. name="Assistant",
  56. llm_config={"config_list": config_list},
  57. # is_termination_msg=lambda x: x.get("content", "") == "",
  58. )
  59. user = autogen.UserProxyAgent(
  60. name="User",
  61. human_input_mode="NEVER",
  62. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  63. code_execution_config={
  64. "last_n_messages": 1,
  65. "work_dir": "tasks",
  66. "use_docker": False,
  67. }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
  68. )
  69. user_2 = autogen.UserProxyAgent(
  70. name="User",
  71. human_input_mode="NEVER",
  72. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  73. code_execution_config={
  74. "last_n_messages": 1,
  75. "work_dir": "tasks",
  76. "use_docker": False,
  77. }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
  78. )
  79. writer = autogen.AssistantAgent(
  80. name="Writer",
  81. llm_config={"config_list": config_list},
  82. system_message="""
  83. You are a professional writer, known for
  84. your insightful and engaging articles.
  85. You transform complex concepts into compelling narratives.
  86. Reply "TERMINATE" in the end when everything is done.
  87. """,
  88. )
  89. autogen.AssistantAgent(
  90. name="Reviewer",
  91. llm_config={"config_list": config_list},
  92. system_message="""
  93. You are a compliance reviewer, known for your thoroughness and commitment to standards.
  94. Your task is to scrutinize content for any harmful elements or regulatory violations, ensuring
  95. all materials align with required guidelines.
  96. You must review carefully, identify potential issues, and maintain the integrity of the organization.
  97. Your role demands fairness, a deep understanding of regulations, and a focus on protecting against
  98. harm while upholding a culture of responsibility.
  99. You also help make revisions to ensure the content is accurate, clear, and compliant.
  100. Reply "TERMINATE" in the end when everything is done.
  101. """,
  102. )
  103. def writing_message(recipient, messages, sender, config):
  104. return f"Make a one-sentence comment. \n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}"
  105. nested_chat_queue = [
  106. {"sender": user_2, "recipient": manager, "summary_method": "reflection_with_llm"},
  107. {"recipient": writer, "message": writing_message, "summary_method": "last_msg", "max_turns": 1},
  108. ]
  109. assistant.register_nested_chats(
  110. nested_chat_queue,
  111. trigger=user,
  112. )
  113. user.initiate_chats(
  114. [{"recipient": assistant, "message": tasks[0], "max_turns": 1}, {"recipient": assistant_2, "message": tasks[1]}]
  115. )
  116. if __name__ == "__main__":
  117. test_nested()