GoogleGenAITextEmbedder
Embed strings using Google AI models.
Key Features
- Embeds text strings using Google AI models
- Supports both the Gemini Developer API and Vertex AI
- Use in query pipelines to embed user queries for semantic search
- Returns a vector embedding alongside model usage metadata
Configuration
- Drag the
GoogleGenAITextEmbeddercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Choose the API to use:
geminifor the Gemini Developer API orvertexfor Vertex AI. - For the Gemini Developer API, enter your Google API key (
GOOGLE_API_KEYorGEMINI_API_KEY). For Vertex AI, enter your GCP project ID and location. For detailed instructions, see Create Secrets. - Select an embedding model. Use the same model as the one used to embed the documents in your document store. For supported models, see the Google AI documentation.
- Choose the API to use:
- Go to the Advanced tab to configure prefix, suffix, and task type settings.
Connections
GoogleGenAITextEmbedder accepts a text string through its text input. It outputs the embedding as a list of floats (embedding) and a meta dictionary with model usage information.
Connect the Input component's query output to this component's text input. Connect the embedding output to an embedding retriever's query_embedding input.
Embedding Models in Query Pipelines and Indexes
The embedding model you use to embed documents in your indexing pipeline must be the same as the embedding model you use to embed the query in your query pipeline.
This means the embedders for your indexing and query pipelines must match. For example, if you use CohereDocumentEmbedder to embed your documents, you should use CohereTextEmbedder with the same model to embed your queries.
Source Code
To check this component's source code, open text_embedder.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
GoogleGenAITextEmbedder:
type: haystack_integrations.components.embedders.google_genai.text_embedder.GoogleGenAITextEmbedder
init_parameters:
api_key:
type: env_var
env_vars:
- GOOGLE_API_KEY
- GEMINI_API_KEY
strict: false
api: gemini
model: text-embedding-004
prefix: ''
suffix: ''
This query pipeline uses GoogleGenAITextEmbedder to embed queries for semantic search:
components:
GoogleGenAITextEmbedder:
type: haystack_integrations.components.embedders.google_genai.text_embedder.GoogleGenAITextEmbedder
init_parameters:
api_key:
type: env_var
env_vars:
- GOOGLE_API_KEY
- GEMINI_API_KEY
strict: false
api: gemini
vertex_ai_project:
vertex_ai_location:
model: text-embedding-004
prefix: ""
suffix: ""
config:
embedding_retriever:
type: haystack_integrations.components.retrievers.opensearch.embedding_retriever.OpenSearchEmbeddingRetriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: 'google-embeddings'
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
top_k: 10
ChatPromptBuilder:
type: haystack.components.builders.chat_prompt_builder.ChatPromptBuilder
init_parameters:
template:
- role: system
content: "You are a helpful assistant answering questions based on the provided documents."
- role: user
content: "Documents:\n{% for doc in documents %}\n{{ doc.content }}\n{% endfor %}\n\nQuestion: {{ query }}"
OpenAIChatGenerator:
type: haystack.components.generators.chat.openai.OpenAIChatGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
OutputAdapter:
type: haystack.components.converters.output_adapter.OutputAdapter
init_parameters:
template: '{{ replies[0] }}'
output_type: List[str]
answer_builder:
type: deepset_cloud_custom_nodes.augmenters.deepset_answer_builder.DeepsetAnswerBuilder
init_parameters:
reference_pattern: acm
connections:
- sender: GoogleGenAITextEmbedder.embedding
receiver: embedding_retriever.query_embedding
- sender: embedding_retriever.documents
receiver: ChatPromptBuilder.documents
- sender: ChatPromptBuilder.prompt
receiver: OpenAIChatGenerator.messages
- sender: OpenAIChatGenerator.replies
receiver: OutputAdapter.replies
- sender: OutputAdapter.output
receiver: answer_builder.replies
- sender: embedding_retriever.documents
receiver: answer_builder.documents
inputs:
query:
- GoogleGenAITextEmbedder.text
- ChatPromptBuilder.query
- answer_builder.query
filters:
- embedding_retriever.filters
outputs:
documents: embedding_retriever.documents
answers: answer_builder.answers
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
text | str | Text to embed. |
Outputs
| Parameter | Type | Description |
|---|---|---|
embedding | List[float] | The embedding of the input text. |
meta | Dict[str, Any] | Information about the usage of the model. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | Secret | Secret.from_env_var(['GOOGLE_API_KEY', 'GEMINI_API_KEY'], strict=False) | Google API key. Not needed if using Vertex AI with Application Default Credentials. |
api | Literal['gemini', 'vertex'] | gemini | Which API to use. Either "gemini" for the Gemini Developer API or "vertex" for Vertex AI. |
vertex_ai_project | Optional[str] | None | Google Cloud project ID for Vertex AI. Required when using Vertex AI with Application Default Credentials. |
vertex_ai_location | Optional[str] | None | Google Cloud location for Vertex AI (for example, "us-central1", "europe-west1"). Required when using Vertex AI with Application Default Credentials. |
model | str | text-embedding-004 | The name of the model to use for calculating embeddings. |
prefix | str | "" | A string to add at the beginning of each text to embed. |
suffix | str | "" | A string to add at the end of each text to embed. |
config | Optional[Dict[str, Any]] | None | A dictionary to configure embedding content configuration. Defaults to {"task_type": "SEMANTIC_SIMILARITY"}. See Google AI Task types. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Description |
|---|---|---|
text | str | Text to embed. |
Related Information
Was this page helpful?