feat: Add groupchat example

Former-commit-id: c36b240bd7ac36cfcca372c397bfb96702aabd4e
group-chat
Zack 2 years ago
parent a8c1b3839b
commit 871186dabe

@ -0,0 +1,46 @@
from swarms.swarms.groupchat import GroupChat
from langchain.llms import OpenAIChat
from swarms import Worker
llm = OpenAIChat(
model_name='gpt-4',
openai_api_key="api-key",
temperature=0.5
)
Worker1 = Worker(
llm=llm,
ai_name="Optimus Prime",
ai_role="Worker in a swarm",
external_tools=None,
human_in_the_loop=False,
temperature=0.5,
)
Worker2 = Worker(
llm=llm,
ai_name="Optimus Prime",
ai_role="Worker in a swarm",
external_tools=None,
human_in_the_loop=False,
temperature=0.5,
)
workers = [Worker1, Worker2]
messages = [
{
"role": "system",
"context": f"Read the above conversation. Then select the next role from {self.agent_names} to play. Only return the role.",
}
]
agent = GroupChat(
workers,
messages,
name="groupchat",
max_consecutive_auto_reply=1,
human_input_mode="NEVER",
system_message="Group chat manager",
)
agent.run(messages, workers)

@ -8,7 +8,6 @@
from swarms.agents.omni_modal_agent import OmniModalAgent from swarms.agents.omni_modal_agent import OmniModalAgent
# utils # utils
from swarms.agents.message import Message from swarms.agents.message import Message
from swarms.agents.stream_response import stream from swarms.agents.stream_response import stream

@ -4,6 +4,7 @@ from typing import Dict, List, Optional, Union
from swarms.workers.worker import Worker from swarms.workers.worker import Worker
@dataclass @dataclass
class GroupChat: class GroupChat:
"""A group chat with multiple participants with a list of workers and a max number of rounds""" """A group chat with multiple participants with a list of workers and a max number of rounds"""
@ -11,7 +12,11 @@ class GroupChat:
workers: List[Worker] workers: List[Worker]
messages: List[Dict] messages: List[Dict]
max_rounds: int = 10 max_rounds: int = 10
<<<<<<< Updated upstream
admin_name: str = "Admin" #admin worker admin_name: str = "Admin" #admin worker
=======
admin_name: str = "Admin" # admin agent
>>>>>>> Stashed changes
@property @property
def worker_names(self) -> List[str]: def worker_names(self) -> List[str]:
@ -20,10 +25,17 @@ class GroupChat:
def reset(self): def reset(self):
self.messages.clear() self.messages.clear()
<<<<<<< Updated upstream
def worker_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.workers[self.worker_names.index(name)] return self.workers[self.worker_names.index(name)]
=======
def agent_by_name(self, name: str) -> Worker:
"""Find the next speaker baed on the message"""
return self.agents[self.agent_names.index(name)]
>>>>>>> Stashed changes
def next_worker(self, worker: Worker) -> Worker: def next_worker(self, worker: Worker) -> Worker:
"""Returns the next worker in the list""" """Returns the next worker in the list"""
@ -67,11 +79,10 @@ class GroupChat:
def _participant_roles(self): def _participant_roles(self):
return "\n".join( return "\n".join(
[f"{worker.ai_name}: {worker.system_message}" for worker in self.workers] [f"{agent.ai_name}: {agent.system_message}" for agent in self.workers]
) )
class GroupChatManager(Worker): class GroupChatManager(Worker):
def __init__( def __init__(
self, self,
@ -142,7 +153,7 @@ class GroupChatManager(Worker):
speaker = groupchat.worker_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 worker is not found in particpants #admin agent is not found in particpants
raise raise
if reply is None: if reply is None:
break break

Loading…
Cancel
Save