Grid AI Toolbar Assistant
The GridToolBarAIAssistantTool is a built-in toolbar tool that integrates an AI prompt interface directly into the Grid toolbar. It allows users to type natural language prompts that can be sent to an AI service, which then suggests appropriate data operations for the Grid to perform.
Setup Steps
Use the following steps to implement Grid AI integration using the GridToolBarAIAssistantTool.
Install NuGet Package
- Add the
Telerik.AI.SmartComponents.ExtensionsNuGet package to your Blazor app. - Import the
Telerik.AI.SmartComponents.Extensionsnamespace in your.razorfile or globally in_Imports.razor.
@using Telerik.AI.SmartComponents.Extensions
Add GridToolBarAIAssistantTool
- Add a
GridToolBarAIAssistantTooltool to the Grid ToolBar. - Subscribe to the
OnPromptRequestevent of the nested<GridToolBarAIAssistantToolPromptSettings>component. It is effectively theOnPromptRequestevent of a Telerik AIPrompt component. - Optionally, define some
PromptSuggestions.
<TelerikGrid>
<GridToolBar>
<GridToolBarAIAssistantTool>
<GridToolBarAIAssistantToolSettings>
<GridToolBarAIAssistantToolPromptSettings OnPromptRequest="@OnAIPromptRequest"
PromptSuggestions="@AIPromptSuggestions">
</GridToolBarAIAssistantToolPromptSettings>
</GridToolBarAIAssistantToolSettings>
</GridToolBarAIAssistantTool>
</GridToolBar>
</TelerikGrid>
@code {
private List<string> AIPromptSuggestions { get; set; } = new();
private async Task OnAIPromptRequest(AIPromptPromptRequestEventArgs args)
{
}
}
Implement OnPromptRequest Handler
Implement the OnPromptRequest event handler of the AIPrompt that is used internally by the GridToolBarAIAssistantTool:
- Submit the
Requestproperty of theOnPromptRequestevent argument to the API endpoint of your AI service. - Return a serialized
GridAIResponsefrom your API endpoint. - Set the
Responseproperty of theAIPromptPromptRequestEventArgsevent argument to thestringresponse of the API endpoint. Alternatively, execute the GridProcessAIResponseAsync()method and provide the API response as astringargument.
private async Task OnAIPromptRequest(AIPromptPromptRequestEventArgs args)
{
try
{
HttpResponseMessage requestResult = await this.HttpClientInstance.PostAsJsonAsync("https://.....", args.Request);
string resultContent = await requestResult.Content.ReadAsStringAsync();
GridAIResponse aiResponse = await requestResult.Content.ReadFromJsonAsync<GridAIResponse>();
args.Output = $"The request was processed. {string.Join(". ", aiResponse.Messages)}.";
args.Response = resultContent;
}
catch (Exception)
{
args.Output = "The request returned no results. Try another request.";
}
}
Examples
The following online demos show complete implementations of the Grid AI smart functionality with the GridToolBarAIAssistantTool. These examples use a Telerik-hosted AI service for demonstration purposes only. See Integration with Telerik.AI.SmartComponents.Extensions for more information on how you can implement your own server-side AI service to be compatible with the Telerik Grid.