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

Pass system message as system_instruction to Gemini #1011. #1090

Merged
merged 11 commits into from
Feb 26, 2025
12 changes: 1 addition & 11 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -1401,16 +1401,6 @@
"is_secret": false
}
],
"website/docs/user-guide/models/google-gemini.mdx": [
{
"type": "Secret Keyword",
"filename": "website/docs/user-guide/models/google-gemini.mdx",
"hashed_secret": "026eec59a93703a5c95624645476e0d2a97cc84a",
"is_verified": false,
"line_number": 54,
"is_secret": false
}
],
"website/docs/user-guide/models/groq.mdx": [
{
"type": "Secret Keyword",
Expand Down Expand Up @@ -1578,5 +1568,5 @@
}
]
},
"generated_at": "2025-02-25T12:38:52Z"
"generated_at": "2025-02-26T21:26:48Z"
}
35 changes: 32 additions & 3 deletions autogen/oai/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def create(self, params: dict) -> ChatCompletion:
messages = params.get("messages", [])
stream = params.get("stream", False)
n_response = params.get("n", 1)
system_instruction = params.get("system_instruction")
system_instruction = self._extract_system_instruction(messages)
response_validation = params.get("response_validation", True)
tools = self._tools_to_gemini_tools(params["tools"]) if "tools" in params else None

Expand Down Expand Up @@ -387,6 +387,21 @@ def create(self, params: dict) -> ChatCompletion:

return response_oai

def _extract_system_instruction(self, messages: list[dict]) -> str | None:
"""Extract system instruction if provided."""
if messages is None or len(messages) == 0 or messages[0].get("role") != "system":
return None

message = messages.pop(0)
content = message["content"]

# Multi-model uses a list of dictionaries as content with text for the system message
# Otherwise normal agents will have strings as content
content = content[0].get("text", "").strip() if isinstance(content, list) else content.strip()

content = content if len(content) > 0 else None
return content

def _oai_content_to_gemini_content(self, message: dict[str, Any]) -> tuple[list[Any], str]:
"""Convert AG2 content to Gemini parts, catering for text and tool calls"""
rst = []
Expand Down Expand Up @@ -816,7 +831,13 @@ def total_cost_k(cost_per_k_input: float, cost_per_k_output: float):
# Vertex AI pricing - based on Text input
# https://cloud.google.com/vertex-ai/generative-ai/pricing#vertex-ai-pricing

if "gemini-1.5-flash" in model_name:
if "gemini-2.0-flash-lite" in model_name:
return total_cost_mil(0.075, 0.3)

elif "gemini-2.0-flash" in model_name:
return total_cost_mil(0.15, 0.6)

elif "gemini-1.5-flash" in model_name:
if up_to_128k:
return total_cost_k(0.00001875, 0.000075)
else:
Expand All @@ -841,7 +862,15 @@ def total_cost_k(cost_per_k_input: float, cost_per_k_output: float):
else:
# Non-Vertex AI pricing

if "gemini-1.5-flash-8b" in model_name:
if "gemini-2.0-flash-lite" in model_name:
# https://ai.google.dev/gemini-api/docs/pricing#gemini-2.0-flash-lite
return total_cost_mil(0.075, 0.3)

elif "gemini-2.0-flash" in model_name:
# https://ai.google.dev/gemini-api/docs/pricing#gemini-2.0-flash
return total_cost_mil(0.1, 0.4)

elif "gemini-1.5-flash-8b" in model_name:
# https://ai.google.dev/pricing#1_5flash-8B
if up_to_128k:
return total_cost_mil(0.0375, 0.15)
Expand Down
27 changes: 27 additions & 0 deletions test/oai/test_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ def test_vertexai_initialization(self):
assert vertexai_global_config.project == "fake-project-id", "Incorrect VertexAI project initialization"
assert vertexai_global_config.credentials == mock_credentials, "Incorrect VertexAI credentials initialization"

def test_extract_system_instruction(self, gemini_client):
# Test: valid system instruction
messages = [{"role": "system", "content": "You are my personal assistant."}]
assert gemini_client._extract_system_instruction(messages) == "You are my personal assistant."

# Test: empty system instruction
messages = [{"role": "system", "content": " "}]
assert gemini_client._extract_system_instruction(messages) is None

# Test: the first message is not a system instruction
messages = [
{"role": "user", "content": "Hello!"},
{"role": "system", "content": "You are my personal assistant."},
]
assert gemini_client._extract_system_instruction(messages) is None

# Test: empty message list
assert gemini_client._extract_system_instruction([]) is None

# Test: None input
assert gemini_client._extract_system_instruction(None) is None

# Test: system message without "content" key
messages = [{"role": "system"}]
with pytest.raises(KeyError):
gemini_client._extract_system_instruction(messages)

def test_gemini_message_handling(self, gemini_client):
messages = [
{"role": "system", "content": "You are my personal assistant."},
Expand Down
Loading
Loading