Getting Started with Blazor Smart TextArea in Blazor Web App
15 Jul 202613 minutes to read
This section briefly explains how to include the Blazor Smart TextArea component in your Blazor Web App using Visual Studio, Visual Studio Code, and the .NET CLI.
To get started quickly with Blazor Smart TextArea component, you can check on this video.
Create a new Blazor Web App
Create a Blazor Web App using Visual Studio via Microsoft Templates or the Blazor Extension.
Run the following command to create a new Blazor Web App with the Interactive Server render mode
dotnet new blazor -o BlazorApp --interactivity Server
cd BlazorAppAlternatively, create a Blazor Web App using Visual Studio Code via Microsoft Templates, the Blazor Extension, or the C# Dev Kit extension.
Run the following command to create a new Blazor Web App with the Interactive Server render mode
dotnet new blazor -o BlazorApp --interactivity Server
cd BlazorAppNOTE
Configure the appropriate Interactive render mode and Interactivity location while creating a Blazor Web App. For detailed information, refer to the Interactive render mode documentation.
Install the required Blazor packages
Install the Syncfusion.Blazor.SmartComponents and Syncfusion.Blazor.Themes NuGet packages. All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details.
- Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
- Search the required NuGet packages (
Syncfusion.Blazor.SmartComponentsandSyncfusion.Blazor.Themes) and install them.
Alternatively, you can install the same packages using the Package Manager Console with the following commands.
Install-Package Syncfusion.Blazor.SmartComponents -Version 34.1.29
Install-Package Syncfusion.Blazor.Themes -Version 34.1.29Open the terminal and run the following commands.
dotnet add package Syncfusion.Blazor.SmartComponents -v 34.1.29
dotnet add package Syncfusion.Blazor.Themes -v 34.1.29Open the command prompt and run the following commands.
dotnet add package Syncfusion.Blazor.SmartComponents -v 34.1.29
dotnet add package Syncfusion.Blazor.Themes -v 34.1.29Add import namespaces
After the packages are installed, open the ~/Components/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.SmartComponents namespaces.
@using Syncfusion.Blazor
@using Syncfusion.Blazor.SmartComponentsRegister the Blazor service
Open the Program.cs file in Blazor Web App and register the Blazor service.
....
using Syncfusion.Blazor;
....
builder.Services.AddSyncfusionBlazor();
....Configure AI Service
Use the following instructions to register and configure an AI model for your application.
OpenAI
For OpenAI, create an API key and place it at openAIApiKey. The value for openAIModel is the model you wish to use (e.g., gpt-3.5-turbo, gpt-4, etc.).
Install the following NuGet packages to your project:
You can install these packages using different methods as shown below:
- Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
- Search the required NuGet packages (
Microsoft.Extensions.AIandMicrosoft.Extensions.AI.OpenAI) and install them.
Alternatively, you can install the same packages using the Package Manager Console with the following commands.
Install-Package Microsoft.Extensions.AI
Install-Package Microsoft.Extensions.AI.OpenAIOpen the terminal and run the following commands.
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAIOpen the command prompt and run the following commands.
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI- To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.
using Syncfusion.Blazor.SmartComponents;
using Syncfusion.Blazor.AI;
using Microsoft.Extensions.AI;
using OpenAI;
var builder = WebApplication.CreateBuilder(args);
....
builder.Services.AddSyncfusionBlazor();
string openAIApiKey = "API-KEY";
string openAIModel = "OPENAI_MODEL";
OpenAIClient openAIClient = new OpenAIClient(openAIApiKey);
IChatClient openAIChatClient = openAIClient.GetChatClient(openAIModel).AsIChatClient();
builder.Services.AddChatClient(openAIChatClient);
builder.Services.AddSyncfusionSmartComponents()
.InjectOpenAIInference();
var app = builder.Build();
....Azure OpenAI
For Azure OpenAI, first deploy an Azure OpenAI Service resource and model, then values for azureOpenAIKey, azureOpenAIEndpoint and azureOpenAIModel will all be provided to you.
Install the following NuGet packages to your project:
You can install these packages using different methods as shown below:
- Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
- Search the required NuGet packages (
Microsoft.Extensions.AI,Microsoft.Extensions.AI.OpenAIandAzure.AI.OpenAI) and install them.
Alternatively, you can install the same packages using the Package Manager Console with the following commands.
Install-Package Microsoft.Extensions.AI
Install-Package Microsoft.Extensions.AI.OpenAI
Install-Package Azure.AI.OpenAIOpen the terminal and run the following commands.
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Azure.AI.OpenAIOpen the command prompt and run the following commands.
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Azure.AI.OpenAI- To configure the AI service, add the following settings to the Program.cs file in your Blazor Web app.
using Syncfusion.Blazor.SmartComponents;
using Syncfusion.Blazor.AI;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using System.ClientModel;
var builder = WebApplication.CreateBuilder(args);
....
builder.Services.AddSyncfusionBlazor();
string azureOpenAIKey = "AZURE_OPENAI_KEY";
string azureOpenAIEndpoint = "AZURE_OPENAI_ENDPOINT";
string azureOpenAIModel = "AZURE_OPENAI_MODEL";
AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(
new Uri(azureOpenAIEndpoint),
new ApiKeyCredential(azureOpenAIKey)
);
IChatClient azureOpenAIChatClient = azureOpenAIClient.GetChatClient(azureOpenAIModel).AsIChatClient();
builder.Services.AddChatClient(azureOpenAIChatClient);
builder.Services.AddSyncfusionSmartComponents()
.InjectOpenAIInference();
var app = builder.Build();
....NOTE
From version 28.2.33 to 30.2.6, the Azure.AI.OpenAI package has been removed from the SmartComponents dependency. To use Azure OpenAI, please install the Azure.AI.OpenAI package separately in your Blazor application.
Ollama
To use Ollama for running self-hosted models:
-
Download and install Ollama
Visit Ollama’s official website and install the application appropriate for your operating system. -
Install the desired model from the Ollama library
You can browse and install models from the Ollama Library (e.g.,llama2:13b,mistral:7b, etc.). -
Configure your application
- Provide the
EndpointURL where the model is hosted (e.g.,http://localhost:11434). - Set
ModelNameto the specific model you installed (e.g.,llama2:13b).
- Provide the
- Install the following NuGet packages to your project:
- Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
- Search the required NuGet packages (
Microsoft.Extensions.AIandOllamaSharp) and install them.
Alternatively, you can install the same packages using the Package Manager Console with the following commands.
Install-Package Microsoft.Extensions.AI
Install-Package OllamaSharpOpen the terminal and run the following commands.
dotnet add package Microsoft.Extensions.AI
dotnet add package OllamaSharpOpen the command prompt and run the following commands.
dotnet add package Microsoft.Extensions.AI
dotnet add package OllamaSharp- Add the following settings to the Program.cs file in your Blazor Web app.
using Syncfusion.Blazor.SmartComponents;
using Syncfusion.Blazor.AI;
using Microsoft.Extensions.AI;
using OllamaSharp;
var builder = WebApplication.CreateBuilder(args);
....
builder.Services.AddSyncfusionBlazor();
string ModelName = "MODEL_NAME";
IChatClient chatClient = new OllamaApiClient("http://localhost:11434", ModelName);
builder.Services.AddChatClient(chatClient);
builder.Services.AddSyncfusionSmartComponents()
.InjectOpenAIInference();
var app = builder.Build();
....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 ~/Components/App.razor 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>Add Blazor Smart TextArea component
Open a Razor file located in the ~/Components/Pages/*.razor (for example, Home.razor) and add the Blazor Smart TextArea component inside the razor file.
NOTE
If the interactivity location is set to
Per page/component, define a render mode at the top of the razor file. (For exampleInteractiveServer). If the Interactivity is set toGlobal, the render mode is automatically configured in theApp.razorfile by default.
@rendermode InteractiveServer
@using Syncfusion.Blazor.SmartComponents
<SfSmartTextArea UserRole="@userRole" UserPhrases="@userPhrases" Placeholder="Enter your queries here" @bind-Value="prompt" Width="75%" RowCount="5">
</SfSmartTextArea>
@code {
private string? prompt;
public string userRole = "Maintainer of an open-source project replying to GitHub issues";
public string[] userPhrases = [
"Thank you for contacting us.",
"To investigate, We will need a reproducible example as a public Git repository.",
"Could you please post a screenshot of NEED_INFO?",
"This sounds like a usage question. This issue tracker is intended for bugs and feature proposals. Unfortunately, we don't have the capacity to answer general usage questions and would recommend StackOverflow for a faster response.",
"We do not accept ZIP files as reproducible examples.",
"Bug report: File not found error occurred in NEED_INFO"
];
}Here,
UserRole: This attribute is required to defines the context of the autocompletion based on the role of the person typing.
UserPhrases: This attribute is optional to provides predefined expressions that align with User/application’s tone and frequently used content.
- Type ‘To investigate’ to experience instant sentence autocompletion.
Run the application
Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application. The Blazor Smart TextArea component will render in your default web browser.
Open the terminal and run the following command.
dotnet runOpen the command prompt and run the following command.
dotnet run
NOTE