New to Telerik UI for BlazorStart a free 30-day trial

AIPrompt Events

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

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. See the example below.

PropertyTypeDescription
PromptstringThe prompt text of the request.
OutputstringThe output of the request. The output is based on the prompt text.
IsCancelledboolWhether the event is cancelled and the built-in action is prevented.
OutputItemAIPromptOutputItemDescriptorThe output item. This property will be populated only when the user retries an existing output. See AIPromptOutputItemDescriptor.

OnCommandExecute

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

The event handler receives an argument of type AIPromptCommandExecuteEventArgs. See the example below.

PropertyTypeDescription
CommandAIPromptCommandDescriptorThe executed command.
OutputstringThe output based on the executed command.
IsCancelledboolWhether the event is cancelled and the built-in action is prevented.
OutputItemAIPromptOutputItemDescriptorThe output item. This property will be populated only when the user retries an existing output. See AIPromptOutputItemDescriptor.

OnOutputRate

The OnOutputRate event fires when the user rates an output.

The event handler receives an argument of type AIPromptOutputRateEventArgs. See the example below.

PropertyTypeDescription
OutputItemAIPromptOutputItemDescriptorSpecifies the output item that is being rated. See AIPromptOutputItemDescriptor.

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

Using AIPrompt events

@* 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;
    }
}