You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.0 KiB
66 lines
2.0 KiB
1 year ago
|
from typing import List, Callable
|
||
1 year ago
|
from swarms import Worker
|
||
|
|
||
1 year ago
|
|
||
1 year ago
|
class MultiAgentDebate:
|
||
1 year ago
|
def __init__(
|
||
|
self,
|
||
|
agents: List[Worker],
|
||
|
selection_func: Callable[[int, List[Worker]], int]
|
||
|
):
|
||
1 year ago
|
self.agents = agents
|
||
1 year ago
|
self.selection_func = selection_func
|
||
1 year ago
|
|
||
1 year ago
|
def reset_agents(self):
|
||
|
for agent in self.agents:
|
||
|
agent.reset()
|
||
|
|
||
|
def inject_agent(self, agent: Worker):
|
||
|
self.agents.append(agent)
|
||
1 year ago
|
|
||
1 year ago
|
def run(self, task: str, max_iters: int = None):
|
||
|
self.reset_agents()
|
||
|
results = []
|
||
|
for i in range(max_iters or len(self.agents)):
|
||
1 year ago
|
speaker_idx = self.selection_func(i, self.agents)
|
||
|
speaker = self.agents[speaker_idx]
|
||
|
response = speaker.run(task)
|
||
1 year ago
|
results.append({
|
||
1 year ago
|
'agent': speaker.ai_name,
|
||
1 year ago
|
'response': response
|
||
|
})
|
||
|
return results
|
||
|
|
||
1 year ago
|
def update_task(self, task: str):
|
||
|
self.task = task
|
||
|
|
||
|
def format_results(self, results):
|
||
|
formatted_results = "\n".join([f"Agent {result['agent']} responded: {result['response']}" for result in results])
|
||
|
return formatted_results
|
||
|
|
||
1 year ago
|
# Define a selection function
|
||
|
def select_speaker(step: int, agents: List[Worker]) -> int:
|
||
|
# This function selects the speaker in a round-robin fashion
|
||
|
return step % len(agents)
|
||
|
|
||
1 year ago
|
# Initialize agents
|
||
1 year ago
|
worker1 = Worker(openai_api_key="", ai_name="Optimus Prime")
|
||
|
worker2 = Worker(openai_api_key="", ai_name="Bumblebee")
|
||
|
worker3 = Worker(openai_api_key="", ai_name="Megatron")
|
||
|
|
||
1 year ago
|
agents = [
|
||
1 year ago
|
worker1,
|
||
|
worker2,
|
||
|
worker3
|
||
1 year ago
|
]
|
||
|
|
||
1 year ago
|
# Initialize multi-agent debate with the selection function
|
||
|
debate = MultiAgentDebate(agents, select_speaker)
|
||
1 year ago
|
|
||
|
# Run task
|
||
|
task = "What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."
|
||
1 year ago
|
results = debate.run(task, max_iters=4)
|
||
1 year ago
|
|
||
|
# Print results
|
||
|
for result in results:
|
||
|
print(f"Agent {result['agent']} responded: {result['response']}")
|