Skip to content

Commit

Permalink
Add GroupChatRunChat
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Dec 30, 2024
1 parent 556e38b commit d0f27e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
10 changes: 8 additions & 2 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
from ..formatting_utils import colored
from ..graph_utils import check_graph_validity, invert_disallowed_to_allowed
from ..io.base import IOStream
from ..messages import create_clear_agents_history, create_group_chat_resume, create_speaker_attempt
from ..messages import (
create_clear_agents_history,
create_group_chat_resume,
create_group_chat_run_chat,
create_speaker_attempt,
)
from ..oai.client import ModelClient
from ..runtime_logging import log_new_agent, logging_enabled
from .agent import Agent
Expand Down Expand Up @@ -1149,7 +1154,8 @@ def run_chat(
speaker = groupchat.select_speaker(speaker, self)
if not silent:
iostream = IOStream.get_default()
iostream.print(colored(f"\nNext speaker: {speaker.name}\n", "green"), flush=True)
group_chat_run_chat = create_group_chat_run_chat(speaker, silent)
group_chat_run_chat.print(iostream.print)
# let the speaker speak
reply = speaker.generate_reply(sender=self)
except KeyboardInterrupt:
Expand Down
15 changes: 15 additions & 0 deletions autogen/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,18 @@ def create_group_chat_resume(
last_speaker_name: str, messages: list[dict[str, Any]], silent: Optional[bool] = False
) -> GroupChatResume:
return GroupChatResume(last_speaker_name=last_speaker_name, messages=messages, verbose=not silent)


class GroupChatRunChat(BaseModel):
speaker_name: str
verbose: Optional[bool] = False

def print(self, f: Optional[Callable[..., Any]] = None) -> None:
f = f or print

if self.verbose:
f(colored(f"\nNext speaker: {self.speaker_name}\n", "green"), flush=True)


def create_group_chat_run_chat(speaker: Agent, silent: Optional[bool] = False) -> GroupChatRunChat:
return GroupChatRunChat(speaker_name=speaker.name, verbose=not silent)
24 changes: 24 additions & 0 deletions test/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
FunctionCallMessage,
FunctionResponseMessage,
GroupChatResume,
GroupChatRunChat,
MessageRole,
PostCarryoverProcessing,
SpeakerAttempt,
Expand All @@ -25,6 +26,7 @@
ToolResponseMessage,
create_clear_agents_history,
create_group_chat_resume,
create_group_chat_run_chat,
create_post_carryover_processing,
create_received_message_model,
create_speaker_attempt,
Expand Down Expand Up @@ -487,3 +489,25 @@ def test_group_chat_resume() -> None:
]

assert mock.call_args_list == expected_call_args_list


def test_group_chat_run_chat() -> None:
speaker = ConversableAgent(
"assistant uno", max_consecutive_auto_reply=0, llm_config=False, human_input_mode="NEVER"
)
silent = False

actual = create_group_chat_run_chat(speaker, silent)

assert isinstance(actual, GroupChatRunChat)
assert actual.speaker_name == "assistant uno"
assert actual.verbose is True

mock = MagicMock()
actual.print(f=mock)

# print(mock.call_args_list)

expected_call_args_list = [call("\x1b[32m\nNext speaker: assistant uno\n\x1b[0m", flush=True)]

assert mock.call_args_list == expected_call_args_list

0 comments on commit d0f27e6

Please sign in to comment.