Call Google AI embedding API. Args: api_kwargs: API parameters model_type: Should be ModelType.EMBEDDER Returns: Google AI embedding response
(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED)
| 209 | max_time=5, |
| 210 | ) |
| 211 | def call(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED): |
| 212 | """Call Google AI embedding API. |
| 213 | |
| 214 | Args: |
| 215 | api_kwargs: API parameters |
| 216 | model_type: Should be ModelType.EMBEDDER |
| 217 | |
| 218 | Returns: |
| 219 | Google AI embedding response |
| 220 | """ |
| 221 | if model_type != ModelType.EMBEDDER: |
| 222 | raise ValueError(f"GoogleEmbedderClient only supports EMBEDDER model type") |
| 223 | |
| 224 | safe_log_kwargs = {k: v for k, v in api_kwargs.items() if k not in {"content", "contents"}} |
| 225 | if "content" in api_kwargs: |
| 226 | safe_log_kwargs["content_chars"] = len(str(api_kwargs.get("content", ""))) |
| 227 | if "contents" in api_kwargs: |
| 228 | try: |
| 229 | contents = api_kwargs.get("contents") |
| 230 | safe_log_kwargs["contents_count"] = len(contents) if hasattr(contents, "__len__") else None |
| 231 | except Exception: |
| 232 | safe_log_kwargs["contents_count"] = None |
| 233 | log.info("Google AI Embeddings call kwargs (sanitized): %s", safe_log_kwargs) |
| 234 | |
| 235 | try: |
| 236 | # Use embed_content for single text or batch embedding |
| 237 | if "content" in api_kwargs: |
| 238 | # Single embedding |
| 239 | response = genai.embed_content(**api_kwargs) |
| 240 | elif "contents" in api_kwargs: |
| 241 | # Batch embedding - Google AI supports batch natively |
| 242 | # Copy to avoid mutating the original dict (needed for retries) |
| 243 | kwargs = api_kwargs.copy() |
| 244 | contents = kwargs.pop("contents") |
| 245 | response = genai.embed_content(content=contents, **kwargs) |
| 246 | else: |
| 247 | raise ValueError("Either 'content' or 'contents' must be provided") |
| 248 | |
| 249 | return response |
| 250 | |
| 251 | except Exception as e: |
| 252 | log.error(f"Error calling Google AI Embeddings API: {e}") |
| 253 | raise |
| 254 | |
| 255 | async def acall(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED): |
| 256 | """Async call to Google AI embedding API. |
no outgoing calls