From 8a3cc4c878fd6e21bb656390c8978b1d484383be Mon Sep 17 00:00:00 2001 From: "chiyoung.song" Date: Mon, 21 Apr 2025 12:00:12 +0900 Subject: [PATCH] FEAT: merge_continuos_system_message_via_model_info_field --- .../src/autogen_ext/models/openai/_openai_client.py | 10 ++++++---- .../tests/models/test_openai_model_client.py | 7 +++++-- 2 files changed, 11 insertions(+), 6 deletions(-) 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 84951e40c482..ffe816e599c8 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 @@ -525,9 +525,9 @@ def _process_create_args( if self.model_info["json_output"] is False and json_output is True: raise ValueError("Model does not support JSON output.") - if create_args.get("model", "unknown").startswith("gemini-"): - # Gemini models accept only one system message(else, it will read only the last one) - # So, merge system messages into one + if not self.model_info.get("multiple_system_messages", False): + # Some models accept only one system message(or, it will read only the last one) + # So, merge system messages into one (if multiple and continuous) system_message_content = "" _messages: List[LLMMessage] = [] _first_system_message_idx = -1 @@ -540,7 +540,9 @@ def _process_create_args( elif _last_system_message_idx + 1 != idx: # That case, system message is not continuous # Merge system messages only contiues system messages - raise ValueError("Multiple and Not continuous system messages are not supported") + raise ValueError( + "Multiple and Not continuous system messages are not supported if model_info['multiple_system_messages'] is False" + ) system_message_content += message.content + "\n" _last_system_message_idx = idx else: diff --git a/python/packages/autogen-ext/tests/models/test_openai_model_client.py b/python/packages/autogen-ext/tests/models/test_openai_model_client.py index 60d5547d0302..263f57887162 100644 --- a/python/packages/autogen-ext/tests/models/test_openai_model_client.py +++ b/python/packages/autogen-ext/tests/models/test_openai_model_client.py @@ -2096,7 +2096,7 @@ async def test_muliple_system_message(model: str, openai_client: OpenAIChatCompl @pytest.mark.asyncio -async def test_system_message_merge_for_gemini_models() -> None: +async def test_system_message_merge_with_continuous_system_messages_models() -> None: """Tests that system messages are merged correctly for Gemini models.""" # Create a mock client mock_client = MagicMock() @@ -2109,6 +2109,7 @@ async def test_system_message_merge_for_gemini_models() -> None: "json_output": False, "family": "unknown", "structured_output": False, + "multiple_system_messages": False, }, ) @@ -2157,6 +2158,7 @@ async def test_system_message_merge_with_non_continuous_messages() -> None: "json_output": False, "family": "unknown", "structured_output": False, + "multiple_system_messages": False, }, ) @@ -2180,7 +2182,7 @@ async def test_system_message_merge_with_non_continuous_messages() -> None: @pytest.mark.asyncio -async def test_system_message_not_merged_for_non_gemini_models() -> None: +async def test_system_message_not_merged_for_multiple_system_messages_true() -> None: """Tests that system messages aren't modified for non-Gemini models.""" # Create a mock client mock_client = MagicMock() @@ -2193,6 +2195,7 @@ async def test_system_message_not_merged_for_non_gemini_models() -> None: "json_output": False, "family": "unknown", "structured_output": False, + "multiple_system_messages": True, }, )