Getting Started with Blazor Inline AI Assist Component
15 Jun 202611 minutes to read
This section briefly explains about how to include Syncfusion® Blazor Inline AI Assist component in a Blazor WebAssembly App using Visual Studio, Visual Studio Code, and the .NET CLI.
Prerequisites
Create a new Blazor App in Visual Studio
Create a Blazor WebAssembly App using Visual Studio via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to the Blazor WASM App Getting Started documentation.
Prerequisites
Create a new Blazor App in Visual Studio Code
Create a Blazor WebAssembly App using Visual Studio Code via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to the Blazor WASM App Getting Started documentation.
Alternatively, create a WebAssembly application by using the following command in the integrated terminal (Ctrl+`).
dotnet new blazorwasm -o BlazorApp
cd BlazorAppPrerequisites
Install the latest version of .NET SDK. If the .NET SDK is already installed, determine the installed version by running the following command in a command prompt (Windows), terminal (macOS), or command shell (Linux).
dotnet --versionCreate a Blazor WebAssembly App using .NET CLI
Run the following command to create a new Blazor WebAssembly App in a command prompt (Windows) or terminal (macOS) or command shell (Linux). For detailed instructions, refer to the Blazor WASM App Getting Started documentation.
dotnet new blazorwasm -o BlazorApp
cd BlazorAppInstall Syncfusion® Blazor packages
Install Syncfusion.Blazor.InteractiveChat and Syncfusion.Blazor.Themes NuGet packages in your project using the NuGet Package Manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), or the integrated terminal in Visual Studio Code (dotnet add package), or the .NET CLI.
Alternatively, run the following commands in the Package Manager Console to achieve the same.
Install-Package Syncfusion.Blazor.InteractiveChat -Version 34.1.29
Install-Package Syncfusion.Blazor.Themes -Version 34.1.29NOTE
All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details.
Add import namespaces
After the packages are installed, open the ~/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.InteractiveChat namespaces.
@using Syncfusion.Blazor
@using Syncfusion.Blazor.InteractiveChatRegister Syncfusion® Blazor service
Register the Syncfusion® Blazor service in the Program.cs file of your Blazor WebAssembly App.
....
using Syncfusion.Blazor;
....
builder.Services.AddSyncfusionBlazor();
....Add stylesheet and script resources
The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Include the stylesheet and script references in the ~/index.html file.
<link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" />
....
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>NOTE
Check out the Blazor Themes topic to discover various methods (Static Web Assets, CDN, and CRG) for referencing themes in the Blazor application. Also, check out the Adding Script Reference topic to learn different approaches for adding script references in the Blazor application.
Add Syncfusion® Blazor Inline AI Assist component
Add the Syncfusion® Inline AI Assist component to your application and render it on the page using a functional component. Use the RelateTo property to position the Inline AI Assist relative to a specific DOM element, and the Target property to specify the element where the component should be appended. Both properties accept either a CSS selector string (for example, .container or #id) or an HTMLElement instance.
```razor
@using Syncfusion.Blazor.InteractiveChat
<style>
#editableText {
width: 100%;
min-height: 120px;
max-height: 300px;
overflow-y: auto;
font-size: 16px;
padding: 12px;
border-radius: 4px;
border: 1px solid;
}
</style>
<div id="container" style="height: 350px; width: 650px;">
<button id="summarizeButton" style="margin-bottom: 10px;" @onclick="ShowPopup"> Content Summarize </button>
<div id="editableText" contenteditable="true">
<p>
Inline AI Assist component provides intelligent text processing
capabilities that enhance user productivity. It leverages advanced
natural language processing to understand context and deliver
precise suggestions. Users can seamlessly integrate AI-powered
features into their applications.
</p>
<p>
With real-time response streaming and customizable prompts,
developers can create interactive experiences. The component
supports multiple response modes including inline editing and
popup-based interactions.
</p>
</div>
<SfInlineAIAssist @ref="inlineAssist"
RelateTo="#summarizeButton"
Target="#container"
PromptRequested="PromptRequest">
</SfInlineAIAssist>
</div>
@code {
private SfInlineAIAssist inlineAssist = new();
private async Task PromptRequest(PromptRequestedEventArgs args)
{
await Task.Delay(1000);
string defaultResponse =
"For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.";
await inlineAssist.UpdateResponseAsync(defaultResponse);
}
private async Task ShowPopup()
{
await inlineAssist.ShowPopupAsync();
}
}
```
To launch the application, press Ctrl+F5 in Visual Studio, run dotnet run from the CLI or integrated terminal in VS Code, or use the Run command in your preferred IDE to render the Syncfusion® Blazor Inline AI Assist component in the default web browser.
Response display modes
Responses can be shown in two modes: Inline (updates content in-place) and Popup (shows responses in a floating popup). Toggle this behavior with the ResponseMode property.
```razor
@using Syncfusion.Blazor.InteractiveChat
@using Syncfusion.Blazor.Buttons
<style>
#editableText {
width: 100%;
min-height: 120px;
max-height: 300px;
overflow-y: auto;
font-size: 16px;
padding: 12px;
border-radius: 4px;
border: 1px solid;
}
</style>
<div class="container" style="height: 350px; width: 650px;">
<div style="margin-bottom: 10px;">
<label for="responseMode">Response Mode:</label>
<select id="responseMode" @onchange="OnResponseModeChangeAsync">
<option value="Popup">Popup</option>
<option value="Inline">Inline</option>
</select>
</div>
<SfButton id="summarizeButton" IsPrimary="true" Style="margin-bottom: 10px;" @onclick="OnSummarizeClick">Content Summarize</SfButton>
<div id="editableText" contenteditable="true">
@((MarkupString)editableContent)
</div>
<SfInlineAIAssist @ref="inlineAssist" ResponseMode="@currentResponseMode" RelateTo="#summarizeButton" PromptRequested="OnPromptRequestAsync"> <ResponseActions ItemSelect="OnItemSelectAsync"></ResponseActions>
</SfInlineAIAssist>
</div>
@code {
private SfInlineAIAssist inlineAssist = new SfInlineAIAssist();
private ResponseDisplayMode currentResponseMode = ResponseDisplayMode.Popup;
private string editableContent = @"<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>";
private async Task OnPromptRequestAsync(PromptRequestedEventArgs args)
{
await Task.Delay(1000);
string defaultResponse = "For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.";
await inlineAssist.UpdateResponseAsync(defaultResponse);
}
private async Task OnItemSelectAsync(ResponseItemSelectEventArgs args)
{
if (args.Item.Label == "Accept")
{
var lastPrompt = inlineAssist?.Prompts.LastOrDefault();
if (lastPrompt != null && !string.IsNullOrEmpty(lastPrompt.Response))
{
editableContent = $"<p>{lastPrompt.Response}</p>";
}
await inlineAssist!.HidePopupAsync();
}
else if (args.Item.Label == "Discard")
{
await inlineAssist!.HidePopupAsync();
}
}
private async Task OnSummarizeClick()
{
await inlineAssist.ShowPopupAsync();
}
private async Task OnResponseModeChangeAsync(ChangeEventArgs args)
{
if (Enum.TryParse<ResponseDisplayMode>(args.Value?.ToString(), out var mode))
{
currentResponseMode = mode;
await InvokeAsync(StateHasChanged);
await inlineAssist.ShowPopupAsync();
}
}
}
```
Note: Starting from version 33.1x, when a user submits a prompt to the Inline AI Assist, the component automatically scrolls and focuses on the latest prompt and response. This behavior eliminates the need for users to manually scroll down to see the new response, ensuring they always view the most recent AI response without interruption. Prior to version 33.1x, the previous responses remained visible when new responses were added.