Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add MessageTransforms to the GroupChat's Select Speaker nested chat (speaker_selection_method='auto') #2719

Merged
merged 21 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
aea58fa
Initial commit with ability to add transforms to GroupChat
marklysze May 19, 2024
9de72c0
Added tests
marklysze May 19, 2024
678f9c0
Tidy up
marklysze May 19, 2024
918a1d0
Tidy up of variable names and commented out test
marklysze May 24, 2024
fad0a77
Tidy up comment
marklysze May 24, 2024
8c203d6
Update import to relative
marklysze Jun 4, 2024
3fb7a14
Added documentation topic for transform messages for speaker selection.
marklysze Jun 5, 2024
4e05e03
Merge remote-tracking branch 'origin/main' into selectspeakertransforms
marklysze Jun 28, 2024
f7328bc
Merge remote-tracking branch 'origin/main' into selectspeakertransforms
marklysze Jul 24, 2024
c479a81
Merge branch 'main' into selectspeakertransforms
marklysze Aug 8, 2024
8e3f6d9
Merge branch 'main' into selectspeakertransforms
thinkall Aug 12, 2024
430a7e4
Merge branch 'main' into selectspeakertransforms
thinkall Aug 14, 2024
44048f7
Merge remote-tracking branch 'origin/main' into selectspeakertransforms
marklysze Aug 19, 2024
4c7b8d2
Merge branch 'selectspeakertransforms' of https://github.com/microsof…
marklysze Aug 19, 2024
3cf1091
Added handling of non-core module, transforms, in groupchat
marklysze Aug 19, 2024
49b7367
Adjusted parameter if module not loaded.
marklysze Aug 19, 2024
7dd60bc
Merge branch 'main' into selectspeakertransforms
thinkall Aug 20, 2024
9293ddb
Merge branch 'main' into selectspeakertransforms
marklysze Aug 20, 2024
9ce18ec
Merge branch 'main' into selectspeakertransforms
thinkall Aug 20, 2024
4b797c3
Merge branch 'main' into selectspeakertransforms
marklysze Aug 23, 2024
aa334fc
Updated groupchat test which failed during CI/CD
marklysze Aug 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from typing import Dict, List

from autogen import ConversableAgent
from autogen.agentchat import ConversableAgent
marklysze marked this conversation as resolved.
Show resolved Hide resolved

from ....formatting_utils import colored
from .transforms import MessageTransform
Expand Down
21 changes: 21 additions & 0 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ..runtime_logging import log_new_agent, logging_enabled
from .agent import Agent
from .chat import ChatResult
from .contrib.capabilities import transform_messages
thinkall marked this conversation as resolved.
Show resolved Hide resolved
from .conversable_agent import ConversableAgent

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -75,6 +76,8 @@ def custom_speaker_selection_func(
of times until a single agent is returned or it exhausts the maximum attempts.
Applies only to "auto" speaker selection method.
Default is 2.
- select_speaker_transform_messages: (optional) the message transformations to apply to the nested select speaker agent-to-agent chat messages.
Takes a TransformMessages object, defaults to None and is only utilised when the speaker selection method is "auto".
- select_speaker_auto_verbose: whether to output the select speaker responses and selections
If set to True, the outputs from the two agents in the nested select speaker chat will be output, along with
whether the responses were successful, or not, in selecting an agent
Expand Down Expand Up @@ -131,6 +134,7 @@ def custom_speaker_selection_func(
The names are case-sensitive and should not be abbreviated or changed.
The only names that are accepted are {agentlist}.
Respond with ONLY the name of the speaker and DO NOT provide a reason."""
select_speaker_transform_messages: Optional[transform_messages.TransformMessages] = None
select_speaker_auto_verbose: Optional[bool] = False
role_for_select_speaker_messages: Optional[str] = "system"

Expand Down Expand Up @@ -248,6 +252,15 @@ def __post_init__(self):
elif self.max_retries_for_selecting_speaker < 0:
raise ValueError("max_retries_for_selecting_speaker must be greater than or equal to zero")

# Load message transforms here (load once for the Group Chat so we don't have to re-initiate it and it maintains the cache across subsequent select speaker calls)
if self.select_speaker_transform_messages is not None:
if isinstance(self.select_speaker_transform_messages, transform_messages.TransformMessages):
self._speaker_selection_transforms = self.select_speaker_transform_messages
else:
raise ValueError("select_speaker_transform_messages must be None or MessageTransforms.")
else:
self._speaker_selection_transforms = None

# Validate select_speaker_auto_verbose
if self.select_speaker_auto_verbose is None or not isinstance(self.select_speaker_auto_verbose, bool):
raise ValueError("select_speaker_auto_verbose cannot be None or non-bool")
Expand Down Expand Up @@ -633,6 +646,10 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
human_input_mode="NEVER", # Suppresses some extra terminal outputs, outputs will be handled by select_speaker_auto_verbose
)

# Add the message transforms, if any, to the speaker selection agent
if self._speaker_selection_transforms is not None:
self._speaker_selection_transforms.add_to_agent(speaker_selection_agent)

# Run the speaker selection chat
result = checking_agent.initiate_chat(
speaker_selection_agent,
Expand Down Expand Up @@ -720,6 +737,10 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
human_input_mode="NEVER", # Suppresses some extra terminal outputs, outputs will be handled by select_speaker_auto_verbose
)

# Add the message transforms, if any, to the speaker selection agent
if self._speaker_selection_transforms is not None:
self._speaker_selection_transforms.add_to_agent(speaker_selection_agent)

# Run the speaker selection chat
result = await checking_agent.a_initiate_chat(
speaker_selection_agent,
Expand Down
42 changes: 42 additions & 0 deletions test/agentchat/test_groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import autogen
from autogen import Agent, AssistantAgent, GroupChat, GroupChatManager
from autogen.agentchat.contrib.capabilities import transform_messages, transforms
from autogen.exception_utils import AgentNameConflict, UndefinedNextAgent


Expand Down Expand Up @@ -2007,6 +2008,46 @@ def test_manager_resume_messages():
return_agent, return_message = manager.resume(messages="Let's get this conversation started.")


def test_select_speaker_transform_messages():
"""Tests adding transform messages to a GroupChat for speaker selection when in 'auto' mode"""

# Test adding a TransformMessages to a group chat
test_add_transforms = transform_messages.TransformMessages(
transforms=[
transforms.MessageHistoryLimiter(max_messages=10),
transforms.MessageTokenLimiter(max_tokens=3000, max_tokens_per_message=500, min_tokens=300),
]
)

coder = AssistantAgent(name="Coder", llm_config=None)
groupchat = GroupChat(messages=[], agents=[coder], select_speaker_transform_messages=test_add_transforms)

# Ensure the transform have been added to the GroupChat
assert groupchat._speaker_selection_transforms == test_add_transforms

# Attempt to add a non MessageTransforms object, such as a list of transforms
with pytest.raises(ValueError, match="select_speaker_transform_messages must be None or MessageTransforms."):
groupchat = GroupChat(
messages=[],
agents=[coder],
select_speaker_transform_messages=List[transforms.MessageHistoryLimiter(max_messages=10)],
)

# Ensure if we don't pass any transforms in, none are on the GroupChat
groupchat_missing = GroupChat(messages=[], agents=[coder])

assert groupchat_missing._speaker_selection_transforms is None

# Ensure we can pass in None
groupchat_none = GroupChat(
messages=[],
agents=[coder],
select_speaker_transform_messages=None,
)

assert groupchat_none._speaker_selection_transforms is None


if __name__ == "__main__":
# test_func_call_groupchat()
# test_broadcast()
Expand Down Expand Up @@ -2037,4 +2078,5 @@ def test_manager_resume_messages():
# test_manager_resume_functions()
# test_manager_resume_returns()
# test_manager_resume_messages()
# test_select_speaker_transform_messages()
pass
marklysze marked this conversation as resolved.
Show resolved Hide resolved
Loading