| 18 | |
| 19 | |
| 20 | class GoogleEmbedderClient(ModelClient): |
| 21 | __doc__ = r"""A component wrapper for Google AI Embeddings API client. |
| 22 | |
| 23 | This client provides access to Google's embedding models through the Google AI API. |
| 24 | It supports text embeddings for various tasks including semantic similarity, |
| 25 | retrieval, and classification. |
| 26 | |
| 27 | Args: |
| 28 | api_key (Optional[str]): Google AI API key. Defaults to None. |
| 29 | If not provided, will use the GOOGLE_API_KEY environment variable. |
| 30 | env_api_key_name (str): Environment variable name for the API key. |
| 31 | Defaults to "GOOGLE_API_KEY". |
| 32 | |
| 33 | Example: |
| 34 | ```python |
| 35 | from api.google_embedder_client import GoogleEmbedderClient |
| 36 | import adalflow as adal |
| 37 | |
| 38 | client = GoogleEmbedderClient() |
| 39 | embedder = adal.Embedder( |
| 40 | model_client=client, |
| 41 | model_kwargs={ |
| 42 | "model": "gemini-embedding-001", |
| 43 | "task_type": "SEMANTIC_SIMILARITY" |
| 44 | } |
| 45 | ) |
| 46 | ``` |
| 47 | |
| 48 | References: |
| 49 | - Google AI Embeddings: https://ai.google.dev/gemini-api/docs/embeddings |
| 50 | - Available models: gemini-embedding-001 |
| 51 | """ |
| 52 | |
| 53 | def __init__( |
| 54 | self, |
| 55 | api_key: Optional[str] = None, |
| 56 | env_api_key_name: str = "GOOGLE_API_KEY", |
| 57 | ): |
| 58 | """Initialize Google AI Embeddings client. |
| 59 | |
| 60 | Args: |
| 61 | api_key: Google AI API key. If not provided, uses environment variable. |
| 62 | env_api_key_name: Name of environment variable containing API key. |
| 63 | """ |
| 64 | super().__init__() |
| 65 | self._api_key = api_key |
| 66 | self._env_api_key_name = env_api_key_name |
| 67 | self._initialize_client() |
| 68 | |
| 69 | def _initialize_client(self): |
| 70 | """Initialize the Google AI client with API key.""" |
| 71 | api_key = self._api_key or os.getenv(self._env_api_key_name) |
| 72 | if not api_key: |
| 73 | raise ValueError( |
| 74 | f"Environment variable {self._env_api_key_name} must be set" |
| 75 | ) |
| 76 | genai.configure(api_key=api_key) |
| 77 |
no outgoing calls