> 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/chat.md).

# Chat

The chat API provides access to conversational AI capabilities. Use it for text generation, question answering, content creation, and more.

## IAIChatService

The primary interface for chat operations:

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

```csharp
public interface IAIChatService
{
    // Non-streaming responses
    Task<ChatResponse> GetChatResponseAsync(
        Action<AIChatBuilder> configure,
        IEnumerable<ChatMessage> messages,
        CancellationToken cancellationToken = default);

    // Streaming responses
    IAsyncEnumerable<ChatResponseUpdate> StreamChatResponseAsync(
        Action<AIChatBuilder> configure,
        IEnumerable<ChatMessage> messages,
        CancellationToken cancellationToken = default);

    // Advanced: Create the underlying client
    Task<IChatClient> CreateChatClientAsync(
        Action<AIChatBuilder> configure,
        CancellationToken cancellationToken = default);
}
```

{% endcode %}

## Basic Usage

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

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

public class ContentAssistant
{
    private readonly IAIChatService _chatService;

    public ContentAssistant(IAIChatService chatService)
    {
        _chatService = chatService;
    }

    public async Task<string> GetSuggestion(string prompt)
    {
        var messages = new List<ChatMessage>
        {
            new(ChatRole.User, prompt)
        };

        var response = await _chatService.GetChatResponseAsync(
            chat => chat.WithAlias("content-assistant"),
            messages);

        return response.Message.Text ?? string.Empty;
    }
}
```

{% endcode %}

## Message Roles

| Role                 | Description                                     |
| -------------------- | ----------------------------------------------- |
| `ChatRole.System`    | Instructions for the AI (set behavior, context) |
| `ChatRole.User`      | Messages from the user                          |
| `ChatRole.Assistant` | Previous responses from the AI                  |
| `ChatRole.Tool`      | Results from tool/function calls                |

## Multi-Turn Conversations

Include previous messages to maintain context:

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

```csharp
var conversation = new List<ChatMessage>
{
    new(ChatRole.System, "You are a helpful content editor."),
    new(ChatRole.User, "Write a headline for a blog about baking."),
    new(ChatRole.Assistant, "The Art of Perfect Sourdough: A Beginner's Journey"),
    new(ChatRole.User, "Make it shorter.")
};

var response = await _chatService.GetChatResponseAsync(
    chat => chat.WithAlias("content-editor"),
    conversation);
// Response considers the full conversation context
```

{% endcode %}

## Choosing a Profile

### Default Profile

Once you have [configured a default chat profile](/ai-in-umbraco/17.latest/backoffice/managing-settings.md) in the backoffice, you can call without specifying a profile:

```csharp
var response = await _chatService.GetChatResponseAsync(
    chat => chat.WithAlias("default-chat"),
    messages);
```

### Specific Profile

Pass the profile ID using the builder:

```csharp
var response = await _chatService.GetChatResponseAsync(
    chat => chat
        .WithAlias("profiled-chat")
        .WithProfile(profileId),
    messages);
```

Or use the profile alias directly:

```csharp
var response = await _chatService.GetChatResponseAsync(
    chat => chat
        .WithAlias("content-assistant")
        .WithProfile("content-assistant"),
    messages);
```

## In This Section

{% content-ref url="/pages/NjZrMJFzzUhSo1Kjt4oU" %}
[Basic Chat](/ai-in-umbraco/17.latest/using-the-api/chat/basic-chat.md)
{% endcontent-ref %}

{% content-ref url="/pages/9JeI0TCCBVwc7JOtD2gd" %}
[Streaming](/ai-in-umbraco/17.latest/using-the-api/chat/streaming.md)
{% endcontent-ref %}

{% content-ref url="/pages/5KZY2zDToU0W7NeZ2W1j" %}
[System Prompts](/ai-in-umbraco/17.latest/using-the-api/chat/system-prompts.md)
{% endcontent-ref %}

{% content-ref url="/pages/3OgPZllyx8t6GQ1cJxp8" %}
[Advanced Options](/ai-in-umbraco/17.latest/using-the-api/chat/advanced-options.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/chat.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.
