3
3
from typing import Any , Callable , Dict , List , Mapping , Sequence
4
4
5
5
from autogen_core import Component , ComponentModel
6
- from autogen_core .models import ChatCompletionClient , SystemMessage , UserMessage
6
+ from autogen_core .models import ChatCompletionClient , ModelFamily , SystemMessage , UserMessage
7
7
from pydantic import BaseModel
8
8
from typing_extensions import Self
9
9
@@ -110,18 +110,17 @@ async def select_speaker(self, thread: List[AgentEvent | ChatMessage]) -> str:
110
110
message += " [Image]"
111
111
else :
112
112
raise ValueError (f"Unexpected message type in selector: { type (msg )} " )
113
- history_messages .append (message )
113
+ history_messages .append (
114
+ message .rstrip () + "\n \n "
115
+ ) # Create some consistency for how messages are separated in the transcript
114
116
history = "\n " .join (history_messages )
115
117
116
118
# Construct agent roles, we are using the participant topic type as the agent name.
117
- roles = "\n " .join (
118
- [
119
- f"{ topic_type } : { description } " .strip ()
120
- for topic_type , description in zip (
121
- self ._participant_topic_types , self ._participant_descriptions , strict = True
122
- )
123
- ]
124
- )
119
+ # Each agent sould appear on a single line.
120
+ roles = ""
121
+ for topic_type , description in zip (self ._participant_topic_types , self ._participant_descriptions , strict = True ):
122
+ roles += re .sub (r"\s+" , " " , f"{ topic_type } : { description } " ).strip () + "\n "
123
+ roles = roles .strip ()
125
124
126
125
# Construct agent list to be selected, skip the previous speaker if not allowed.
127
126
if self ._previous_speaker is not None and not self ._allow_repeated_speaker :
@@ -136,11 +135,20 @@ async def select_speaker(self, thread: List[AgentEvent | ChatMessage]) -> str:
136
135
roles = roles , participants = str (participants ), history = history
137
136
)
138
137
select_speaker_messages : List [SystemMessage | UserMessage ]
139
- if self ._model_client .model_info ["family" ].startswith ("gemini" ):
140
- select_speaker_messages = [UserMessage (content = select_speaker_prompt , source = "selector" )]
141
- else :
138
+ if self ._model_client .model_info ["family" ] in [
139
+ ModelFamily .GPT_4 ,
140
+ ModelFamily .GPT_4O ,
141
+ ModelFamily .GPT_35 ,
142
+ ModelFamily .O1 ,
143
+ ModelFamily .O3 ,
144
+ ]:
142
145
select_speaker_messages = [SystemMessage (content = select_speaker_prompt )]
146
+ else :
147
+ # Many other models need a UserMessage to respond to
148
+ select_speaker_messages = [UserMessage (content = select_speaker_prompt , source = "selector" )]
149
+
143
150
response = await self ._model_client .create (messages = select_speaker_messages )
151
+
144
152
assert isinstance (response .content , str )
145
153
mentions = self ._mentioned_agents (response .content , self ._participant_topic_types )
146
154
if len (mentions ) != 1 :
0 commit comments