# Editor Integration with AIPrompt

The Editor provides a built-in integration with the [AIPrompt component](https://www.telerik.com/blazor-ui/documentation/components/aiprompt/overview) to help users apply AI-driven suggestions and completions to their Editor.

The integration with the AIPrompt component covers the following use cases:

* Standalone prompting - The user can ask the AIPrompt without any context provided by the Editor. This could be useful for idea generation, writing a whole piece of content (blog post), or just random questions.
* Prompting with context - The user prompt will use additional context from the Editor (the UI allows the user to decide whether to use the selected text, if any, or the whole content).
* Command with context - The user can select a command that will be applied to the selected content (if any) or the whole content if no text is selected.

## Enabling the AIPrompt

To enable the AIPrompt in the Editor:

1. Set the `EnableAIPrompt` parameter to `true`.
2. Register an [`IChatClient` service](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.ai.ichatclient?view=net-9.0-pp) to generate the AIPrompt responses. Configure the service according to the model you are using. The AIPrompt is designed to automatically use the registered `IChatClient` as the component provides a [built-in integration with the **Microsoft.Extensions.AI** library](https://www.telerik.com/blazor-ui/documentation/common-features/microsoft-extensions-ai-integration).

> caption Enabling the AIPrompt in the Editor

```RAZOR.skip-repl Editor
<TelerikEditor @bind-Value="@EditorValue"
               EnableAIPrompt="true"
               Height="400px">
</TelerikEditor>

@code {
    private string EditorValue { get; set; } = "<p>Editor content</p>";
}
```

```C# Program.cs
// ...

// This example uses Azure OpenAI but you must configure the service depending on the model you are using.
// Read more at https://www.telerik.com/blazor-ui/documentation/common-features/microsoft-extensions-ai-integration

builder.Services.AddSingleton(new AzureOpenAIClient(
    new Uri("YOUR_AZURE_OPENAI_ENDPOINT"),
    new AzureKeyCredential("YOUR_AZURE_OPENAI_API_KEY")
));

builder.Services.AddChatClient(services => services.GetRequiredService<AzureOpenAIClient>().GetChatClient("YOUR_MODEL_NAME").AsIChatClient());

// ...
```

## Customizing the AIPrompt

The Editor allows customizing some of the integrated AIPrompt's settings. For that purpose, use the `<EditorAIPromptSettings>` tag. It provides the following parameters that you can configure:

<style>
    .article table {
        table-layout: auto;
        word-break: normal;
    }
</style>

| Parameter      | Type and Default value            | Description                                                                                                                                                                                                                                   |
| -------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `SystemPrompt` | `string`                          | The system prompt that will be passed to the integrated AIPrompt. If not provided, the AIPrompt will use its [default `SystemPrompt` value](https://www.telerik.com/blazor-ui/documentation/components/aiprompt/overview#parameters-and-api). |
| `Commands`     | `List<AIPromptCommandDescriptor>` | The commands displayed within the Commands view. If not set the AIPrompt will use the [default predefined commands](https://www.telerik.com/blazor-ui/documentation/components/editor/ai-integration/overview#ai-integration-capabilities).   |

> caption Customizing the AIPrompt in the Editor

```RAZOR.skip-repl
<TelerikEditor @bind-Value="@EditorValue"
               EnableAIPrompt="true"
               Height="400px">
    <EditorSettings>
        <EditorAIPromptSettings Commands="@AIPromptCommands" />
    </EditorSettings>
</TelerikEditor>

@code {
    private string EditorValue { get; set; } = "Sample Editor content";

    private List<AIPromptCommandDescriptor> AIPromptCommands { get; set; } = new()
    {
        new AIPromptCommandDescriptor() { Id = "1", Title = "Simplify", Icon = SvgIcon.MinWidth, Prompt = "Simplify the text" },
        new AIPromptCommandDescriptor() { Id = "2", Title = "Expand", Icon = SvgIcon.MaxWidth , Prompt = "Expand the text" },
        new AIPromptCommandDescriptor() { Id = "3", Title = "Translate", Icon = SvgIcon.EditTools,
            Children = new List<AIPromptCommandDescriptor>
            {
                new AIPromptCommandDescriptor() { Id = "4", Title = "English", Prompt = "Translate the text to English" },
                new AIPromptCommandDescriptor() { Id = "5", Title = "Bulgarian", Prompt = "Translate the text to Bulgarian" },
                new AIPromptCommandDescriptor() { Id = "6", Title = "Spanish", Prompt = "Translate the text to Spanish" },
            }
        }
    };
}
```

## See Also

* [Live Demo: AI Integration](https://demos.telerik.com/blazor-ui/editor/ai-integration)
* [AI Integration Overview](https://www.telerik.com/blazor-ui/documentation/components/editor/ai-integration/overview)
* [Editor Integration with Inline Prompt](https://www.telerik.com/blazor-ui/documentation/components/editor/ai-integration/integration-with-inline-prompt)
