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_graph_utils.py 6.8 kB

Graph group chat (#857) * Move contrib-openai.yml * Moved groupgroupchat * From #753 * Removed local test references * Added ignore=test/agentchat/contrib * Trying to pass contrib-openai tests * More specific in unit testing. * Update .github/workflows/contrib-tests.yml Co-authored-by: Li Jiang <lijiang1@microsoft.com> * Remove coverage as it is included in test dependencies * Improved docstring with overview of GraphGroupChat * Iterate on feedback * Precommit pass * user just use pip install pyautogen[graphs] * Pass precommit * Pas precommit * Graph utils an test completed * Added inversion tests * Added inversion util * allow_repeat_speaker can be a list of Agents * Remove unnessary imports * Expect ValueError with 1 and 0 agents * Check that main passes all tests * Check main * Pytest all in main * All done * pre-commit changes * noqa E402 * precommit pass * Removed bin * Removed old unit test * Test test_graph_utils * minor cleanup * restore tests * Correct documentation * Special case of only one agent remaining. * Improved pytest * precommit pass * Delete OAI_CONFIG_LIST_sample copy * Returns a filtered list for auto to work * Rename var speaker_order_dict * To write test cases * Added check for a list of Agents to repeat * precommit pass * Update documentation * Extract names in allow_repeat_speaker * Post review changes * hange "pull_request_target" into "pull_request" temporarily. * 3 return values from main * pre-commit changes * PC edits * docstr changes * PC edits * Rest of changes from main * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Remove unnecessary script files from tracking * Non empty scripts files from main * Revert changes in script files to match main branch * Removed link from website as notebook is removed. * test/test_graph_utils.py is tested as part of L52 of build.yml * GroupChat ValueError check * docstr update * More clarification in docstr * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * 1.add commit to line138 in groupchat.py;2.fix bug if random choice [];3.return selected_agent if len(graph_eligible_agents) is 1;4.replace all speaker_order to speaker_transitions;5.format * fix graph_modelling notebook in the last cell * fix failure in test_groupchat.py * fix agent out of group to initiate a chat like SocietyOfMind * add a warning rule in graph_utils to check duplicates in any lists * refactor allowed_or_disallowed_speaker_transitions to Dict[Agent, List[Agent]] and modify the tests and notebook * delete Rule 4 in graph_utils and related test case. Add a test to resolve https://github.com/microsoft/autogen/pull/857/files/993fd006e922c8efe5e50bd0700e355994c6d337#r1460726831 * fix as the final comments * modify setup option from graphs to graph and add texts in optional-dependencies.md * Update autogen/graph_utils.py --------- Co-authored-by: Li Jiang <lijiang1@microsoft.com> Co-authored-by: Beibin Li <BeibinLi@users.noreply.github.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun0327@gmail.com> Co-authored-by: Yishen Sun <freedeaths@FREEDEATHS-XPS> Co-authored-by: freedeaths <register917@gmail.com>
2 years ago
Graph group chat (#857) * Move contrib-openai.yml * Moved groupgroupchat * From #753 * Removed local test references * Added ignore=test/agentchat/contrib * Trying to pass contrib-openai tests * More specific in unit testing. * Update .github/workflows/contrib-tests.yml Co-authored-by: Li Jiang <lijiang1@microsoft.com> * Remove coverage as it is included in test dependencies * Improved docstring with overview of GraphGroupChat * Iterate on feedback * Precommit pass * user just use pip install pyautogen[graphs] * Pass precommit * Pas precommit * Graph utils an test completed * Added inversion tests * Added inversion util * allow_repeat_speaker can be a list of Agents * Remove unnessary imports * Expect ValueError with 1 and 0 agents * Check that main passes all tests * Check main * Pytest all in main * All done * pre-commit changes * noqa E402 * precommit pass * Removed bin * Removed old unit test * Test test_graph_utils * minor cleanup * restore tests * Correct documentation * Special case of only one agent remaining. * Improved pytest * precommit pass * Delete OAI_CONFIG_LIST_sample copy * Returns a filtered list for auto to work * Rename var speaker_order_dict * To write test cases * Added check for a list of Agents to repeat * precommit pass * Update documentation * Extract names in allow_repeat_speaker * Post review changes * hange "pull_request_target" into "pull_request" temporarily. * 3 return values from main * pre-commit changes * PC edits * docstr changes * PC edits * Rest of changes from main * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Remove unnecessary script files from tracking * Non empty scripts files from main * Revert changes in script files to match main branch * Removed link from website as notebook is removed. * test/test_graph_utils.py is tested as part of L52 of build.yml * GroupChat ValueError check * docstr update * More clarification in docstr * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * 1.add commit to line138 in groupchat.py;2.fix bug if random choice [];3.return selected_agent if len(graph_eligible_agents) is 1;4.replace all speaker_order to speaker_transitions;5.format * fix graph_modelling notebook in the last cell * fix failure in test_groupchat.py * fix agent out of group to initiate a chat like SocietyOfMind * add a warning rule in graph_utils to check duplicates in any lists * refactor allowed_or_disallowed_speaker_transitions to Dict[Agent, List[Agent]] and modify the tests and notebook * delete Rule 4 in graph_utils and related test case. Add a test to resolve https://github.com/microsoft/autogen/pull/857/files/993fd006e922c8efe5e50bd0700e355994c6d337#r1460726831 * fix as the final comments * modify setup option from graphs to graph and add texts in optional-dependencies.md * Update autogen/graph_utils.py --------- Co-authored-by: Li Jiang <lijiang1@microsoft.com> Co-authored-by: Beibin Li <BeibinLi@users.noreply.github.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun0327@gmail.com> Co-authored-by: Yishen Sun <freedeaths@FREEDEATHS-XPS> Co-authored-by: freedeaths <register917@gmail.com>
2 years ago
Graph group chat (#857) * Move contrib-openai.yml * Moved groupgroupchat * From #753 * Removed local test references * Added ignore=test/agentchat/contrib * Trying to pass contrib-openai tests * More specific in unit testing. * Update .github/workflows/contrib-tests.yml Co-authored-by: Li Jiang <lijiang1@microsoft.com> * Remove coverage as it is included in test dependencies * Improved docstring with overview of GraphGroupChat * Iterate on feedback * Precommit pass * user just use pip install pyautogen[graphs] * Pass precommit * Pas precommit * Graph utils an test completed * Added inversion tests * Added inversion util * allow_repeat_speaker can be a list of Agents * Remove unnessary imports * Expect ValueError with 1 and 0 agents * Check that main passes all tests * Check main * Pytest all in main * All done * pre-commit changes * noqa E402 * precommit pass * Removed bin * Removed old unit test * Test test_graph_utils * minor cleanup * restore tests * Correct documentation * Special case of only one agent remaining. * Improved pytest * precommit pass * Delete OAI_CONFIG_LIST_sample copy * Returns a filtered list for auto to work * Rename var speaker_order_dict * To write test cases * Added check for a list of Agents to repeat * precommit pass * Update documentation * Extract names in allow_repeat_speaker * Post review changes * hange "pull_request_target" into "pull_request" temporarily. * 3 return values from main * pre-commit changes * PC edits * docstr changes * PC edits * Rest of changes from main * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Remove unnecessary script files from tracking * Non empty scripts files from main * Revert changes in script files to match main branch * Removed link from website as notebook is removed. * test/test_graph_utils.py is tested as part of L52 of build.yml * GroupChat ValueError check * docstr update * More clarification in docstr * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update autogen/agentchat/groupchat.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * 1.add commit to line138 in groupchat.py;2.fix bug if random choice [];3.return selected_agent if len(graph_eligible_agents) is 1;4.replace all speaker_order to speaker_transitions;5.format * fix graph_modelling notebook in the last cell * fix failure in test_groupchat.py * fix agent out of group to initiate a chat like SocietyOfMind * add a warning rule in graph_utils to check duplicates in any lists * refactor allowed_or_disallowed_speaker_transitions to Dict[Agent, List[Agent]] and modify the tests and notebook * delete Rule 4 in graph_utils and related test case. Add a test to resolve https://github.com/microsoft/autogen/pull/857/files/993fd006e922c8efe5e50bd0700e355994c6d337#r1460726831 * fix as the final comments * modify setup option from graphs to graph and add texts in optional-dependencies.md * Update autogen/graph_utils.py --------- Co-authored-by: Li Jiang <lijiang1@microsoft.com> Co-authored-by: Beibin Li <BeibinLi@users.noreply.github.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: Qingyun Wu <qingyun0327@gmail.com> Co-authored-by: Yishen Sun <freedeaths@FREEDEATHS-XPS> Co-authored-by: freedeaths <register917@gmail.com>
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import pytest
  2. import logging
  3. from autogen.agentchat import Agent
  4. import autogen.graph_utils as gru
  5. class TestHelpers:
  6. def test_has_self_loops(self):
  7. # Setup test data
  8. agents = [Agent(name=f"Agent{i}") for i in range(3)]
  9. allowed_speaker_transitions = {
  10. agents[0]: [agents[1], agents[2]],
  11. agents[1]: [agents[2]],
  12. agents[2]: [agents[0]],
  13. }
  14. allowed_speaker_transitions_with_self_loops = {
  15. agents[0]: [agents[0], agents[1], agents[2]],
  16. agents[1]: [agents[1], agents[2]],
  17. agents[2]: [agents[0]],
  18. }
  19. # Testing
  20. assert not gru.has_self_loops(allowed_speaker_transitions)
  21. assert gru.has_self_loops(allowed_speaker_transitions_with_self_loops)
  22. class TestGraphUtilCheckGraphValidity:
  23. def test_valid_structure(self):
  24. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  25. valid_speaker_transitions_dict = {agent: [other_agent for other_agent in agents] for agent in agents}
  26. gru.check_graph_validity(allowed_speaker_transitions_dict=valid_speaker_transitions_dict, agents=agents)
  27. def test_graph_with_invalid_structure(self):
  28. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  29. unseen_agent = Agent("unseen_agent")
  30. invalid_speaker_transitions_dict = {unseen_agent: ["stranger"]}
  31. with pytest.raises(ValueError):
  32. gru.check_graph_validity(invalid_speaker_transitions_dict, agents)
  33. def test_graph_with_invalid_string(self):
  34. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  35. invalid_speaker_transitions_dict = {
  36. agent: ["agent1"] for agent in agents
  37. } # 'agent1' is a string, not an Agent. Therefore raises an error.
  38. with pytest.raises(ValueError):
  39. gru.check_graph_validity(invalid_speaker_transitions_dict, agents)
  40. def test_graph_with_invalid_key(self):
  41. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  42. with pytest.raises(ValueError):
  43. gru.check_graph_validity({1: 1}, agents)
  44. # Test for Warning 1: Isolated agent nodes
  45. def test_isolated_agent_nodes_warning(self, caplog):
  46. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  47. # Create a speaker_transitions_dict where at least one agent is isolated
  48. speaker_transitions_dict_with_isolation = {agents[0]: [agents[0], agents[1]], agents[1]: [agents[0]]}
  49. # Add an isolated agent
  50. speaker_transitions_dict_with_isolation[agents[2]] = []
  51. with caplog.at_level(logging.WARNING):
  52. gru.check_graph_validity(
  53. allowed_speaker_transitions_dict=speaker_transitions_dict_with_isolation, agents=agents
  54. )
  55. assert "isolated" in caplog.text
  56. # Test for Warning 2: Warning if the set of agents in allowed_speaker_transitions do not match agents
  57. def test_warning_for_mismatch_in_agents(self, caplog):
  58. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  59. # Test with missing agents in allowed_speaker_transitions_dict
  60. unknown_agent_dict = {
  61. agents[0]: [agents[0], agents[1], agents[2]],
  62. agents[1]: [agents[0], agents[1], agents[2]],
  63. agents[2]: [agents[0], agents[1], agents[2], Agent("unknown_agent")],
  64. }
  65. with caplog.at_level(logging.WARNING):
  66. gru.check_graph_validity(allowed_speaker_transitions_dict=unknown_agent_dict, agents=agents)
  67. assert "allowed_speaker_transitions do not match agents" in caplog.text
  68. # Test for Warning 3: Warning if there is duplicated agents in allowed_speaker_transitions_dict
  69. def test_warning_for_duplicate_agents(self, caplog):
  70. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  71. # Construct an `allowed_speaker_transitions_dict` with duplicated agents
  72. duplicate_agents_dict = {
  73. agents[0]: [agents[0], agents[1], agents[2]],
  74. agents[1]: [agents[0], agents[1], agents[2], agents[1]],
  75. agents[2]: [agents[0], agents[1], agents[2], agents[0], agents[2]],
  76. }
  77. with caplog.at_level(logging.WARNING):
  78. gru.check_graph_validity(allowed_speaker_transitions_dict=duplicate_agents_dict, agents=agents)
  79. assert "duplicate" in caplog.text
  80. class TestGraphUtilInvertDisallowedToAllowed:
  81. def test_basic_functionality(self):
  82. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  83. disallowed_graph = {agents[0]: [agents[1]], agents[1]: [agents[0], agents[2]], agents[2]: []}
  84. expected_allowed_graph = {
  85. agents[0]: [agents[0], agents[2]],
  86. agents[1]: [agents[1]],
  87. agents[2]: [agents[0], agents[1], agents[2]],
  88. }
  89. # Compare names of agents
  90. inverted = gru.invert_disallowed_to_allowed(disallowed_graph, agents)
  91. assert inverted == expected_allowed_graph
  92. def test_empty_disallowed_graph(self):
  93. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  94. disallowed_graph = {}
  95. expected_allowed_graph = {
  96. agents[0]: [agents[0], agents[1], agents[2]],
  97. agents[1]: [agents[0], agents[1], agents[2]],
  98. agents[2]: [agents[0], agents[1], agents[2]],
  99. }
  100. # Compare names of agents
  101. inverted = gru.invert_disallowed_to_allowed(disallowed_graph, agents)
  102. assert inverted == expected_allowed_graph
  103. def test_fully_disallowed_graph(self):
  104. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  105. disallowed_graph = {
  106. agents[0]: [agents[0], agents[1], agents[2]],
  107. agents[1]: [agents[0], agents[1], agents[2]],
  108. agents[2]: [agents[0], agents[1], agents[2]],
  109. }
  110. expected_allowed_graph = {agents[0]: [], agents[1]: [], agents[2]: []}
  111. # Compare names of agents
  112. inverted = gru.invert_disallowed_to_allowed(disallowed_graph, agents)
  113. assert inverted == expected_allowed_graph
  114. def test_disallowed_graph_with_nonexistent_agent(self):
  115. agents = [Agent("agent1"), Agent("agent2"), Agent("agent3")]
  116. disallowed_graph = {agents[0]: [Agent("nonexistent_agent")]}
  117. # In this case, the function should ignore the nonexistent agent and proceed with the inversion
  118. expected_allowed_graph = {
  119. agents[0]: [agents[0], agents[1], agents[2]],
  120. agents[1]: [agents[0], agents[1], agents[2]],
  121. agents[2]: [agents[0], agents[1], agents[2]],
  122. }
  123. # Compare names of agents
  124. inverted = gru.invert_disallowed_to_allowed(disallowed_graph, agents)
  125. assert inverted == expected_allowed_graph