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 BlazorApp

Alternatively, 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 BlazorApp

NOTE

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.

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Syncfusion.Blazor.SmartComponents and Syncfusion.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.29

Open 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.29

Open 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.29

Add 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.SmartComponents

Register 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:

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Microsoft.Extensions.AI and Microsoft.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.OpenAI

Open the terminal and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI

Open 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:

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Microsoft.Extensions.AI, Microsoft.Extensions.AI.OpenAI and Azure.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.OpenAI

Open 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.OpenAI

Open 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:

  1. Download and install Ollama
    Visit Ollama’s official website and install the application appropriate for your operating system.

  2. 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.).

  3. Configure your application

    • Provide the Endpoint URL where the model is hosted (e.g., http://localhost:11434).
    • Set ModelName to the specific model you installed (e.g., llama2:13b).
  • Install the following NuGet packages to your project:
  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Microsoft.Extensions.AI and OllamaSharp) 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 OllamaSharp

Open the terminal and run the following commands.

dotnet add package Microsoft.Extensions.AI
dotnet add package OllamaSharp

Open 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 example InteractiveServer). If the Interactivity is set to Global, the render mode is automatically configured in the App.razor file 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 run

Open the command prompt and run the following command.

dotnet run

Syncfusion Smart TextArea - Output

NOTE

View Sample in GitHub.

See also

  1. Getting Blazor Smart TextArea in Blazor Web App
  2. Getting Started with Blazor Web App
  3. Getting Started with Blazor Server App