embeddings - gunpal5/Google_GenerativeAI GitHub Wiki

Introduction

This page explains how to generate embeddings using the Google Generative AI C# SDK. Embeddings are numerical representations of text that capture semantic meaning. They are useful for various tasks, such as semantic similarity comparison, clustering, and information retrieval. You first initialize an embedding model and then use it to generate the embeddings.

Initializing an Embedding Model

Before you can generate embeddings, you need to initialize an embedding model. You can do this by calling the CreateEmbeddingModel(modelName) method of either the GoogleAI or VertexAI class, depending on which platform you are using. We'll use the text-embedding-004 model in these examples.

// For Google AI:
var embeddingModel = googleAI.CreateEmbeddingModel("text-embedding-004");

// For Vertex AI:
var embeddingModel = vertexAI.CreateEmbeddingModel("text-embedding-004");

Replace "text-embedding-004" with the name of the embedding model you want to use if you need a different model. Refer to the API documentation for a list of available models.

Generating Embeddings

Once you have an initialized embedding model, you can use the following methods to generate embeddings for your content:

EmbedContentAsync(Content content)

Embeds content from a Content object. This example demonstrates using the constructor to create the Content object.

// Using the constructor:
var content = new Content("This is a test embedding."); // Implicit User role

// Or with explicit role:
var contentWithRole = new Content("This is a test embedding.", Roles.User);

EmbedContentResponse response = await embeddingModel.EmbedContentAsync(content);

// Access the embedding
var embedding = response.Embedding;
//... use the embedding...

EmbedContentAsync(EmbedContentRequest request)

Embeds content from an EmbedContentRequest object. This gives you more control over the embedding process.

var request = new EmbedContentRequest { Content = new Content("This is a test embedding.") }; // Using constructor
EmbedContentResponse response = await embeddingModel.EmbedContentAsync(request);

var embedding = response.Embedding;
//... use the embedding...

EmbedContentAsync(string message)

Embeds content from a string message. This is a convenient method for simple text input.

string message = "This is a test embedding.";
EmbedContentResponse response = await embeddingModel.EmbedContentAsync(message);

var embedding = response.Embedding;
//... use the embedding...

EmbedContentAsync(IEnumerable<Part> parts)

Embeds content from a sequence of Part objects. This allows you to create more structured input for embedding generation.

var parts = new List<Part> { new Part { Text = "This is a test embedding." } };
EmbedContentResponse response = await embeddingModel.EmbedContentAsync(parts);

var embedding = response.Embedding;
//... use the embedding...

EmbedContentAsync(IEnumerable<string> messages)

Embeds content from a collection of strings. Note: This combines all strings into a single text and generates one embedding for the combined text. If you want embeddings for each individual string, use BatchEmbedContentAsync.

var messages = new List<string> { "This is a test embedding.", "Another test embedding." };
EmbedContentResponse response = await embeddingModel.EmbedContentAsync(messages);

var embedding = response.Embedding; // Embedding for the combined text
//... use the embedding...

BatchEmbedContentAsync(IEnumerable<EmbedContentRequest> requests)

Embeds a batch of content from a collection of EmbedContentRequest objects. This is the most efficient way to generate embeddings for multiple inputs and provides individual embeddings for each.

var requests = new List<EmbedContentRequest>
{
    new EmbedContentRequest { Content = new Content("This is a test embedding.") }, // Using constructor
    new EmbedContentRequest { Content = new Content("Another test embedding.") } // Using constructor
};
BatchEmbedContentsResponse response = await embeddingModel.BatchEmbedContentAsync(requests);

// Access individual embeddings from the batch response
foreach (var embedding in response.Embeddings)
{
    //... use each embedding...
}

BatchEmbedContentAsync(IEnumerable<Content> contents)

Embeds a batch of content from a collection of Content objects. This is a convenient way to generate embeddings for multiple Content objects.

var contents = new List<Content>
{
    new Content("This is a test embedding."), // Using constructor
    new Content("Another test embedding.") // Using constructor
};
BatchEmbedContentsResponse response = await embeddingModel.BatchEmbedContentAsync(contents);

// Access individual embeddings from the batch response
foreach (var embedding in response.Embeddings)
{
    //... use each embedding...
}

API Reference

⚠️ **GitHub.com Fallback** ⚠️