-
Notifications
You must be signed in to change notification settings - Fork 621
UNS-480 [FEAT] Add Gemini LLM adapter for Google AI Studio #1890
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
Open
jaseemjaskp
wants to merge
9
commits into
main
Choose a base branch
from
feature/UNS-480-gemini-llm-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
47a4563
UNS-480 [FEAT] Add Gemini LLM adapter for Google AI Studio
jaseemjaskp 862e0ae
UNS-480 [FEAT] Add defaults for max_tokens and max_retries in Gemini …
jaseemjaskp 3e7e715
UNS-480 [FIX] Address PR review: avoid dict mutation, validate blank …
jaseemjaskp f36afbe
Merge branch 'main' into feature/UNS-480-gemini-llm-adapter
jaseemjaskp f5fdb0d
UNS-482 [FEAT] Add Gemini thinking mode support with tests
jaseemjaskp 1b34aad
UNS-482 [FIX] Use pytest.approx for temperature float comparison
jaseemjaskp 8a4fa49
UNS-482 [FIX] Validate budget_tokens when Gemini thinking mode is ena…
jaseemjaskp 54b0ad3
UNS-480 [FIX] Add gemini-2.5-flash to model description in JSON schema
jaseemjaskp 1f35647
UNS-480 [FIX] Clean up Gemini JSON schema descriptions per PR review
jaseemjaskp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from typing import Any | ||
|
|
||
| from unstract.sdk1.adapters.base1 import BaseAdapter, GeminiLLMParameters | ||
| from unstract.sdk1.adapters.enums import AdapterTypes | ||
|
|
||
|
|
||
| class GeminiLLMAdapter(GeminiLLMParameters, BaseAdapter): | ||
| @staticmethod | ||
| def get_id() -> str: | ||
| return "gemini|085f6c03-b57e-4594-85bb-40e2616c2736" | ||
|
|
||
| @staticmethod | ||
| def get_metadata() -> dict[str, Any]: | ||
| return { | ||
| "name": "Gemini", | ||
| "version": "1.0.0", | ||
| "adapter": GeminiLLMAdapter, | ||
| "description": "Google Gemini LLM adapter via Google AI Studio", | ||
| "is_active": True, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def get_name() -> str: | ||
| return "Gemini" | ||
|
|
||
| @staticmethod | ||
jaseemjaskp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def get_description() -> str: | ||
| return "Google Gemini LLM adapter via Google AI Studio" | ||
|
|
||
| @staticmethod | ||
| def get_provider() -> str: | ||
| return "gemini" | ||
|
|
||
| @staticmethod | ||
| def get_icon() -> str: | ||
| return "/icons/adapter-icons/Gemini.png" | ||
|
|
||
| @staticmethod | ||
| def get_adapter_type() -> AdapterTypes: | ||
| return AdapterTypes.LLM | ||
86 changes: 86 additions & 0 deletions
86
unstract/sdk1/src/unstract/sdk1/adapters/llm1/static/gemini.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| { | ||
| "title": "Gemini LLM", | ||
| "type": "object", | ||
| "required": ["adapter_name", "api_key", "model"], | ||
| "properties": { | ||
| "adapter_name": { | ||
| "type": "string", | ||
| "title": "Name", | ||
| "default": "", | ||
| "description": "Provide a unique name for this adapter instance. Example: gemini-group-1" | ||
| }, | ||
| "api_key": { | ||
| "type": "string", | ||
| "title": "API Key", | ||
| "default": "", | ||
| "description": "Google AI Studio API key", | ||
| "format": "password" | ||
| }, | ||
| "model": { | ||
| "type": "string", | ||
| "title": "Model", | ||
| "default": "gemini-2.0-flash", | ||
| "description": "Supported: gemini-2.0-flash, gemini-2.5-pro, gemini-2.5-flash, gemini-1.5-pro, gemini-1.5-flash. The gemini/ prefix will be added automatically if omitted." | ||
| }, | ||
greptile-apps[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "temperature": { | ||
| "type": "number", | ||
| "minimum": 0, | ||
| "maximum": 2, | ||
| "title": "Temperature", | ||
| "default": 0.1, | ||
| "description": "Sampling temperature between 0 and 2" | ||
| }, | ||
| "max_tokens": { | ||
| "type": "number", | ||
jaseemjaskp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "minimum": 0, | ||
| "multipleOf": 1, | ||
| "default": 8192, | ||
| "title": "Maximum Output Tokens", | ||
| "description": "Maximum number of output tokens to limit LLM replies, the maximum possible differs from model to model." | ||
| }, | ||
| "timeout": { | ||
| "type": "number", | ||
jaseemjaskp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "minimum": 0, | ||
| "multipleOf": 1, | ||
| "title": "Timeout", | ||
| "default": 600, | ||
| "description": "Timeout in seconds" | ||
| }, | ||
| "max_retries": { | ||
| "type": "number", | ||
| "minimum": 0, | ||
| "multipleOf": 1, | ||
| "default": 3, | ||
| "title": "Max Retries", | ||
| "description": "Maximum number of retries" | ||
| }, | ||
| "enable_thinking": { | ||
| "type": "boolean", | ||
| "title": "Enable Thinking Mode", | ||
| "default": false, | ||
| "description": "Enable extended thinking for supported models. Thinking mode is only supported on: gemini-2.5-pro, gemini-2.5-flash. When enabled, temperature is forced to 1." | ||
| } | ||
| }, | ||
| "allOf": [ | ||
| { | ||
| "if": { | ||
| "properties": { | ||
| "enable_thinking": { "const": true } | ||
| }, | ||
| "required": ["enable_thinking"] | ||
| }, | ||
| "then": { | ||
| "required": ["budget_tokens"], | ||
| "properties": { | ||
| "budget_tokens": { | ||
| "type": "integer", | ||
| "minimum": 1024, | ||
| "default": 1024, | ||
| "title": "Budget Tokens", | ||
| "description": "Number of tokens allocated for the thinking process. Minimum: 1024." | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| """Unit tests for the Gemini LLM adapter (UNS-480 / UNS-482).""" | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from unstract.sdk1.adapters.base1 import GeminiLLMParameters | ||
| from unstract.sdk1.adapters.llm1.gemini import GeminiLLMAdapter | ||
|
|
||
| BASE_METADATA = {"api_key": "test-key", "model": "gemini-2.5-flash"} | ||
|
|
||
|
|
||
| # ── validate_model ─────────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_validate_model_prefixes_when_missing() -> None: | ||
| assert ( | ||
| GeminiLLMParameters.validate_model({"model": "gemini-2.5-flash"}) | ||
| == "gemini/gemini-2.5-flash" | ||
| ) | ||
|
|
||
|
|
||
| def test_validate_model_does_not_double_prefix() -> None: | ||
| assert ( | ||
| GeminiLLMParameters.validate_model({"model": "gemini/gemini-2.5-pro"}) | ||
| == "gemini/gemini-2.5-pro" | ||
| ) | ||
|
|
||
|
|
||
| def test_validate_model_blank_raises() -> None: | ||
| with pytest.raises(ValueError, match="model is required"): | ||
| GeminiLLMParameters.validate_model({"model": " "}) | ||
|
|
||
|
|
||
| # ── validate: thinking disabled ────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_validate_thinking_disabled_by_default() -> None: | ||
| result = GeminiLLMParameters.validate({**BASE_METADATA, "temperature": 0.3}) | ||
| assert result["model"] == "gemini/gemini-2.5-flash" | ||
| assert "thinking" not in result | ||
| assert result["temperature"] == pytest.approx(0.3) | ||
|
|
||
|
|
||
| def test_validate_excludes_control_fields_from_model() -> None: | ||
| result = GeminiLLMParameters.validate(BASE_METADATA.copy()) | ||
| assert "enable_thinking" not in result | ||
| assert "budget_tokens" not in result | ||
|
|
||
|
|
||
| # ── validate: thinking enabled ─────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_validate_thinking_enabled_with_budget() -> None: | ||
| result = GeminiLLMParameters.validate( | ||
| {**BASE_METADATA, "enable_thinking": True, "budget_tokens": 2048} | ||
| ) | ||
| assert result["thinking"] == {"type": "enabled", "budget_tokens": 2048} | ||
| assert result["temperature"] == 1 | ||
|
|
||
|
|
||
| def test_validate_thinking_overrides_user_temperature() -> None: | ||
| result = GeminiLLMParameters.validate( | ||
| { | ||
| **BASE_METADATA, | ||
| "temperature": 0.7, | ||
| "enable_thinking": True, | ||
| "budget_tokens": 1024, | ||
| } | ||
| ) | ||
| assert result["temperature"] == 1 | ||
|
|
||
|
|
||
| def test_validate_thinking_enabled_without_budget_raises() -> None: | ||
| with pytest.raises(ValueError, match="budget_tokens is required"): | ||
| GeminiLLMParameters.validate({**BASE_METADATA, "enable_thinking": True}) | ||
|
|
||
|
|
||
| def test_validate_thinking_budget_tokens_invalid_type_raises() -> None: | ||
| with pytest.raises(ValueError, match="budget_tokens must be an integer >= 1024"): | ||
| GeminiLLMParameters.validate( | ||
| {**BASE_METADATA, "enable_thinking": True, "budget_tokens": "hello"} | ||
| ) | ||
|
|
||
|
|
||
| def test_validate_thinking_budget_tokens_too_small_raises() -> None: | ||
| with pytest.raises(ValueError, match="budget_tokens must be an integer >= 1024"): | ||
| GeminiLLMParameters.validate( | ||
| {**BASE_METADATA, "enable_thinking": True, "budget_tokens": 512} | ||
| ) | ||
|
|
||
|
|
||
| def test_validate_preserves_existing_thinking_config() -> None: | ||
| existing = {"type": "enabled", "budget_tokens": 4096} | ||
| result = GeminiLLMParameters.validate({**BASE_METADATA, "thinking": existing}) | ||
| assert result["thinking"] == existing | ||
| assert result["temperature"] == 1 | ||
|
|
||
|
|
||
| def test_validate_does_not_mutate_input() -> None: | ||
| metadata = {**BASE_METADATA, "enable_thinking": True, "budget_tokens": 2048} | ||
| snapshot = metadata.copy() | ||
| GeminiLLMParameters.validate(metadata) | ||
| assert metadata == snapshot | ||
|
|
||
|
|
||
| # ── Pydantic field surface ─────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_thinking_controls_not_pydantic_fields() -> None: | ||
| fields = GeminiLLMParameters.model_fields | ||
| assert "enable_thinking" not in fields | ||
| assert "budget_tokens" not in fields | ||
| assert "thinking" not in fields | ||
| assert "api_key" in fields | ||
|
|
||
|
|
||
| def test_api_key_is_required() -> None: | ||
| from pydantic import ValidationError | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| GeminiLLMParameters(model="gemini/gemini-2.5-flash") | ||
|
|
||
|
|
||
| # ── Adapter identity ───────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_adapter_identity() -> None: | ||
| assert GeminiLLMAdapter.get_name() == "Gemini" | ||
| assert GeminiLLMAdapter.get_provider() == "gemini" | ||
| assert GeminiLLMAdapter.get_id().startswith("gemini|") | ||
| metadata = GeminiLLMAdapter.get_metadata() | ||
| assert metadata["is_active"] is True | ||
| assert metadata["name"] == "Gemini" | ||
|
|
||
|
|
||
| # ── JSON schema ────────────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def gemini_schema() -> dict: | ||
| schema_path = ( | ||
| Path(__file__).parent.parent | ||
| / "src/unstract/sdk1/adapters/llm1/static/gemini.json" | ||
| ) | ||
| return json.loads(schema_path.read_text()) | ||
|
|
||
|
|
||
| def test_schema_required_fields(gemini_schema: dict) -> None: | ||
| assert set(gemini_schema["required"]) >= {"adapter_name", "api_key", "model"} | ||
|
|
||
|
|
||
| def test_schema_enable_thinking_default_false(gemini_schema: dict) -> None: | ||
| assert gemini_schema["properties"]["enable_thinking"]["default"] is False | ||
|
|
||
|
|
||
| def test_schema_budget_tokens_conditional(gemini_schema: dict) -> None: | ||
| all_of = gemini_schema["allOf"] | ||
| assert len(all_of) == 1 | ||
| conditional = all_of[0] | ||
| assert conditional["if"]["properties"]["enable_thinking"]["const"] is True | ||
| then_block = conditional["then"] | ||
| assert "budget_tokens" in then_block["required"] | ||
| budget = then_block["properties"]["budget_tokens"] | ||
| assert budget["minimum"] == 1024 | ||
| assert budget["default"] == 1024 | ||
| assert "maximum" not in budget |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.