> 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/using-the-api/using-the-api.md). Agents can also search and read this documentation through its [MCP server](https://docs.umbraco.com/~gitbook/mcp).

# Overview

Learn how to use Umbraco.AI services in your code for chat, embeddings, and more.

Umbraco.AI provides a service layer that wraps Microsoft.Extensions.AI types with Umbraco-specific features like profiles and connections. This section covers how to use these services in your code.

## Primary Services

| Service               | Purpose                                |
| --------------------- | -------------------------------------- |
| `IAIChatService`      | Chat completions and conversational AI |
| `IAIEmbeddingService` | Generate vector embeddings for text    |

## Dependency Injection (DI)

All services are registered automatically and can be injected into your controllers, services, and components:

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

```csharp
public class MyController : UmbracoApiController
{
    private readonly IAIChatService _chatService;
    private readonly IAIEmbeddingService _embeddingService;

    public MyController(
        IAIChatService chatService,
        IAIEmbeddingService embeddingService)
    {
        _chatService = chatService;
        _embeddingService = embeddingService;
    }
}
```

{% endcode %}

## Using M.E.AI Types

Umbraco.AI uses standard Microsoft.Extensions.AI (M.E.AI) types:

| Type                 | Namespace                 | Purpose                                  |
| -------------------- | ------------------------- | ---------------------------------------- |
| `ChatMessage`        | `Microsoft.Extensions.AI` | A message in a conversation              |
| `ChatRole`           | `Microsoft.Extensions.AI` | User, Assistant, System, Tool            |
| `ChatResponse`       | `Microsoft.Extensions.AI` | Complete response from chat              |
| `ChatResponseUpdate` | `Microsoft.Extensions.AI` | Streaming response chunk                 |
| `ChatOptions`        | `Microsoft.Extensions.AI` | Request options (temperature, and so on) |

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

```csharp
using Microsoft.Extensions.AI;

var messages = new List<ChatMessage>
{
    new(ChatRole.System, "You are a helpful assistant."),
    new(ChatRole.User, "Hello!")
};
```

{% endcode %}

## Quick Examples

### Chat Completion

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

```csharp
var messages = new List<ChatMessage>
{
    new(ChatRole.User, "What is Umbraco CMS?")
};

var response = await _chatService.GetChatResponseAsync(
    chat => chat.WithAlias("content-chat"),
    messages);
var answer = response.Message.Text;
```

{% endcode %}

### Streaming Chat

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

```csharp
var messages = new List<ChatMessage>
{
    new(ChatRole.User, "Write a short poem about coding.")
};

await foreach (var update in _chatService.StreamChatResponseAsync(
    chat => chat.WithAlias("poem-writer"),
    messages))
{
    Console.Write(update.Text);
}
```

{% endcode %}

### Generate Embedding

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

```csharp
var embedding = await _embeddingService.GenerateEmbeddingAsync(
    emb => emb.WithAlias("content-embedding"),
    "Umbraco is a content management system");

float[] vector = embedding.Vector.ToArray();
```

{% endcode %}

## In This Section

{% content-ref url="/pages/xxcnQXqfTTygEw9wi7uo" %}
[Chat](https://docs.umbraco.com/ai-in-umbraco/using-the-api/chat.md)
{% endcontent-ref %}

{% content-ref url="/pages/hvhAhZqLR6USuDvT8aSC" %}
[Embeddings](https://docs.umbraco.com/ai-in-umbraco/using-the-api/embeddings.md)
{% endcontent-ref %}

{% content-ref url="/pages/bSd1nIgcUSU7TLlaOd1u" %}
[Tools](https://docs.umbraco.com/ai-in-umbraco/using-the-api/tools.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/using-the-api/using-the-api.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.
