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

# Embeddings Controller

`UaiEmbeddingsController` provides a frontend API for generating text embeddings from custom backoffice elements. It follows the same controller pattern as [UaiChatController](/ai-in-umbraco/17.latest/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/17.latest/frontend/chat-controller.md) - Chat completions in the frontend
* [Embeddings API](/ai-in-umbraco/17.latest/using-the-api/embeddings.md) - Backend embeddings service
* [Types](/ai-in-umbraco/17.latest/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 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/frontend/embeddings-controller.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.
