> 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/17.latest/using-the-api/embeddings/generating-embeddings.md).

# Generating Embeddings

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/qu8GYvb7Icuf3oO4oavu" %}
[Batch Embeddings](/ai-in-umbraco/17.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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
