Former-commit-id: b2bd28199f
group-chat
Kye 2 years ago
parent 4b455ba47f
commit a8c1b3839b

@ -11,24 +11,24 @@ class GroupChat:
workers: 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 workers in the group chat""" """returns the names of the workers in the group chat"""
return [agent.ai_name for agent in self.workers] 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.workers[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.workers[ return self.workers[
(self.workers_names.index(agent.ai_name) + 1) % len(self.workers) (self.workers_names.index(worker.ai_name) + 1) % len(self.workers)
] ]
def select_speaker_msg(self): def select_speaker_msg(self):
@ -38,7 +38,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
""" """
@ -54,20 +54,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.workers] [f"{worker.ai_name}: {worker.system_message}" for worker in self.workers]
) )
@ -118,11 +118,11 @@ class GroupChatManager(Worker):
groupchat.messages.append(message) groupchat.messages.append(message)
#broadcast the message to all workers except the speaker #broadcast the message to all workers except the speaker
for agent in groupchat.workers: 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

Loading…
Cancel
Save