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.
swarms/example2.py

43 lines
1.5 KiB

1 year ago
from typing import List, Callable
1 year ago
from swarms import Worker
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
def run(self, task: str):
results = []
1 year ago
for i in range(len(self.agents)):
# Select the speaker based on the selection function
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
# 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
agents = [
Worker(openai_api_key="", ai_name="Optimus Prime"),
Worker(openai_api_key="", ai_name="Bumblebee"),
Worker(openai_api_key="", ai_name="Megatron")
]
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."
results = debate.run(task)
# Print results
for result in results:
print(f"Agent {result['agent']} responded: {result['response']}")