> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/ai-in-umbraco/19.latest/using-the-api/embeddings/generating-embeddings.md).

# Generating Embeddings

Generate vector embeddings for individual text values.

Generate a single embedding vector for a piece of text. This is the most common operation for search indexing and similarity comparisons.

## Basic Example

{% code title="BasicEmbedding.cs" %}

```csharp
using Microsoft.Extensions.AI;
using Umbraco.AI.Core.Embeddings;

public class EmbeddingExample
{
    private readonly IAIEmbeddingService _embeddingService;

    public EmbeddingExample(IAIEmbeddingService embeddingService)
    {
        _embeddingService = embeddingService;
    }

    public async Task<float[]> GenerateEmbedding(string text)
    {
        var embedding = await _embeddingService.GenerateEmbeddingAsync(
            emb => emb.WithAlias("content-embedding"),
            text);

        // The vector is a ReadOnlyMemory<float>
        return embedding.Vector.ToArray();
    }
}
```

{% endcode %}

## Understanding the Response

The `Embedding<float>` type contains:

| Property    | Type                    | Description                            |
| ----------- | ----------------------- | -------------------------------------- |
| `Vector`    | `ReadOnlyMemory<float>` | The embedding vector                   |
| `ModelId`   | `string?`               | The model that generated the embedding |
| `CreatedAt` | `DateTimeOffset?`       | When the embedding was generated       |

{% code title="EmbeddingDetails.cs" %}

```csharp
var embedding = await _embeddingService.GenerateEmbeddingAsync(
    emb => emb.WithAlias("content-embedding"),
    text);

// Access the vector
ReadOnlyMemory<float> vector = embedding.Vector;
int dimensions = vector.Length; // e.g., 1536 for text-embedding-3-small

// Convert to array if needed
float[] array = vector.ToArray();

// Check which model was used
string? model = embedding.ModelId;
```

{% endcode %}

## Using a Specific Profile

Select a profile by alias directly on the builder. The service resolves the alias for you.

{% code title="WithProfile.cs" %}

```csharp
public class ProfiledEmbeddingService
{
    private readonly IAIEmbeddingService _embeddingService;

    public ProfiledEmbeddingService(IAIEmbeddingService embeddingService)
    {
        _embeddingService = embeddingService;
    }

    public async Task<float[]> GenerateWithProfile(string text, string profileAlias)
    {
        var embedding = await _embeddingService.GenerateEmbeddingAsync(
            emb => emb
                .WithAlias("content-embedding")
                .WithProfile(profileAlias),
            text);

        return embedding.Vector.ToArray();
    }
}
```

{% endcode %}

## Error Handling

{% code title="ErrorHandling.cs" %}

```csharp
public async Task<float[]?> SafeGenerateEmbedding(string text)
{
    try
    {
        var embedding = await _embeddingService.GenerateEmbeddingAsync(
            emb => emb.WithAlias("content-embedding"),
            text);
        return embedding.Vector.ToArray();
    }
    catch (InvalidOperationException ex) when (ex.Message.Contains("profile"))
    {
        _logger.LogError(ex, "Embedding profile not configured");
        return null;
    }
    catch (HttpRequestException ex)
    {
        _logger.LogError(ex, "Failed to reach embedding service");
        return null;
    }
}
```

{% endcode %}

## Next Steps

{% content-ref url="/pages/xVbRohCXezmiewARUfRQ" %}
[Batch Embeddings](/ai-in-umbraco/19.latest/using-the-api/embeddings/batch-embeddings.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/ai-in-umbraco/19.latest/using-the-api/embeddings/generating-embeddings.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
