|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Getting Started with Blazor AI AssistView Component | Syncfusion |
| 4 | +description: Checkout and learn about getting started with Blazor AI AssistView component in Blazor Server App and Blazor WebAssembly App. |
| 5 | +platform: Blazor |
| 6 | +control: AI AssistView |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Getting Started with Blazor AI AssistView Component |
| 11 | + |
| 12 | +This section briefly explains about how to include [Blazor AI AssistView](https://www.syncfusion.com/blazor-components/blazor-ai-assistview) component in your Blazor Server App and Blazor WebAssembly App using Visual Studio. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +* [System requirements for Blazor components](https://blazor.syncfusion.com/documentation/system-requirements) |
| 17 | + |
| 18 | +## Create a new Blazor App in Visual Studio |
| 19 | + |
| 20 | +You can create a **Blazor Server App** or **Blazor WebAssembly App** using Visual Studio via [Microsoft Templates](https://learn.microsoft.com/en-us/aspnet/core/blazor/tooling?view=aspnetcore-7.0) or the [Syncfusion Blazor Extension](https://blazor.syncfusion.com/documentation/visual-studio-integration/template-studio). |
| 21 | + |
| 22 | +## Install Syncfusion Blazor InteractiveChat and Themes NuGet in the App |
| 23 | + |
| 24 | +To add **Blazor AI AssistView** component in the app, open the NuGet package manager in Visual Studio (*Tools → NuGet Package Manager → Manage NuGet Packages for Solution*), search and install `Syncfusion.Blazor.InteractiveChat` and [Syncfusion.Blazor.Themes](https://www.nuget.org/packages/Syncfusion.Blazor.Themes/). Alternatively, you can utilize the following package manager command to achieve the same. |
| 25 | + |
| 26 | +{% tabs %} |
| 27 | +{% highlight C# tabtitle="Package Manager" %} |
| 28 | + |
| 29 | +Install-Package Syncfusion.Blazor.InteractiveChat -Version {{ site.releaseversion }} |
| 30 | +Install-Package Syncfusion.Blazor.Themes -Version {{ site.releaseversion }} |
| 31 | + |
| 32 | +{% endhighlight %} |
| 33 | +{% endtabs %} |
| 34 | + |
| 35 | +N> Syncfusion Blazor components are available in [nuget.org](https://www.nuget.org/packages?q=syncfusion.blazor). Refer to [NuGet packages](https://blazor.syncfusion.com/documentation/nuget-packages) topic for available NuGet packages list with component details. |
| 36 | + |
| 37 | +## Register Syncfusion Blazor Service |
| 38 | + |
| 39 | +Open **~/_Imports.razor** file and import the `Syncfusion.Blazor` and `Syncfusion.Blazor.InteractiveChat` namespace. |
| 40 | + |
| 41 | +{% tabs %} |
| 42 | +{% highlight razor tabtitle="~/_Imports.razor" %} |
| 43 | + |
| 44 | +@using Syncfusion.Blazor |
| 45 | +@using Syncfusion.Blazor.InteractiveChat |
| 46 | + |
| 47 | +{% endhighlight %} |
| 48 | +{% endtabs %} |
| 49 | + |
| 50 | +Now, register the Syncfusion Blazor Service in the **~/Program.cs** file of your Blazor Server App or Blazor WebAssembly App. |
| 51 | + |
| 52 | +{% tabs %} |
| 53 | +{% highlight C# tabtitle="Blazor Server App" hl_lines="3 10" %} |
| 54 | + |
| 55 | +using Microsoft.AspNetCore.Components; |
| 56 | +using Microsoft.AspNetCore.Components.Web; |
| 57 | +using Syncfusion.Blazor; |
| 58 | + |
| 59 | +var builder = WebApplication.CreateBuilder(args); |
| 60 | + |
| 61 | +// Add services to the container. |
| 62 | +builder.Services.AddRazorPages(); |
| 63 | +builder.Services.AddServerSideBlazor(); |
| 64 | +builder.Services.AddSyncfusionBlazor(); |
| 65 | + |
| 66 | +var app = builder.Build(); |
| 67 | +.... |
| 68 | + |
| 69 | +{% endhighlight %} |
| 70 | +{% highlight C# tabtitle="Blazor WebAssembly App" hl_lines="3 11" %} |
| 71 | + |
| 72 | +using Microsoft.AspNetCore.Components.Web; |
| 73 | +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; |
| 74 | +using Syncfusion.Blazor; |
| 75 | + |
| 76 | +var builder = WebAssemblyHostBuilder.CreateDefault(args); |
| 77 | +builder.RootComponents.Add<App>("#app"); |
| 78 | +builder.RootComponents.Add<HeadOutlet>("head::after"); |
| 79 | + |
| 80 | +builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); |
| 81 | + |
| 82 | +builder.Services.AddSyncfusionBlazor(); |
| 83 | +await builder.Build().RunAsync(); |
| 84 | +.... |
| 85 | + |
| 86 | +{% endhighlight %} |
| 87 | +{% endtabs %} |
| 88 | + |
| 89 | +## Add stylesheet and script resources |
| 90 | + |
| 91 | +The theme stylesheet and script can be accessed from NuGet through [Static Web Assets](https://blazor.syncfusion.com/documentation/appearance/themes#static-web-assets). Reference the stylesheet and script in the `<head>` of the main page as follows: |
| 92 | + |
| 93 | +* For **.NET 6** Blazor Server app, include it in **~/Pages/_Layout.cshtml** file. |
| 94 | + |
| 95 | +* For **.NET 7** Blazor Server app, include it in the **~/Pages/_Host.cshtml** file. |
| 96 | + |
| 97 | +* For Blazor WebAssembly app, include it in the **~/index.html** file. |
| 98 | + |
| 99 | +```html |
| 100 | +<head> |
| 101 | + .... |
| 102 | + <link href="_content/Syncfusion.Blazor.Themes/material3.css" rel="stylesheet" /> |
| 103 | + <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script> |
| 104 | +</head> |
| 105 | +``` |
| 106 | +N> Check out the [Blazor Themes](https://blazor.syncfusion.com/documentation/appearance/themes) topic to discover various methods ([Static Web Assets](https://blazor.syncfusion.com/documentation/appearance/themes#static-web-assets), [CDN](https://blazor.syncfusion.com/documentation/appearance/themes#cdn-reference), and [CRG](https://blazor.syncfusion.com/documentation/common/custom-resource-generator)) for referencing themes in your Blazor application. Also, check out the [Adding Script Reference](https://blazor.syncfusion.com/documentation/common/adding-script-references) topic to learn different approaches for adding script references in your Blazor application. |
| 107 | + |
| 108 | +## Add Syncfusion Blazor AI AssistView component |
| 109 | + |
| 110 | +Add the Syncfusion Blazor AI AssistView component in the **~/Pages/Index.razor** file. |
| 111 | + |
| 112 | +{% tabs %} |
| 113 | +{% highlight razor %} |
| 114 | + |
| 115 | +<div class="aiassist-container" style="height: 350px; width: 650px;"> |
| 116 | + <SfAIAssistView></SfAIAssistView> |
| 117 | +</div> |
| 118 | + |
| 119 | +{% endhighlight %} |
| 120 | +{% endtabs %} |
| 121 | + |
| 122 | +* Press <kbd>Ctrl</kbd>+<kbd>F5</kbd> (Windows) or <kbd>⌘</kbd>+<kbd>F5</kbd> (macOS) to launch the application. This will render the Syncfusion Blazor AI AssistView component in your default web browser. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +## Configure suggestions and responses |
| 127 | + |
| 128 | +You can use the `PromptSuggestions` property to add prompt suggestions and the `PromptRequested` event to add responses when the prompt matches the specified prompts data otherwise, the default response will be displayed. |
| 129 | + |
| 130 | +{% tabs %} |
| 131 | +{% highlight razor %} |
| 132 | + |
| 133 | +@* desired render mode define here *@ |
| 134 | +@rendermode InteractiveAuto |
| 135 | + |
| 136 | +{% endhighlight %} |
| 137 | +{% endtabs %} |
| 138 | + |
| 139 | +{% tabs %} |
| 140 | +{% highlight razor %} |
| 141 | + |
| 142 | +<div class="aiassist-container" style="height: 350px; width: 650px;"> |
| 143 | + <SfAIAssistView PromptSuggestions="@promptSuggestions" PromptRequested="@PromptRequest"></SfAIAssistView> |
| 144 | +</div> |
| 145 | + |
| 146 | +@code { |
| 147 | + List<string> promptSuggestions = new List<string> { "How do I prioritize my tasks?", "How can I improve my time management skills?" }; |
| 148 | + public class AssistModel |
| 149 | + { |
| 150 | + public string Prompt { get; set; } |
| 151 | + public string Response { get; set; } |
| 152 | + } |
| 153 | + private List<AssistModel> prompts = new List<AssistModel>() |
| 154 | + { |
| 155 | + new AssistModel() { Prompt = "How do I prioritize my tasks?", Response = "Prioritize tasks by urgency and impact: tackle high-impact tasks first, delegate when possible, and break large tasks into smaller steps. For more assistance, feel free to ask—I’m here to help!" }, |
| 156 | + new AssistModel() { Prompt = "How can I improve my time management skills?", Response = "To improve time management skills, try setting clear goals, using a planner or digital tools, prioritizing tasks, breaking tasks into smaller steps, and minimizing distractions. Regularly review and adjust your approach for better efficiency" } |
| 157 | + }; |
| 158 | + private async Task PromptRequest(AssistViewPromptRequestedEventArgs args) |
| 159 | + { |
| 160 | + await Task.Delay(3000); |
| 161 | + var isPromptFound = prompts.Any(prompt => prompt.Prompt == args.Prompt); |
| 162 | + var promptData = prompts.FirstOrDefault(prompt => prompt.Prompt == args.Prompt); |
| 163 | + var defaultResponse = "For real-time prompt processing, connect the AI AssistView 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."; |
| 164 | + args.Response = isPromptFound ? promptData.Response : defaultResponse; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +{% endhighlight %} |
| 169 | +{% endtabs %} |
| 170 | + |
| 171 | + |
0 commit comments