> 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/frontend/embeddings-controller.md).

# Embeddings Controller

Controller for generating text embeddings from custom backoffice elements.

`UaiEmbeddingsController` provides a frontend API for generating text embeddings from custom backoffice elements. It follows the same controller pattern as [UaiChatController](/ai-in-umbraco/frontend/chat-controller.md).

## Import

{% code title="Import" %}

```typescript
import { UaiEmbeddingsController, UaiEmbeddingOptions } from "@umbraco-ai/core";
```

{% endcode %}

## Constructor

{% code title="Constructor" %}

```typescript
new UaiEmbeddingsController(host: UmbControllerHost)
```

{% endcode %}

| Parameter | Type                | Description                                           |
| --------- | ------------------- | ----------------------------------------------------- |
| `host`    | `UmbControllerHost` | The controller host (usually `this` in a Lit element) |

## Methods

### generate

Generates an embedding for a single text value.

{% code title="Signature" %}

```typescript
async generate(
    value: string,
    options?: UaiEmbeddingOptions
): Promise<{ data?: number[]; error?: unknown }>
```

{% endcode %}

| Parameter | Type                  | Description            |
| --------- | --------------------- | ---------------------- |
| `value`   | `string`              | The text to embed      |
| `options` | `UaiEmbeddingOptions` | Optional configuration |

**Returns**: Promise resolving to `{ data?, error? }` where `data` is a `number[]` vector.

### `generateMany`

Generates embeddings for multiple text values in a batch.

{% code title="Signature" %}

```typescript
async generateMany(
    values: string[],
    options?: UaiEmbeddingOptions
): Promise<{ data?: UaiEmbeddingResult; error?: unknown }>
```

{% endcode %}

| Parameter | Type                  | Description            |
| --------- | --------------------- | ---------------------- |
| `values`  | `string[]`            | The texts to embed     |
| `options` | `UaiEmbeddingOptions` | Optional configuration |

**Returns**: Promise resolving to `{ data?, error? }` where `data` contains an `embeddings` array.

## Options

{% code title="UaiEmbeddingOptions" %}

```typescript
interface UaiEmbeddingOptions {
    /** Profile ID (GUID) or alias. If omitted, uses the default embedding profile. */
    profileIdOrAlias?: string;
    /** AbortSignal for cancellation. */
    signal?: AbortSignal;
}
```

{% endcode %}

## Result Types

{% code title="UaiEmbeddingResult" %}

```typescript
interface UaiEmbeddingResult {
    embeddings: UaiEmbeddingItem[];
}

interface UaiEmbeddingItem {
    index: number;
    vector: number[];
}
```

{% endcode %}

## Example

{% code title="similarity-checker.element.ts" %}

```typescript
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { UaiEmbeddingsController } from "@umbraco-ai/core";

@customElement("similarity-checker")
export class SimilarityCheckerElement extends LitElement {
    #embeddings = new UaiEmbeddingsController(this);

    @state()
    private _similarity?: number;

    async #compare(text1: string, text2: string) {
        const { data: result } = await this.#embeddings.generateMany(
            [text1, text2],
            { profileIdOrAlias: "text-embedding" },
        );

        if (result) {
            this._similarity = cosineSimilarity(
                result.embeddings[0].vector,
                result.embeddings[1].vector,
            );
        }
    }

    render() {
        return html`
            ${this._similarity != null
                ? html`<p>Similarity: ${(this._similarity * 100).toFixed(1)}%</p>`
                : ""}
        `;
    }
}
```

{% endcode %}

## Related

* [Chat Controller](/ai-in-umbraco/frontend/chat-controller.md) - Chat completions in the frontend
* [Embeddings API](/ai-in-umbraco/using-the-api/embeddings.md) - Backend embeddings service
* [Types](/ai-in-umbraco/frontend/types.md) - All frontend type definitions


---

# 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/frontend/embeddings-controller.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.
