LlamaIndex Google Generative AI Embeddings

0.5.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `GoogleGenerativeAIEmbedding` model and set it as the default embedding model in LlamaIndex's global `Settings`. It then generates an embedding for a sample text. Ensure the `GOOGLE_API_KEY` environment variable is set for authentication with Google GenAI.

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}")

view raw JSON →