Skip to content

Commit

Permalink
groupchat notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sonichi committed Aug 2, 2023
1 parent fa83f21 commit 547e248
Show file tree
Hide file tree
Showing 3 changed files with 579 additions and 3 deletions.
31 changes: 28 additions & 3 deletions flaml/autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ class ChatManagerAgent(ResponsiveAgent):

broadcaster: BroadcastAgent
max_round: int
SELECT_SPEAKER_MSG = "select_speaker prompt" # TODO: replace the prompt

def _select_speaker_msg(self):
return {
"role": "system",
"content": f"""You are in a role play game. Read the following conversation.
Then select the next role from {self._agent_names} to play. Only return the role.""",
}

def __init__(
self,
Expand Down Expand Up @@ -108,7 +114,7 @@ def _generate_reply_for_participant(
def _select_speaker(self):
"""Select the next speaker."""
i = self._random.randint(0, len(self._agent_names) - 1) # randomly pick an id
self.send(self.SELECT_SPEAKER_MSG, self.broadcaster.agents[i])
self.send(self._select_speaker_msg(), self.broadcaster.agents[i])

def _find_next_speaker(self, message: Dict) -> str:
"""Find the next speaker based on the message."""
Expand Down Expand Up @@ -166,12 +172,31 @@ def _generate_reply_for_chat_manager(
if messages is None:
messages = self._oai_messages[sender.name]
message = messages[-1]
sender = self.chat_manager.broadcaster
if message["content"] == "speak":
sender = self.chat_manager.broadcaster
reply = super().generate_reply(
self.chat_messages[sender.name], default_reply, sender, class_specific_reply=False
)
self.send(reply, self.chat_manager.broadcaster)
else:
# TODO: run _speaker_selection() and return the result
return self._speaker_selection(message)

def _speaker_selection(self, instruction):
"""Select the next speaker."""
if self.llm_config is False:
return self.name
sender = self.chat_manager.broadcaster
roles_msg = {
"content": f"""The following roles are available:
{self._participant_roles()}""",
"role": "system",
}
old_system_msg = self.system_message
self.update_system_message(instruction["content"])
reply = self._oai_reply([roles_msg] + self.chat_messages[sender.name])
self.update_system_message(old_system_msg)
return reply

def _participant_roles(self):
return "\n".join([f"{agent.name}: {agent.system_message}" for agent in self.chat_manager.broadcaster.agents])
4 changes: 4 additions & 0 deletions flaml/autogen/agentchat/responsive_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def register_class_specific_reply(self, class_type, reply_func: Callable):
"""
self._class_specific_reply.append((class_type, reply_func))

def system_message(self):
"""Return the system message."""
return self._oai_system_message[0]["content"]

def update_system_message(self, system_message: str):
"""Update the system message.
Expand Down
Loading

0 comments on commit 547e248

Please sign in to comment.