LlamaIndex Google Generative AI Embeddings
This library provides the integration for Google Generative AI embedding models within LlamaIndex. It allows users to leverage Google's powerful text embedding capabilities, such as 'embedding-001' or 'text-embedding-004', for various RAG applications and data indexing tasks. The current version is 0.5.0, and it follows the LlamaIndex release cadence, often updating with new core versions or Google GenAI SDK updates.
Common errors
-
ModuleNotFoundError: No module named 'llama_index.embeddings.google'
cause The `llama-index-embeddings-google-genai` package is not installed, or the LlamaIndex version is very old and doesn't support the modularized import path.fixRun `pip install llama-index-embeddings-google-genai`. If using an older `llama-index` core, consider upgrading or checking the documentation for your specific version. -
ValueError: API key not found. Please set the GOOGLE_API_KEY environment variable.
cause The `GOOGLE_API_KEY` environment variable is not set or accessible by the application.fixSet the environment variable: `export GOOGLE_API_KEY='your_api_key'` in your shell, or pass `api_key='your_api_key'` directly to the `GoogleGenerativeAIEmbedding` constructor. -
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument. Target: embeddings.google.ai
cause This error typically indicates an issue with the request parameters, such as an invalid or expired API key, or an incorrect `model_name`.fixVerify your `GOOGLE_API_KEY` is correct and active. Double-check the `model_name` you are using (e.g., ensure it's `models/embedding-001` and not just `embedding-001`). Also check Google Cloud project quotas.
Warnings
- breaking LlamaIndex v0.10+ modularized core components and integrations. The import path for `GoogleGenerativeAIEmbedding` changed from a potentially monolithic `llama_index.embeddings` (if it existed there) to `llama_index.embeddings.google`.
- gotcha Google API key authentication is typically handled via the `GOOGLE_API_KEY` environment variable. If it's not set, or is invalid, you will encounter authentication errors.
- gotcha When specifying a model name, it's best practice to use the full resource name (e.g., 'models/embedding-001') as expected by the Google Generative AI API, though the library might sometimes infer it.
Install
-
pip install llama-index-embeddings-google-genai
Imports
- GoogleGenerativeAIEmbedding
from llama_index.embeddings import GoogleGenerativeAIEmbedding
from llama_index.embeddings.google import GoogleGenerativeAIEmbedding
- Settings
from llama_index.legacy.service_context import ServiceContext
from llama_index.core import Settings
Quickstart
import os
from llama_index.embeddings.google import GoogleGenerativeAIEmbedding
from llama_index.core import Settings
# Ensure GOOGLE_API_KEY environment variable is set
# For testing purposes, you can uncomment and set it:
# os.environ["GOOGLE_API_KEY"] = os.environ.get("GOOGLE_API_KEY", "YOUR_GOOGLE_API_KEY")
try:
# Initialize the embedding model
# Optionally specify model_name, e.g., model_name="models/embedding-001"
embed_model = GoogleGenerativeAIEmbedding(api_key=os.environ.get('GOOGLE_API_KEY'))
# Set as the default embedding model for LlamaIndex operations
Settings.embed_model = embed_model
# Generate an embedding for a piece of text
text_to_embed = "The quick brown fox jumps over the lazy dog."
embedding = embed_model.get_text_embedding(text_to_embed)
print(f"Successfully generated embedding.")
print(f"Embedding dimension: {len(embedding)}")
# print(f"First 5 values of embedding: {embedding[:5]}") # Uncomment to see values
except ValueError as e:
print(f"Error initializing or using model: {e}")
if "API key not found" in str(e) or "authentication" in str(e):
print("Please ensure GOOGLE_API_KEY environment variable is set and valid.")
except Exception as e:
print(f"An unexpected error occurred: {e}")