multi agent debate

Former-commit-id: 536c793ea6
group-chat
Kye 1 year ago
parent 2593b04904
commit 2ca7191c77

@ -1,51 +0,0 @@
from abc import ABC, abstractmethod
from agent_protocol import Agent, Step, Task
class AbstractAgent:
@staticmethod
async def plan(step: Step) -> Step:
task = await Agent.db.get_task(step.task_id)
steps = generate_steps(task.input)
last_step = steps[-1]
for step in steps[:-1]:
await Agent.db.create_step(
task_id=task.task_id,
name=step,
pass
)
await Agent.db.create_step(
task_id=task.task_id,
name=last_step,
is_last=True
)
step.output = steps
return step
@staticmethod
async def execute(step: Step) -> Step:
# Use tools, websearch, etc.
...
@staticmethod
async def task_handler(task: Task) -> None:
await Agent.db.create_step(
task_id=task.task_id,
name="plan",
pass
)
@staticmethod
async def step_handler(step: Step) -> Step:
if step.name == "plan":
await AbstractAgent.plan(step)
else:
await AbstractAgent.execute(step)
return step
@staticmethod
def start_agent():
Agent.setup_agent(AbstractAgent.task_handler, AbstractAgent.step_handler).start()

@ -6,3 +6,5 @@ from swarms.swarms.orchestrate import Orchestrator
from swarms.swarms.god_mode import GodMode from swarms.swarms.god_mode import GodMode
from swarms.swarms.simple_swarm import SimpleSwarm from swarms.swarms.simple_swarm import SimpleSwarm
from swarms.swarms.multi_agent_debate import MultiAgentDebate from swarms.swarms.multi_agent_debate import MultiAgentDebate
from swarms.swarms.groupchat import GroupChat

@ -19,3 +19,5 @@ class AbstractSwarm(ABC):
@abstractmethod @abstractmethod
def run(self): def run(self):
pass pass

@ -7,29 +7,29 @@ from swarms.workers.worker import Worker
@dataclass @dataclass
class GroupChat: class GroupChat:
"""A group chat with multiple participants with a list of agents and a max number of rounds""" """A group chat with multiple participants with a list of workers and a max number of rounds"""
agents: List[Worker] workers: List[Worker]
messages: List[Dict] messages: List[Dict]
max_rounds: int = 10 max_rounds: int = 10
admin_name: str = "Admin" #admin agent admin_name: str = "Admin" #admin worker
@property @property
def agent_names(self) -> List[str]: def worker_names(self) -> List[str]:
"""returns the names of the agents in the group chat""" """returns the names of the workers in the group chat"""
return [agent.ai_name for agent in self.agents] return [worker.ai_name for worker in self.workers]
def reset(self): def reset(self):
self.messages.clear() self.messages.clear()
def agent_by_name(self, name: str) -> Worker: def worker_by_name(self, name: str) -> Worker:
"""Find the next speaker baed on the message""" """Find the next speaker baed on the message"""
return self.agents[self.agent_names.index(name)] return self.workers[self.worker_names.index(name)]
def next_agent(self, agent: Worker) -> Worker: def next_worker(self, worker: Worker) -> Worker:
"""Returns the next agent in the list""" """Returns the next worker in the list"""
return self.agents[ return self.workers[
(self.agents_names.index(agent.ai_name) + 1) % len(self.agents) (self.workers_names.index(worker.ai_name) + 1) % len(self.workers)
] ]
def select_speaker_msg(self): def select_speaker_msg(self):
@ -39,7 +39,7 @@ class GroupChat:
You are in a role play game the following rules are available: You are in a role play game the following rules are available:
{self.__participant_roles()}. {self.__participant_roles()}.
Read the following conversation then select the next role from {self.agent_names} Read the following conversation then select the next role from {self.worker_names}
to play and only return the role to play and only return the role
""" """
@ -55,20 +55,20 @@ class GroupChat:
self.messages + [ self.messages + [
{ {
"role": "system", "role": "system",
"context": f"Read the above conversation. Then select the next role from {self.agent_names} to play. Only return the role.", "context": f"Read the above conversation. Then select the next role from {self.worker_names} to play. Only return the role.",
} }
] ]
) )
if not final: if not final:
return self.next_agent(last_speaker) return self.next_worker(last_speaker)
try: try:
return self.agent_by_name(name) return self.worker_by_name(name)
except ValueError: except ValueError:
return self.next_agent(last_speaker) return self.next_worker(last_speaker)
def _participant_roles(self): def _participant_roles(self):
return "\n".join( return "\n".join(
[f"{agent.ai_name}: {agent.system_message}" for agent in self.agents] [f"{worker.ai_name}: {worker.system_message}" for worker in self.workers]
) )
@ -117,12 +117,12 @@ class GroupChatManager(Worker):
groupchat.messages.append(message) groupchat.messages.append(message)
#broadcast the message to all agents except the speaker #broadcast the message to all workers except the speaker
for agent in groupchat.agents: for worker in groupchat.workers:
if agent != speaker: if worker != speaker:
self.send( self.send(
message, message,
agent, worker,
request_reply=False, request_reply=False,
silent=True, silent=True,
) )
@ -137,12 +137,12 @@ class GroupChatManager(Worker):
except KeyboardInterrupt: except KeyboardInterrupt:
#let the admin speak if interrupted #let the admin speak if interrupted
if groupchat.admin_name in groupchat.agent_names: if groupchat.admin_name in groupchat.worker_names:
#admin agent is a particpant #admin worker is a particpant
speaker = groupchat.agent_by_name(groupchat.admin_name) speaker = groupchat.worker_by_name(groupchat.admin_name)
reply = speaker.generate_reply(sender=self) reply = speaker.generate_reply(sender=self)
else: else:
#admin agent is not found in particpants #admin worker is not found in particpants
raise raise
if reply is None: if reply is None:
break break

@ -4,8 +4,8 @@ class SimpleSwarm:
def __init__( def __init__(
self, self,
num_workers, num_workers,
openai_api_key, openai_api_key,
ai_name ai_name
): ):
""" """
@ -24,11 +24,12 @@ class SimpleSwarm:
def run(self, task): def run(self, task):
responses = [] responses = []
for worker in self.workers: for worker in self.workers:
response = worker.run(task) response = worker.run(task)
responses.append(response) responses.append(response)
return responses return responses
def __call__(self, task): def __call__(self, task):
return self.run(task) return self.run(task)
Loading…
Cancel
Save