|
21 | 21 | from ..runtime_logging import log_new_agent, logging_enabled
|
22 | 22 | from .agent import Agent
|
23 | 23 | from .chat import ChatResult
|
| 24 | +from .contrib.capabilities import transform_messages |
24 | 25 | from .conversable_agent import ConversableAgent
|
25 | 26 |
|
26 | 27 | logger = logging.getLogger(__name__)
|
@@ -82,6 +83,8 @@ def custom_speaker_selection_func(
|
82 | 83 | of times until a single agent is returned or it exhausts the maximum attempts.
|
83 | 84 | Applies only to "auto" speaker selection method.
|
84 | 85 | Default is 2.
|
| 86 | + - select_speaker_transform_messages: (optional) the message transformations to apply to the nested select speaker agent-to-agent chat messages. |
| 87 | + Takes a TransformMessages object, defaults to None and is only utilised when the speaker selection method is "auto". |
85 | 88 | - select_speaker_auto_verbose: whether to output the select speaker responses and selections
|
86 | 89 | If set to True, the outputs from the two agents in the nested select speaker chat will be output, along with
|
87 | 90 | whether the responses were successful, or not, in selecting an agent
|
@@ -138,6 +141,7 @@ def custom_speaker_selection_func(
|
138 | 141 | The names are case-sensitive and should not be abbreviated or changed.
|
139 | 142 | The only names that are accepted are {agentlist}.
|
140 | 143 | Respond with ONLY the name of the speaker and DO NOT provide a reason."""
|
| 144 | + select_speaker_transform_messages: Optional[transform_messages.TransformMessages] = None |
141 | 145 | select_speaker_auto_verbose: Optional[bool] = False
|
142 | 146 | role_for_select_speaker_messages: Optional[str] = "system"
|
143 | 147 |
|
@@ -255,6 +259,15 @@ def __post_init__(self):
|
255 | 259 | elif self.max_retries_for_selecting_speaker < 0:
|
256 | 260 | raise ValueError("max_retries_for_selecting_speaker must be greater than or equal to zero")
|
257 | 261 |
|
| 262 | + # 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) |
| 263 | + if self.select_speaker_transform_messages is not None: |
| 264 | + if isinstance(self.select_speaker_transform_messages, transform_messages.TransformMessages): |
| 265 | + self._speaker_selection_transforms = self.select_speaker_transform_messages |
| 266 | + else: |
| 267 | + raise ValueError("select_speaker_transform_messages must be None or MessageTransforms.") |
| 268 | + else: |
| 269 | + self._speaker_selection_transforms = None |
| 270 | + |
258 | 271 | # Validate select_speaker_auto_verbose
|
259 | 272 | if self.select_speaker_auto_verbose is None or not isinstance(self.select_speaker_auto_verbose, bool):
|
260 | 273 | raise ValueError("select_speaker_auto_verbose cannot be None or non-bool")
|
@@ -661,6 +674,10 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
|
661 | 674 | else:
|
662 | 675 | start_message = messages[-1]
|
663 | 676 |
|
| 677 | + # Add the message transforms, if any, to the speaker selection agent |
| 678 | + if self._speaker_selection_transforms is not None: |
| 679 | + self._speaker_selection_transforms.add_to_agent(speaker_selection_agent) |
| 680 | + |
664 | 681 | # Run the speaker selection chat
|
665 | 682 | result = checking_agent.initiate_chat(
|
666 | 683 | speaker_selection_agent,
|
@@ -755,6 +772,10 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
|
755 | 772 | else:
|
756 | 773 | start_message = messages[-1]
|
757 | 774 |
|
| 775 | + # Add the message transforms, if any, to the speaker selection agent |
| 776 | + if self._speaker_selection_transforms is not None: |
| 777 | + self._speaker_selection_transforms.add_to_agent(speaker_selection_agent) |
| 778 | + |
758 | 779 | # Run the speaker selection chat
|
759 | 780 | result = await checking_agent.a_initiate_chat(
|
760 | 781 | speaker_selection_agent,
|
|
0 commit comments