Gemini.Types.Request.EmbedContentRequest (GeminiEx v0.8.4)
View SourceRequest structure for embedding content using Gemini embedding models.
Represents a request to generate text embeddings from input content. Embeddings are numerical representations of text that enable use cases such as clustering, similarity measurement, and information retrieval.
API Differences
This module handles the differences between Gemini API and Vertex AI embedding models:
- Gemini API (
gemini-embedding-001): UsestaskTypeparameter Vertex AI (
embeddinggemma): Uses prompt prefixes like "task: search result | query: "
The new/2 function automatically detects the model type and formats accordingly.
Fields
model: The embedding model to use (e.g., "gemini-embedding-001" or "embeddinggemma")content: The content to embed (only text parts will be processed)task_type: Optional task type for optimized embeddingstitle: Optional title for retrieval documentsoutput_dimensionality: Optional dimension reduction for embeddings
Examples
# Simple embedding request (auto-detects model from auth)
EmbedContentRequest.new("What is the meaning of life?")
# With task type - automatically formats for model type
EmbedContentRequest.new("Document text here",
task_type: :retrieval_document,
title: "Important Document",
output_dimensionality: 256
)
# For EmbeddingGemma, the text becomes:
# "title: Important Document | text: Document text here"
Summary
Functions
Format text with the appropriate prompt prefix for EmbeddingGemma models.
Creates a new embedding request from text content.
Creates an embedding request with explicit API type specification.
Converts the request struct to API-compatible map format.
Types
@type t() :: %Gemini.Types.Request.EmbedContentRequest{ content: Gemini.Types.Content.t(), model: String.t(), output_dimensionality: pos_integer() | nil, task_type: task_type() | nil, title: String.t() | nil }
@type task_type() ::
:task_type_unspecified
| :retrieval_query
| :retrieval_document
| :semantic_similarity
| :classification
| :clustering
| :question_answering
| :fact_verification
| :code_retrieval_query
Functions
Format text with the appropriate prompt prefix for EmbeddingGemma models.
This is exposed for cases where you need to manually format text for EmbeddingGemma without going through the full request creation.
Parameters
text: The original texttask_type: Task type atomopts: Options including:titlefor retrieval_document
Examples
format_for_embedding_gemma("My query", :retrieval_query)
#=> "task: search result | query: My query"
format_for_embedding_gemma("My document", :retrieval_document, title: "Title")
#=> "title: Title | text: My document"
Creates a new embedding request from text content.
Automatically handles model-specific formatting:
- For Gemini embedding models: Uses taskType parameter
- For EmbeddingGemma: Prepends prompt prefix to text content
Parameters
text: The text to embedopts: Optional keyword list of options:model: Model to use (default: auto-detected based on auth):task_type: Task type for optimized embeddings:title: Title for retrieval documents (required for EmbeddingGemma with :retrieval_document):output_dimensionality: Dimension reduction
Examples
# Basic usage (auto-detects model)
EmbedContentRequest.new("What is AI?")
# With task type (works with both APIs)
EmbedContentRequest.new("Document content",
task_type: :retrieval_document,
title: "AI Overview"
)
# Explicit model selection
EmbedContentRequest.new("Query text",
model: "embeddinggemma",
task_type: :retrieval_query
)
# Text becomes: "task: search result | query: Query text"
Creates an embedding request with explicit API type specification.
Use this when you need to force a specific API's embedding model regardless of the current authentication configuration.
Parameters
text: The text to embedapi_type::geminior:vertex_aiopts: Same options asnew/2
Examples
# Force Gemini API embedding model
EmbedContentRequest.new_for_api("Query", :gemini, task_type: :retrieval_query)
# Force Vertex AI embedding model
EmbedContentRequest.new_for_api("Document", :vertex_ai, task_type: :retrieval_document)
Converts the request struct to API-compatible map format.
Converts snake_case field names to camelCase as required by the Gemini API. For EmbeddingGemma models, the task type is already embedded in the text content.