-
Notifications
You must be signed in to change notification settings - Fork 622
UNS-481 [FEAT] Add Gemini embedding adapter for Google AI Studio #1891
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
5
commits into
main
Choose a base branch
from
feature/UNS-481-gemini-embedding-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
5 commits
Select commit
Hold shift + click to select a range
eb3c049
UNS-481 [FEAT] Add Gemini embedding adapter for Google AI Studio
jaseemjaskp 7bf78ac
UNS-481 [FIX] Address PR review: avoid dict mutation, validate blank …
jaseemjaskp f556a5b
UNS-481 [FIX] Handle None model value in validate_model to prevent ge…
jaseemjaskp 78bddca
Merge branch 'main' into feature/UNS-481-gemini-embedding-adapter
jaseemjaskp 9dcb145
UNS-481 [FIX] Catch ValueError from Gemini validate_model in embeddin…
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
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
40 changes: 40 additions & 0 deletions
40
unstract/sdk1/src/unstract/sdk1/adapters/embedding1/gemini.py
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, GeminiEmbeddingParameters | ||
| from unstract.sdk1.adapters.enums import AdapterTypes | ||
|
|
||
|
|
||
| class GeminiEmbeddingAdapter(GeminiEmbeddingParameters, BaseAdapter): | ||
| @staticmethod | ||
| def get_id() -> str: | ||
| return "gemini|5c2a36b8-0b8e-4f26-82c0-9f3b564cb066" | ||
|
|
||
| @staticmethod | ||
| def get_metadata() -> dict[str, Any]: | ||
| return { | ||
| "name": "Gemini", | ||
| "version": "1.0.0", | ||
| "adapter": GeminiEmbeddingAdapter, | ||
| "description": "Gemini embedding adapter", | ||
| "is_active": True, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def get_name() -> str: | ||
| return "Gemini" | ||
|
|
||
| @staticmethod | ||
| def get_description() -> str: | ||
| return "Gemini embedding adapter" | ||
|
|
||
| @staticmethod | ||
| def get_provider() -> str: | ||
| return "gemini" | ||
|
|
||
| @staticmethod | ||
| def get_icon() -> str: | ||
| return "/icons/adapter-icons/Gemini.png" | ||
jaseemjaskp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @staticmethod | ||
| def get_adapter_type() -> AdapterTypes: | ||
| return AdapterTypes.EMBEDDING | ||
44 changes: 44 additions & 0 deletions
44
unstract/sdk1/src/unstract/sdk1/adapters/embedding1/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,44 @@ | ||
| { | ||
| "title": "Gemini Embedding", | ||
| "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-emb-1" | ||
| }, | ||
| "model": { | ||
| "type": "string", | ||
| "title": "Model", | ||
| "default": "gemini/text-embedding-004", | ||
| "description": "Provide the name of the model. The gemini/ prefix will be added automatically if omitted. Recommended: gemini/text-embedding-004" | ||
| }, | ||
| "api_key": { | ||
| "type": "string", | ||
| "title": "API Key", | ||
| "default": "", | ||
| "format": "password" | ||
| }, | ||
| "embed_batch_size": { | ||
| "type": "number", | ||
jaseemjaskp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "minimum": 1, | ||
| "multipleOf": 1, | ||
| "title": "Embed Batch Size", | ||
| "description": "Number of texts to embed in a single batch. Leave empty to use the system default." | ||
| }, | ||
| "timeout": { | ||
| "type": "number", | ||
| "minimum": 0, | ||
| "multipleOf": 1, | ||
| "title": "Timeout", | ||
| "default": 240, | ||
| "description": "Timeout in seconds" | ||
jaseemjaskp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
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,152 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
| from unstract.sdk1.adapters.embedding1.gemini import GeminiEmbeddingAdapter | ||
| from unstract.sdk1.adapters.enums import AdapterTypes | ||
|
|
||
|
|
||
| class TestGeminiEmbeddingAdapter: | ||
| def test_adapter_registration(self) -> None: | ||
| from unstract.sdk1.adapters.embedding1 import adapters | ||
|
|
||
| gemini_ids = [k for k in adapters if "gemini" in k.lower()] | ||
| assert len(gemini_ids) == 1 | ||
|
|
||
| def test_get_id_format(self) -> None: | ||
| adapter_id = GeminiEmbeddingAdapter.get_id() | ||
| assert adapter_id.startswith("gemini|") | ||
| # Standard UUID-4 with hyphens is 36 characters | ||
| uuid_part = adapter_id.split("|")[1] | ||
| assert len(uuid_part) == 36 | ||
|
|
||
| def test_get_adapter_type(self) -> None: | ||
| assert GeminiEmbeddingAdapter.get_adapter_type() == AdapterTypes.EMBEDDING | ||
|
|
||
| def test_get_name(self) -> None: | ||
| assert GeminiEmbeddingAdapter.get_name() == "Gemini" | ||
|
|
||
| def test_get_provider(self) -> None: | ||
| assert GeminiEmbeddingAdapter.get_provider() == "gemini" | ||
|
|
||
| def test_json_schema_loads(self) -> None: | ||
| schema = json.loads(GeminiEmbeddingAdapter.get_json_schema()) | ||
| assert isinstance(schema, dict) | ||
| assert "title" in schema | ||
| assert "properties" in schema | ||
| assert schema["title"] == "Gemini Embedding" | ||
|
|
||
| def test_json_schema_required_fields(self) -> None: | ||
| schema = json.loads(GeminiEmbeddingAdapter.get_json_schema()) | ||
| assert set(schema["required"]) == {"adapter_name", "api_key", "model"} | ||
|
|
||
| def test_json_schema_no_batch_size_default(self) -> None: | ||
| schema = json.loads(GeminiEmbeddingAdapter.get_json_schema()) | ||
| assert "default" not in schema["properties"]["embed_batch_size"] | ||
|
|
||
| def test_json_schema_api_key_password_format(self) -> None: | ||
| schema = json.loads(GeminiEmbeddingAdapter.get_json_schema()) | ||
| assert schema["properties"]["api_key"]["format"] == "password" | ||
|
|
||
| def test_json_schema_model_default(self) -> None: | ||
| schema = json.loads(GeminiEmbeddingAdapter.get_json_schema()) | ||
| assert schema["properties"]["model"]["default"] == "gemini/text-embedding-004" | ||
|
|
||
| def test_validate_model_adds_prefix(self) -> None: | ||
| meta = {"model": "text-embedding-004", "api_key": "test"} | ||
| result = GeminiEmbeddingAdapter.validate_model(meta) | ||
| assert result == "gemini/text-embedding-004" | ||
|
|
||
| def test_validate_model_idempotent(self) -> None: | ||
| meta = {"model": "gemini/text-embedding-004", "api_key": "test"} | ||
| result = GeminiEmbeddingAdapter.validate_model(meta) | ||
| assert result == "gemini/text-embedding-004" | ||
|
|
||
| def test_validate_model_does_not_mutate_input(self) -> None: | ||
| meta = {"model": "text-embedding-004", "api_key": "test"} | ||
| GeminiEmbeddingAdapter.validate_model(meta) | ||
| assert meta["model"] == "text-embedding-004" | ||
|
|
||
| def test_validate_does_not_mutate_input(self) -> None: | ||
| meta = {"model": "text-embedding-004", "api_key": "test-key"} | ||
| original_model = meta["model"] | ||
| GeminiEmbeddingAdapter.validate(meta) | ||
| assert meta["model"] == original_model | ||
|
|
||
| def test_validate_model_empty_string_raises(self) -> None: | ||
| meta = {"model": "", "api_key": "test"} | ||
| with pytest.raises(ValueError, match="model.*required"): | ||
| GeminiEmbeddingAdapter.validate_model(meta) | ||
|
|
||
| def test_validate_model_whitespace_only_raises(self) -> None: | ||
| meta = {"model": " ", "api_key": "test"} | ||
| with pytest.raises(ValueError, match="model.*required"): | ||
| GeminiEmbeddingAdapter.validate_model(meta) | ||
|
|
||
| def test_validate_model_none_raises(self) -> None: | ||
| meta = {"model": None, "api_key": "test"} | ||
| with pytest.raises(ValueError, match="model.*required"): | ||
| GeminiEmbeddingAdapter.validate_model(meta) | ||
|
|
||
| def test_validate_model_missing_key_raises(self) -> None: | ||
| meta = {"api_key": "test"} | ||
| with pytest.raises(ValueError, match="model.*required"): | ||
| GeminiEmbeddingAdapter.validate_model(meta) | ||
|
|
||
| def test_validate_empty_model_raises(self) -> None: | ||
| meta = {"model": "", "api_key": "test-key"} | ||
| with pytest.raises(ValueError, match="model.*required"): | ||
| GeminiEmbeddingAdapter.validate(meta) | ||
|
|
||
| def test_validate_none_model_raises(self) -> None: | ||
| meta = {"model": None, "api_key": "test-key"} | ||
| with pytest.raises(ValueError, match="model.*required"): | ||
| GeminiEmbeddingAdapter.validate(meta) | ||
|
|
||
| def test_validate_missing_api_key_raises(self) -> None: | ||
| from pydantic import ValidationError | ||
|
|
||
| meta = {"model": "gemini/text-embedding-004"} | ||
| with pytest.raises(ValidationError): | ||
| GeminiEmbeddingAdapter.validate(meta) | ||
|
|
||
| def test_validate_calls_validate_model(self) -> None: | ||
| meta = {"model": "text-embedding-004", "api_key": "test-key"} | ||
| validated = GeminiEmbeddingAdapter.validate(meta) | ||
| assert validated["model"] == "gemini/text-embedding-004" | ||
|
|
||
| def test_validate_embed_batch_size_none_by_default(self) -> None: | ||
| meta = {"model": "gemini/text-embedding-004", "api_key": "test-key"} | ||
| validated = GeminiEmbeddingAdapter.validate(meta) | ||
| assert validated["embed_batch_size"] is None | ||
|
|
||
| def test_validate_embed_batch_size_preserved(self) -> None: | ||
| meta = { | ||
| "model": "gemini/text-embedding-004", | ||
| "api_key": "test-key", | ||
| "embed_batch_size": 50, | ||
| } | ||
| validated = GeminiEmbeddingAdapter.validate(meta) | ||
| assert validated["embed_batch_size"] == 50 | ||
|
|
||
| def test_validate_strips_extra_fields(self) -> None: | ||
| meta = { | ||
| "model": "gemini/text-embedding-004", | ||
| "api_key": "test-key", | ||
| "adapter_name": "my-adapter", | ||
| "unknown_field": "should_be_dropped", | ||
| } | ||
| validated = GeminiEmbeddingAdapter.validate(meta) | ||
| assert "adapter_name" not in validated | ||
| assert "unknown_field" not in validated | ||
|
|
||
| def test_validate_includes_base_fields(self) -> None: | ||
| meta = {"model": "gemini/text-embedding-004", "api_key": "test-key"} | ||
| validated = GeminiEmbeddingAdapter.validate(meta) | ||
| assert "timeout" in validated | ||
| assert "max_retries" in validated | ||
|
|
||
| def test_metadata(self) -> None: | ||
| metadata = GeminiEmbeddingAdapter.get_metadata() | ||
| assert metadata["name"] == "Gemini" | ||
| assert metadata["is_active"] is True | ||
| assert metadata["adapter"] is GeminiEmbeddingAdapter |
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.