@ -23,22 +23,6 @@ bid_parser = BidOutputParser(
)
def select_next_speaker_director ( step : int , agents , director ) - > int :
# if the step if even => director
# => director selects next speaker
if step % 2 == 1 :
idx = 0
else :
idx = director . select_next_speaker ( ) + 1
return idx
# Define a selection function
def select_speaker_round_table ( step : int , agents ) - > int :
# This function selects the speaker in a round-robin fashion
return step % len ( agents )
# main
class MultiAgentCollaboration :
"""
@ -49,6 +33,15 @@ class MultiAgentCollaboration:
selection_function ( callable ) : The function that selects the next speaker .
Defaults to select_next_speaker .
max_iters ( int ) : The maximum number of iterations . Defaults to 10.
autosave ( bool ) : Whether to autosave the state of all agents . Defaults to True .
saved_file_path_name ( str ) : The path to the saved file . Defaults to
" multi_agent_collab.json " .
stopping_token ( str ) : The token that stops the collaboration . Defaults to
" <DONE> " .
results ( list ) : The results of the collaboration . Defaults to [ ] .
logger ( logging . Logger ) : The logger . Defaults to logger .
logging ( bool ) : Whether to log the collaboration . Defaults to True .
Methods :
reset : Resets the state of all agents .
@ -62,18 +55,40 @@ class MultiAgentCollaboration:
Usage :
>> > from swarms . models import MultiAgentCollaboration
>> > from swarms . models import Flow
>> > from swarms . models import OpenAIChat
>> > from swarms . models import Anthropic
>> > from swarms . structs import Flow
>> > from swarms . swarms . multi_agent_collab import MultiAgentCollaboration
>> >
>> > # Initialize the language model
>> > llm = OpenAIChat (
>> > temperature = 0.5 ,
>> > )
>> >
>> >
>> > ## Initialize the workflow
>> > flow = Flow ( llm = llm , max_loops = 1 , dashboard = True )
>> >
>> > # Run the workflow on a task
>> > out = flow . run ( " Generate a 10,000 word blog on health and wellness. " )
>> >
>> > # Initialize the multi-agent collaboration
>> > swarm = MultiAgentCollaboration (
>> > agents = [ flow ] ,
>> > max_iters = 4 ,
>> > )
>> >
>> > # Run the multi-agent collaboration
>> > swarm . run ( )
>> >
>> > # Format the results of the multi-agent collaboration
>> > swarm . format_results ( swarm . results )
"""
def __init__ (
self ,
agents : List [ Flow ] ,
selection_function : callable = select_next_speaker_director ,
selection_function : callable = None ,
max_iters : int = 10 ,
autosave : bool = True ,
saved_file_path_name : str = " multi_agent_collab.json " ,
@ -165,7 +180,7 @@ class MultiAgentCollaboration:
) ,
retry_error_callback = lambda retry_state : 0 ,
)
def run ( self ) :
def run _director ( self , task : str ) :
""" Runs the multi-agent collaboration. """
n = 0
self . reset ( )
@ -179,6 +194,74 @@ class MultiAgentCollaboration:
print ( " \n " )
n + = 1
def select_next_speaker_roundtable ( self , step : int , agents : List [ Flow ] ) - > int :
""" Selects the next speaker. """
return step % len ( agents )
def select_next_speaker_director ( step : int , agents : List [ Flow ] , director ) - > int :
# if the step if even => director
# => director selects next speaker
if step % 2 == 1 :
idx = 0
else :
idx = director . select_next_speaker ( ) + 1
return idx
# def run(self, task: str):
# """Runs the multi-agent collaboration."""
# for step in range(self.max_iters):
# speaker_idx = self.select_next_speaker_roundtable(step, self.agents)
# speaker = self.agents[speaker_idx]
# result = speaker.run(task)
# self.results.append({"agent": speaker, "response": result})
# if self.autosave:
# self.save_state()
# if result == self.stopping_token:
# break
# return self.results
# def run(self, task: str):
# for _ in range(self.max_iters):
# for step, agent, in enumerate(self.agents):
# result = agent.run(task)
# self.results.append({"agent": agent, "response": result})
# if self.autosave:
# self.save_state()
# if result == self.stopping_token:
# break
# return self.results
# def run(self, task: str):
# conversation = task
# for _ in range(self.max_iters):
# for agent in self.agents:
# result = agent.run(conversation)
# self.results.append({"agent": agent, "response": result})
# conversation = result
# if self.autosave:
# self.save()
# if result == self.stopping_token:
# break
# return self.results
def run ( self , task : str ) :
conversation = task
for _ in range ( self . max_iters ) :
for agent in self . agents :
result = agent . run ( conversation )
self . results . append ( { " agent " : agent , " response " : result } )
conversation + = result
if self . autosave :
self . save_state ( )
if result == self . stopping_token :
break
return self . results
def format_results ( self , results ) :
""" Formats the results of the run method """
formatted_results = " \n " . join (