Skip to content

Commit 9566fa2

Browse files
(tests) Add tests that make sure combo instruction expansion working
1 parent 9f5e33e commit 9566fa2

File tree

1 file changed

+161
-38
lines changed

1 file changed

+161
-38
lines changed

plurals/tests.py

Lines changed: 161 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def test_moderator_manual(self):
458458
mod = Moderator(
459459
persona="You are a conservative moderator overseeing a discussion about the following task: ${task}.",
460460
combination_instructions="- Here are the previous responses: ${previous_responses}- Take only the most "
461-
"conservative parts of what was previously said.",
461+
"conservative parts of what was previously said.",
462462
)
463463
mixed = Chain([a2, a3, a4], task=self.task, moderator=mod)
464464
mixed.process()
@@ -525,16 +525,16 @@ def test_chain_multiple_cycles(self):
525525
chain = Chain([agent1, agent2], task=task, cycles=3)
526526

527527
with patch.object(
528-
Agent,
529-
"_get_response",
530-
side_effect=[
531-
"Pros: flexibility, no commute. Cons: isolation.",
532-
"Pros: reduced office costs. Cons: difficulty in team building.",
533-
"Agree on flexibility, add increased productivity as pro.",
534-
"Agree on team building issues, add potential for overwork.",
535-
"Emphasize need for balance and hybrid models.",
536-
"Suggest importance of clear communication and expectations.",
537-
],
528+
Agent,
529+
"_get_response",
530+
side_effect=[
531+
"Pros: flexibility, no commute. Cons: isolation.",
532+
"Pros: reduced office costs. Cons: difficulty in team building.",
533+
"Agree on flexibility, add increased productivity as pro.",
534+
"Agree on team building issues, add potential for overwork.",
535+
"Emphasize need for balance and hybrid models.",
536+
"Suggest importance of clear communication and expectations.",
537+
],
538538
):
539539
chain.process()
540540

@@ -745,9 +745,9 @@ def test_chain_current_task_description(self):
745745

746746
# Mock ONLY _get_response from `process()`.
747747
with patch.object(
748-
Agent,
749-
"_get_response",
750-
return_value="Social media has both positive and negative impacts on society.",
748+
Agent,
749+
"_get_response",
750+
return_value="Social media has both positive and negative impacts on society.",
751751
):
752752
chain.process()
753753

@@ -786,13 +786,13 @@ def test_chain_with_different_agent_tasks(self):
786786
chain = Chain([agent1, agent2, agent3])
787787

788788
with patch.object(
789-
Agent,
790-
"_get_response",
791-
side_effect=[
792-
"Pros: flexibility, no commute.",
793-
"Cons: isolation, difficulty in team building.",
794-
"Summary: Remote work offers flexibility but poses challenges in collaboration.",
795-
],
789+
Agent,
790+
"_get_response",
791+
side_effect=[
792+
"Pros: flexibility, no commute.",
793+
"Cons: isolation, difficulty in team building.",
794+
"Summary: Remote work offers flexibility but poses challenges in collaboration.",
795+
],
796796
):
797797
chain.process()
798798

@@ -974,14 +974,14 @@ def test_debate_with_different_agent_tasks(self):
974974
debate = Debate([agent1, agent2], cycles=2)
975975

976976
with patch.object(
977-
Agent,
978-
"_get_response",
979-
side_effect=[
980-
"Stricter laws will reduce gun violence.",
981-
"Stricter laws infringe on Second Amendment rights.",
982-
"Gun control laws have been effective in other countries.",
983-
"Law-abiding citizens need guns for self-defense.",
984-
],
977+
Agent,
978+
"_get_response",
979+
side_effect=[
980+
"Stricter laws will reduce gun violence.",
981+
"Stricter laws infringe on Second Amendment rights.",
982+
"Gun control laws have been effective in other countries.",
983+
"Law-abiding citizens need guns for self-defense.",
984+
],
985985
):
986986
debate.process()
987987

@@ -1251,13 +1251,13 @@ def test_graph_with_different_agent_tasks(self):
12511251
graph = Graph(agents=agents, edges=edges)
12521252

12531253
with patch.object(
1254-
Agent,
1255-
"process",
1256-
side_effect=[
1257-
"Economic policies should focus on growth.",
1258-
"Social policies should address inequality.",
1259-
"Both economic growth and social equality are important policy goals.",
1260-
],
1254+
Agent,
1255+
"process",
1256+
side_effect=[
1257+
"Economic policies should focus on growth.",
1258+
"Social policies should address inequality.",
1259+
"Both economic growth and social equality are important policy goals.",
1260+
],
12611261
):
12621262
final_response = graph.process()
12631263

@@ -2087,11 +2087,12 @@ def test_structure_repr_with_empty_collections(self):
20872087
print(repr_string)
20882088
self.assertIn("'responses': []", repr_string)
20892089

2090+
20902091
class TestPathHandling(unittest.TestCase):
20912092
"""Test cases for resource path handling."""
20922093

20932094
def test_load_yaml_pkg_resources_paths(self):
2094-
"""Test load_yaml works both with and without pkg_resources."""
2095+
"""Test load_yaml works both with and without pkg_resources. This has to do with different versions of Python"""
20952096

20962097
# test we can load stuff with pkg_resources
20972098
yaml_content_pkg_resources = load_yaml("instructions.yaml")
@@ -2106,9 +2107,131 @@ def test_load_yaml_pkg_resources_paths(self):
21062107
self.assertIn("persona_template", yaml_content_pathlib)
21072108
self.assertIn("combination_instructions", yaml_content_pathlib)
21082109

2109-
# assert equality of the two methods
21102110
self.assertEqual(yaml_content_pkg_resources, yaml_content_pathlib)
21112111

21122112

2113+
class CombinationInstructionExpansionTests(unittest.TestCase):
2114+
"""Tests verifying the proper expansion of combination instruction templates across structures
2115+
2116+
Notes:
2117+
- We don't need to test ensemble because it never uses combination instructions
2118+
- The reason why I am redefining agents a bunch of times is because if you use the same Agents, e.g. in setup,
2119+
then their combination instructions are already set
2120+
2121+
"""
2122+
2123+
def test_structure_template_expansion(self):
2124+
"""Test that when no agent has combination instructions, they inherit correctly expanded template from structure"""
2125+
task = "Generate argument for social media in 10 words."
2126+
2127+
# Chain
2128+
chain_agents = [
2129+
Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2130+
Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2131+
]
2132+
chain = Chain(chain_agents, task=task, combination_instructions="chain")
2133+
chain.process()
2134+
chain_template = DEFAULTS["combination_instructions"]["chain"]
2135+
for agent in chain.agents:
2136+
self.assertEqual(chain_template, agent.combination_instructions)
2137+
2138+
debate_agents = [
2139+
Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2140+
Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2141+
]
2142+
debate = Debate(debate_agents, task=task, combination_instructions="debate")
2143+
debate.process()
2144+
debate_template = DEFAULTS["combination_instructions"]["debate"]
2145+
for agent in debate.agents:
2146+
self.assertEqual(debate_template, agent.combination_instructions)
2147+
2148+
graph_agents = {
2149+
"agent1": Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2150+
"agent2": Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2151+
}
2152+
graph = Graph(
2153+
agents=graph_agents,
2154+
edges=[("agent1", "agent2")],
2155+
combination_instructions="critique_revise"
2156+
)
2157+
graph.process()
2158+
critique_template = DEFAULTS["combination_instructions"]["critique_revise"]
2159+
for agent in graph.agents:
2160+
self.assertEqual(critique_template, agent.combination_instructions)
2161+
2162+
def test_agent_template_expansion(self):
2163+
"""Test that agents with template names get their templates properly expanded"""
2164+
task = "Generate argument for social media in 10 words."
2165+
2166+
chain_agents = [
2167+
Agent(task=task, combination_instructions="default", model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2168+
Agent(task=task, combination_instructions="chain", model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2169+
]
2170+
chain = Chain(chain_agents, task=task)
2171+
chain.process()
2172+
self.assertEqual(DEFAULTS["combination_instructions"]["default"], chain_agents[0].combination_instructions)
2173+
self.assertEqual(DEFAULTS["combination_instructions"]["chain"], chain_agents[1].combination_instructions)
2174+
2175+
debate_agents = [
2176+
Agent(task=task, combination_instructions="debate", model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2177+
Agent(task=task, combination_instructions="default", model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2178+
]
2179+
debate = Debate(debate_agents, task=task)
2180+
debate.process()
2181+
self.assertEqual(DEFAULTS["combination_instructions"]["debate"], debate_agents[0].combination_instructions)
2182+
self.assertEqual(DEFAULTS["combination_instructions"]["default"], debate_agents[1].combination_instructions)
2183+
2184+
graph_agents = {
2185+
"agent1": Agent(task=task, combination_instructions="critique_revise", model="gpt-3.5-turbo",
2186+
kwargs={"max_tokens": 30}),
2187+
"agent2": Agent(task=task, combination_instructions="default", model="gpt-3.5-turbo",
2188+
kwargs={"max_tokens": 30})
2189+
}
2190+
graph = Graph(agents=graph_agents, edges=[("agent1", "agent2")])
2191+
graph.process()
2192+
agent_list = list(graph_agents.values())
2193+
self.assertEqual(DEFAULTS["combination_instructions"]["critique_revise"],
2194+
agent_list[0].combination_instructions)
2195+
self.assertEqual(DEFAULTS["combination_instructions"]["default"], agent_list[1].combination_instructions)
2196+
2197+
def test_mixed_template_expansion(self):
2198+
"""Test when some agents have templates and others don't"""
2199+
task = "Generate argument for social media in 10 words."
2200+
2201+
chain_agents = [
2202+
Agent(task=task, combination_instructions="default", model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2203+
Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2204+
]
2205+
chain = Chain(chain_agents, task=task, combination_instructions="chain")
2206+
chain.process()
2207+
self.assertEqual(DEFAULTS["combination_instructions"]["default"], chain_agents[0].combination_instructions)
2208+
self.assertEqual(DEFAULTS["combination_instructions"]["chain"], chain_agents[1].combination_instructions)
2209+
2210+
debate_agents = [
2211+
Agent(task=task, combination_instructions="debate", model="gpt-3.5-turbo", kwargs={"max_tokens": 30}),
2212+
Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2213+
]
2214+
debate = Debate(debate_agents, task=task, combination_instructions="chain")
2215+
debate.process()
2216+
self.assertEqual(DEFAULTS["combination_instructions"]["debate"], debate_agents[0].combination_instructions)
2217+
self.assertEqual(DEFAULTS["combination_instructions"]["chain"], debate_agents[1].combination_instructions)
2218+
2219+
graph_agents = {
2220+
"agent1": Agent(task=task, combination_instructions="critique_revise", model="gpt-3.5-turbo",
2221+
kwargs={"max_tokens": 30}),
2222+
"agent2": Agent(task=task, model="gpt-3.5-turbo", kwargs={"max_tokens": 30})
2223+
}
2224+
graph = Graph(
2225+
agents=graph_agents,
2226+
edges=[("agent1", "agent2")],
2227+
combination_instructions="chain"
2228+
)
2229+
graph.process()
2230+
agent_list = list(graph_agents.values())
2231+
self.assertEqual(DEFAULTS["combination_instructions"]["critique_revise"],
2232+
agent_list[0].combination_instructions)
2233+
self.assertEqual(DEFAULTS["combination_instructions"]["chain"], agent_list[1].combination_instructions)
2234+
2235+
21132236
if __name__ == "__main__":
21142237
unittest.main()

0 commit comments

Comments
 (0)