diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py index f60a44dbb90..a1acf8f6f8e 100644 --- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py +++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py @@ -24,6 +24,7 @@ FunctionExecutionResult, FunctionExecutionResultMessage, SystemMessage, + DeveloperMessage, UserMessage, ) from autogen_core.tools import FunctionTool, Tool @@ -238,15 +239,20 @@ def __init__( system_message: ( str | None ) = "You are a helpful AI assistant. Solve tasks using your tools. Reply with TERMINATE when the task has been completed.", + developer_message: (str | None) = None, reflect_on_tool_use: bool = False, tool_call_summary_format: str = "{result}", ): super().__init__(name=name, description=description) self._model_client = model_client - if system_message is None: + if system_message is None or developer_message is not None: self._system_messages = [] else: self._system_messages = [SystemMessage(content=system_message)] + if developer_message is None: + self._developer_messages = [] + else: + self._developer_messages = [DeveloperMessage(content=developer_message)] self._tools: List[Tool] = [] if tools is not None: if model_client.model_info["function_calling"] is False: @@ -326,7 +332,10 @@ async def on_messages_stream( inner_messages: List[AgentEvent | ChatMessage] = [] # Generate an inference result based on the current model context. - llm_messages = self._system_messages + await self._model_context.get_messages() + if len(self._developer_messages) > 0: + llm_messages = self._developer_messages + await self._model_context.get_messages() + else: + llm_messages = self._system_messages + await self._model_context.get_messages() result = await self._model_client.create( llm_messages, tools=self._tools + self._handoff_tools, cancellation_token=cancellation_token ) @@ -379,7 +388,10 @@ async def on_messages_stream( if self._reflect_on_tool_use: # Generate another inference result based on the tool call and result. - llm_messages = self._system_messages + await self._model_context.get_messages() + if len(self._developer_messages) > 0: + llm_messages = self._developer_messages + await self._model_context.get_messages() + else: + llm_messages = self._system_messages + await self._model_context.get_messages() result = await self._model_client.create(llm_messages, cancellation_token=cancellation_token) assert isinstance(result.content, str) # Add the response to the model context. diff --git a/python/packages/autogen-core/src/autogen_core/models/__init__.py b/python/packages/autogen-core/src/autogen_core/models/__init__.py index c9fa23ffa11..fbdfb2f22a4 100644 --- a/python/packages/autogen-core/src/autogen_core/models/__init__.py +++ b/python/packages/autogen-core/src/autogen_core/models/__init__.py @@ -9,6 +9,7 @@ LLMMessage, RequestUsage, SystemMessage, + DeveloperMessage, TopLogprob, UserMessage, ) @@ -17,6 +18,7 @@ "ModelCapabilities", "ChatCompletionClient", "SystemMessage", + "DeveloperMessage", "UserMessage", "AssistantMessage", "FunctionExecutionResult", diff --git a/python/packages/autogen-core/src/autogen_core/models/_types.py b/python/packages/autogen-core/src/autogen_core/models/_types.py index fb118562e4d..9f8b189017d 100644 --- a/python/packages/autogen-core/src/autogen_core/models/_types.py +++ b/python/packages/autogen-core/src/autogen_core/models/_types.py @@ -12,6 +12,11 @@ class SystemMessage(BaseModel): type: Literal["SystemMessage"] = "SystemMessage" +class DeveloperMessage(BaseModel): + content: str + type: Literal["DeveloperMessage"] = "DeveloperMessage" + + class UserMessage(BaseModel): content: Union[str, List[Union[str, Image]]] @@ -42,7 +47,8 @@ class FunctionExecutionResultMessage(BaseModel): LLMMessage = Annotated[ - Union[SystemMessage, UserMessage, AssistantMessage, FunctionExecutionResultMessage], Field(discriminator="type") + Union[SystemMessage, DeveloperMessage, UserMessage, AssistantMessage, FunctionExecutionResultMessage], + Field(discriminator="type"), ] diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py index 0a811dacce8..3f96950459c 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py @@ -43,6 +43,7 @@ ModelInfo, RequestUsage, SystemMessage, + DeveloperMessage, TopLogprob, UserMessage, ) @@ -58,6 +59,7 @@ ChatCompletionMessageToolCallParam, ChatCompletionRole, ChatCompletionSystemMessageParam, + ChatCompletionDeveloperMessageParam, ChatCompletionToolMessageParam, ChatCompletionToolParam, ChatCompletionUserMessageParam, @@ -172,6 +174,13 @@ def system_message_to_oai(message: SystemMessage) -> ChatCompletionSystemMessage ) +def developer_message_to_oai(message: DeveloperMessage) -> ChatCompletionDeveloperMessageParam: + return ChatCompletionDeveloperMessageParam( + content=message.content, + role="developer", + ) + + def func_call_to_oai(message: FunctionCall) -> ChatCompletionMessageToolCallParam: return ChatCompletionMessageToolCallParam( id=message.id, @@ -212,6 +221,8 @@ def assistant_message_to_oai( def to_oai_type(message: LLMMessage) -> Sequence[ChatCompletionMessageParam]: if isinstance(message, SystemMessage): return [system_message_to_oai(message)] + elif isinstance(message, DeveloperMessage): + return [developer_message_to_oai(message)] elif isinstance(message, UserMessage): return [user_message_to_oai(message)] elif isinstance(message, AssistantMessage):