Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ navigation:
"*components/filemanager/data-binding":
title: "Data Binding"
position: 2
"*components/aiprompt/views":
title: "Views"
position: 10

## List helpers directory names and order here, like this:
"*appearance":
Expand Down Expand Up @@ -266,6 +269,8 @@ navigation:
title: "Spreadsheet"
"*gauges/arc":
title: "Arc"
"*aiprompt":
title: "AIPrompt"
"*appbar":
title: "AppBar"
"*autocomplete":
Expand Down Expand Up @@ -620,6 +625,7 @@ intro_columns:
-
title: "Interactivity and UX"
items:
"AIPrompt": "aiprompt-overview"
"Loader": "loader-overview"
"LoaderContainer": "loadercontainer-overview"
"Skeleton": "skeleton-overview"
Expand Down
129 changes: 129 additions & 0 deletions components/aiprompt/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
title: Events
page_title: AIPrompt - Events
description: Explore the events generated by the AIPrompt for Blazor. Learn how to handle these events and implement custom functionality.
slug: aiprompt-events
tags: telerik,blazor,aiprompt,ai,prompt,events
published: True
position: 40
---

# AIPrompt Events

This article explains the events available in the Telerik AIPrompt for Blazor:

* [`OnPromptRequest`](#onpromptrequest)
* [`OnCommandExecute`](#oncommandexecute)
* [`OnOutputRate`](#onoutputrate)
* [`PromptTextChanged`](#promptexttchanged)

## OnPromptRequest

The `OnPromptRequest` event fires when the user clicks on the **Generate** button within the Prompt view or retries a prompt from the Output view.

The event handler receives an argument of type [`AIPromptPromptRequestEventArgs`](/blazor-ui/api/Telerik.Blazor.Components.AIPromptPromptRequestEventArgs). See the [example below](#example).

@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout)

| Property | Type | Description |
| --- | --- | --- |
| `Prompt` | `string` | The prompt text of the request. |
| `Output` | `string` | The output of the request. The output is based on the prompt text. |
| `IsCancelled` | `bool` | Whether the event is cancelled and the built-in action is prevented. |
| `OutputItem` | `AIPromptOutputItem` | The output item. This property will be populated only when the user retries an existing output. See [`AIPromptOutputItem`](/blazor-ui/api/Telerik.Blazor.Components.AIPromptOutputItem). |


## OnCommandExecute

The `OnCommandExecute` event fires when the user clicks on a command within the Commands view.

The event handler receives an argument of type [`AIPromptCommandDescriptorExecuteEventArgs`](/blazor-ui/api/Telerik.Blazor.Components.AIPromptCommandDescriptorExecuteEventArgs). See the [example below](#example).

@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout)

| Property | Type | Description |
| --- | --- | --- |
| `Command` | `AIPromptCommandDescriptor` | The executed command. |
| `Output` | `string` | The output based on the executed command. |
| `IsCancelled` | `bool` | Whether the event is cancelled and the built-in action is prevented. |
| `OutputItem` | `AIPromptOutputItem` | The output item. This property will be populated only when the user retries an existing output. See [`AIPromptOutputItem`](/blazor-ui/api/Telerik.Blazor.Components.AIPromptOutputItem). |


## OnOutputRate

The `OnOutputRate` event fires when the user rates an output.

The event handler receives an argument of type [`AIPromptOutputRateEventArgs`](/blazor-ui/api/Telerik.Blazor.Components.AIPromptOutputRateEventArgs). See the [example below](#example).

@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout)

| Property | Type | Description |
| --- | --- | --- |
| `OutputItem` | `AIPromptOutputItem` | Specifies the output item that is being rated. See [`AIPromptOutputItem`](/blazor-ui/api/Telerik.Blazor.Components.AIPromptOutputItem). |

## PromptTextChanged

The `PromptTextChanged` event fires when the user changes the prompt text. Use the event to update the AIPrompt's prompt when the `PromptText` parameter is set with one-way binding, otherwise, the user action will be ignored.

## See Also

## Example

>caption Using AIPrompt events

````CSHTML
@* All AIPrompt events *@

<TelerikAIPrompt OnPromptRequest="@OnPromptRequestHandler"
OnCommandExecute="@OnCommandExecuteHandler"
OnOutputRate="@OnOutputRateHandler"
PromptChanged="@OnPromptChanged"
ShowOutputRating="true"
Prompt="@Prompt"
Commands="@PromptCommands">
</TelerikAIPrompt>

@code {
private string Prompt { get; set; }

private List<AIPromptCommandDescriptor> PromptCommands { get; set; } = new List<AIPromptCommandDescriptor>()
{
new AIPromptCommandDescriptor() { Id = "1", Title = "Correct Spelling and grammar", Icon = SvgIcon.SpellChecker },
new AIPromptCommandDescriptor() { Id = "2", Title = "Change Tone", Icon = SvgIcon.TellAFriend,
Children = new List<AIPromptCommandDescriptor>
{
new AIPromptCommandDescriptor() { Id = "3", Title = "Professional" },
new AIPromptCommandDescriptor() { Id = "4", Title = "Conversational" },
new AIPromptCommandDescriptor() { Id = "5", Title = "Humorous" },
new AIPromptCommandDescriptor() { Id = "6", Title = "Empathic" },
new AIPromptCommandDescriptor() { Id = "7", Title = "Academic" },
}
},
};

private void OnPromptRequestHandler(AIPromptPromptRequestEventArgs args)
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
args.Output = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vel pretium lectus quam id leo in.";
}

private void OnCommandExecuteHandler(AIPromptCommandExecuteEventArgs args)
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
args.Output = "Nisl pretium fusce id velit ut tortor pretium. A pellentesque sit amet porttitor eget dolor. Lectus mauris ultrices eros in cursus turpis massa tincidunt.";
}

private void OnOutputRateHandler(AIPromptOutputRateEventArgs args)
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
}

private void OnPromptChanged(string prompt)
{
Prompt = prompt;
}
}

````

* [Live Demo: AIPrompt Overview](https://demos.telerik.com/blazor-ui/aiprompt/overview)
163 changes: 163 additions & 0 deletions components/aiprompt/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
title: Overview
page_title: AIPrompt Overview
description: Discover the AIPrompt component for Blazor. Learn how to add the component to your app and explore its features like handling prompts and their outputs.
slug: aiprompt-overview
tags: telerik,blazor,aiprompt,ai,prompt
published: True
position: 0
---

# Blazor AIPrompt Overview

The <a href = "https://www.telerik.com/blazor-ui/aiprompt" target="_blank">Blazor AIPrompt component</a> helps you write input (prompt) instructing the Generative AI to produce the desired response.

The component allows you to interact with the output from the AI and execute a set of predefined commands. Furthermore, the AIPrompt comes with three predefined views—Prompt, Output, and Command, as well as the option to define custom views. Users can navigate the views through the AIPrompt's ToolBar.

## Creating Blazor AIPrompt

1. Add the `<TelerikAIPrompt>` tag.
1. Subscribe to the `OnPromptRequest` event that will fire whenever the user sends a prompt request. The handler expects an argument of type `AIPromptPromptRequestEventArgs`.
1. (optional) Set the `Commands` parameter to a `List<AIPromptCommandDescriptor>`. If the parameter is not set, the AIPrompt will not render the Commands view.
1. (optional) Subscribe to the `OnCommandExecute` event that will fire whenever the user executes a command. The handler expects an argument of type `AIPromptCommandDescriptorExecuteEventArgs`.

>caption Basic configuration of the Telerik AIPrompt

````CSHTML
<TelerikAIPrompt OnPromptRequest="@HandlePromptRequest"
OnCommandExecute="@HandleCommandExecute"
Commands="@PromptCommands">
</TelerikAIPrompt>

@code {
private void HandlePromptRequest(AIPromptPromptRequestEventArgs args)
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
args.Output = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}

private void HandleCommandExecute(AIPromptCommandExecuteEventArgs args)
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
args.Output = "Vel pretium lectus quam id leo in. Nisl pretium fusce id velit ut tortor pretium.";
}

private List<AIPromptCommandDescriptor> PromptCommands { get; set; } = new List<AIPromptCommandDescriptor>()
{
new AIPromptCommandDescriptor() { Id = "1", Title = "Correct spelling and grammar", Icon = FontIcon.SpellChecker },
new AIPromptCommandDescriptor() { Id = "2", Title = "Change Tone", Icon = SvgIcon.TellAFriend,
Children = new List<AIPromptCommandDescriptor>
{
new AIPromptCommandDescriptor() { Id = "3", Title = "Professional" },
new AIPromptCommandDescriptor() { Id = "4", Title = "Conversational" },
new AIPromptCommandDescriptor() { Id = "5", Title = "Humorous" },
new AIPromptCommandDescriptor() { Id = "6", Title = "Empathic" },
new AIPromptCommandDescriptor() { Id = "7", Title = "Academic" },
}
},
new AIPromptCommandDescriptor() { Id = "8", Title = "Simplify", Icon = SvgIcon.MinWidth },
new AIPromptCommandDescriptor() { Id = "9", Title = "Expand", Icon = SvgIcon.MaxWidth },
};
}
````


## ToolBar

The AIPrompt includes a toolbar with built-in buttons that activate the view they are related to. The component also exposes the option to add custom tools, which may be associated with arbitrary handlers. [Read more about the AIPrompt's ToolBar...]({%slug aiprompt-toolbar%})


## Views

The AIPrompt component offers the Prompt, Output, and Commands views that relate to the current state of the prompt-response lifecycle. You can also customize the component through custom views. [Read more about the AIPrompt views...]({%slug aiprompt-views-overview%})


## Templates

The AIPrompt component provides templates that enable developers to customize the rendering and appearance of the component. [Read more about the AIPrompt templates...]({%slug aiprompt-templates%})


## Events

The various AIPrompt events allow you to implement custom functionality and handle user interactions with the component's ToolBar. [Read more about the AIPrompt events...]({%slug aiprompt-events%})


## AIPrompt Parameters

The table below lists the AIPrompt parameters. For a full list of the AIPrompt API members (parameters, methods, and events), check the [AIPrompt API Reference](/blazor-ui/api/Telerik.Blazor.Components.TelerikAIPrompt).

@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout)

| Parameter | Type and Default&nbsp;Value | Description |
| --- | --- | --- |
| `AIPromptViews` | `RenderFragment` | Allows control over the views of the content. Use it to set the visibility of a predefined view or to create custom views. If a render fragment is not provided, the AIPrompt will display its default views. |
| `AIPromptToolBar` | `RenderFragment` | Any additional buttons that will be rendered within the ToolBar. This parameter will append the new items, rather than override buttons related to existing views. |
| `PromptText` | `string` | The value of the text within the prompt view. Use it when you need to add some form of transformation to the prompt. The parameter supports two-way binding. |
| `PromptTextChanged` | `EventCallback<string>` | The handler called whenever the `PromptText` changes. |
| `PromptSuggestions` | `List<string>` | The prompt suggestions displayed within the Prompt view. |
| `PromptSuggestionItemTemplate` | `RenderFragment<string>` | The Prompt Suggestion Item template of the AIPrompt. |
| `Commands` | `List<AIPromptCommandDescriptor>` | The predefined commands displayed within the Commands view. |
| `ShowOutputRating` | `bool` <br /> (`false`) | Controls the visibility of the rating buttons within the output card. |
| `OnPromptRequest` | `EventCallback<AIPromptPromptRequestEventArgs>` | The event handler called when the user requests an output for a given prompt. |
| `OnCommandExecute` | `EventCallback<AIPromptCommandDescriptorExecuteEventArgs>` | The event handler called when a user clicks on a command within the Command view. |
| `OnOutputRate` | `EventCallback<AIPromptOutputRateEventArgs>` | The event handler called when a user rates an output item. |
| `Class` | `string` | The `class` attribute of the `<div class="k-prompt">` element. Use it to apply custom styles or [override the theme]({%slug themes-override%}). |
| `Height` | `string` | The `height` style of the component in any [supported CSS unit]({%slug common-features/dimensions%}). The default AIPrompt dimensions depend on the CSS theme. |
| `Width` | `string` | The `width` style of the component in any [supported CSS unit]({%slug common-features/dimensions%}). The default AIPrompt dimensions depend on the CSS theme. |

## AIPrompt Reference and Methods

The AIPrompt exposes methods for programmatic operation. To use them, define a reference to the component instance with the `@ref` directive attribute.

| Method | Description |
| --- | --- |
| `Refresh` | Re-renders the component. |
| `AddOutput` | Insert a new output item to the AIPrompt. |

>caption AIPrompt reference and method usage

````CSHTML
<TelerikAIPrompt @ref="@AIPromptRef" OnPromptRequest="@HandlePromptRequest"></TelerikAIPrompt>
<div style="margin-top: 2em;">
<TelerikTextBox @bind-Value="@CustomPrompt"></TelerikTextBox>
<TelerikButton OnClick="@ExternalGenerateHandler">Generate</TelerikButton>
</div>

@code {
private string CustomPrompt { get; set; }
private TelerikAIPrompt AIPromptRef { get; set; }

private void ExternalGenerateHandler()
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
AIPromptRef.AddOutput(
output: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
title: "Generated from an external prompt",
subtitle: string.Empty,
prompt: CustomPrompt,
commandId: null,
openOutputView: true);
}

private void HandlePromptRequest(AIPromptPromptRequestEventArgs args)
{
// The example uses dummy data intentionally. Replace the hard-coded string with a call to your AI API.
args.Output = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}
}
````


## Next Steps

* [Configure the AIPrompt ToolBar]({%slug aiprompt-toolbar%})
* [Customize the AIPrompt Views]({%slug aiprompt-views-overview%})
* [Make the AIPrompt Your Own through Custom Commands]({%slug aiprompt-views-commands%})
* [Implement AIPrompt Views Templates]({%slug aiprompt-views-templates%})
* [Implement AIPrompt Templates]({%slug aiprompt-templates%})
* [Handle the AIPrompt Events]({%slug aiprompt-events%})

## See Also

* [Live Demo: AIPrompt](https://demos.telerik.com/blazor-ui/aiprompt/overview)
* [AIPrompt API Reference](/blazor-ui/api/Telerik.Blazor.Components.TelerikAIPrompt)
74 changes: 74 additions & 0 deletions components/aiprompt/templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: Templates
page_title: AIPrompt Templates
description: Discover the AIPrompt templates that let you customize the appearance of the component, for example, the rendering of the prompt suggestions.
slug: aiprompt-templates
tags: telerik,blazor,aiprompt,ai,prompt,templates
published: True
position: 30
---

# AIPrompt Templates

The AIPrompt component provides the `PromptSuggestionItemTemplate` that allows you to change the appearance of the prompt suggestions made by the component.

>tip The AIPrompt component also implements [View templates]({%slug aiprompt-templates%}) that control the rendering of the Prompt, Output, and Command views.

The Prompt view of the AIPrompt renders any suggestions passed to the `PromptSuggestions` parameter in the form of elevated bubbles within a collapsible section. The `PromptSuggestionItemTemplate` allows you to control the rendering of individual suggestions.

>note By default, clicking on a suggestion will populate the prompt's input with the suggestion's value and also trigger a `PromptTextChanged` event. If you use the `PromptSuggestionItemTemplate`, you should also handle [any event]({%slug aiprompt-events%}) you deem necessary (such as `onclick`).

>caption Using the `PromptSuggestionItemTemplate` to alter the appearance of the suggestions

````CSHTML
<TelerikAIPrompt @bind-Prompt="@Prompt" PromptSuggestions="@Suggestions">
<PromptSuggestionItemTemplate>
<div @onclick="@OnSuggestionClick" class="my-custom-suggestion-item">
<TelerikSvgIcon Icon="@SvgIcon.Clipboard" />
<span class="my-custom-suggestion-item-text">@context</span>
</div>
</PromptSuggestionItemTemplate>
</TelerikAIPrompt>

<style>
.my-custom-suggestion-item {
display: flex;
align-items: center;
width: fit-content;
padding: 0.5em 1.5em;
border-radius: 20px;
box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
cursor: pointer;
transition: transform 0.2s ease-in-out;
}

.my-custom-suggestion-item:hover {
transform: scale(1.05);
}

.my-custom-suggestion-item-text {
padding-inline-start: 0.5em;
}

</style>

@code {
private string Prompt { get; set; }
private TelerikAIPrompt AIPromptRef { get; set; }

private List<string> Suggestions { get; set; } = new List<string>()
{
"Explain quantum physics in simple terms.",
"What are the three laws of thermodynamics?"
};

private void OnSuggestionClick()
{
// handle suggestion click here.
}
}
````

## See Also

* [Views Templates]({%slug aiprompt-views-templates%})
Loading