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: 11 additions & 1 deletion autogen/oai/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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 @@ -375,6 +375,16 @@ 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]["role"] != "system":
return None

message = messages.pop(0)
content = message["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
Loading