The Blazor PromptBox component helps connect people with AI models, with three input modes, customization and integration with your favorite AI model.
Nowadays, it is increasingly common to see more and more applications that integrate features using AI to make things easier for users. An essential component for this to work is the text box in which the user can write instructions for the AI model to process their request.
Internally, that component can be very simple or incorporate complex features. In this article, we will analyze the Progress Telerik UI for Blazor PromptBox component, designed for communication between people and AI. Let’s see what it is about.
What Is the Telerik PromptBox Component?
The Telerik PromptBox component is built for AI-based applications. One advantage it offers is that it combines text input, speech recognition, file selection and an action button that toggles between send and stop.
Its main features include:
- Allows specifying the input mode
- Integrates a button to start speech-to-text recognition
- Includes a button to select files with built-in validation
- Has an action button with loading states
- Among other features
Let’s see some of the most important features in action.
Creating a Project and Configuring It to Use Blazor PromptBox
The first thing you should do to work with the PromptBox component is create a new project using the Telerik pre-created template, or if you already have an existing project, installing the Telerik components into your existing project.
Another important point is to install the packages necessary to use generative AI models. In my case, since I will use Azure OpenAI, I will install the packages:
To carry out the configuration of an AI model, it is recommended to register a service of type IChatClient, so it can be injected into any component we want. We will do this by going to Program.cs, reading the key from the configuration:
In the code above, we read two possible configuration keys, one to read the OpenAI API Key, and another for Azure OpenAI. Depending on the value found, the method AddChatClient is used to configure the corresponding client with the necessary values.
If no keys are found configured, a warning will be shown. With the model configured, it’s time to try the component.
Using the PromptBox Component in a Blazor Project
Let’s start by creating a component style page called Playground.razor, defining the basic structure of the page:
In the previous code section, we make sure to inject the IChatClient instance so we can talk to an LLM model.
Configuring Selection Mode
The PromptBox component allows users to interact in three ways, through the parameter PromptBoxMode:
- Auto (default): Starts as a single-line input and transforms into a multi-line area if the content requires it.
- SingleLine: A single-line input with horizontal scrolling. Height remains constant.
- MultiLine: A multi-line text area that grows vertically.
To see them in action at the same time, I’ll use the fantastic Telerik components TelerikButtonGroup and TelerikChip to create a section that allows me to select between the different modes:
Once the mode selection part is defined, we can implement the PromptBox component in the project using the tag TelerikPromptBox. For this demonstration, we’re going to enable all its features, including the modes parameter, the loading state, voice recognition, file selection and an initial template:
In the previous code, we can notice the usage of some important parameters:
- Mode is bound to SelectedMode, allowing the mode to change automatically.
- IsLoading allows showing whether a response is being generated, giving good feedback to the user that their request is being processed.
- EnableSpeechToText and EnableFileSelect enable the microphone and attach-file buttons.
- <PromptBoxSettings> is a tag that allows configuring details for each button of the component. For example, PromptBoxFileSelectButtonSettings limits allowed extensions as well as whether multiple file selection is allowed, while PromptBoxSpeechToTextButtonSettings defines the language for voice recognition.
- <PromptBoxStartAffixTemplate> is a section where we can add adornments to the component. In our example we add an icon at the start of the text box. Here you could add any content, such as buttons or a status indicator.
When running the application, the component already displays correctly, configured as specified:
By pressing each of the modes, you can notice how the size of the text box changes to adapt. Now let’s see how to converse with the AI model.
Interacting with an AI Model Using PromptBox
We’ll keep a conversation with the AI model by maintaining a list of messages. To make it more interactive, we’ll display the user’s messages on the right, and those generated by the AI model on the left. Similarly, we’ll implement a loader to show while a response is being generated:
In the previous code, if the list is empty, a message is shown so the user knows they should send a prompt. Otherwise, if the list has data, it iterates through the list and aligns left or right depending on who sent the message.
For user messages, if any have attached files, they are displayed as a list of TelerikChip. Also, if IsProcessing is true, a TelerikLoader is shown with a message indicating that a response is being generated.
To populate the message list, we need to add the interaction logic with the AI model, which we’ll do next.
Conversing with AI Models Using the PromptBox Component
For everything to work correctly, we must implement the method OnPromptAction. This event handler receives an argument of type PromptBoxActionButtonEventArgs that contains information such as the text, the action to send or stop, as well as attached files. The method is as follows:
In the previous method, we do the following:
- We check whether the received action is Stop, in which case we cancel the CancellationTokenSource to stop message generation.
- We verify that the text is not empty and that there is no request in progress.
- We add the user’s message to the list, including the names of attached files.
- We verify an AI model has been configured beforehand.
- In the try block, we invoke GetResponseAsync passing the prompt text and the cancellation token. We also catch possible exceptions, and add either a success message or an error message to the messages list, always resetting IsProcessing at the end of execution.
Once we apply the changes and test the application, this is the result of the execution:
In the image above, you can see that the AI model generates a response correctly.
Conclusion
Throughout this article, you have seen how the PromptBox component helps connect people with AI models. We saw its three input modes, how to customize it, and how to connect it to an AI model, thanks to Microsoft.Extensions.AI. With just a few lines of code, we achieved a professional experience that would otherwise take a long time to develop.
Try Telerik UI for Blazor
You can experiment with the PromptBox plus 120 other Telerik UI for Blazor components with the free 30-day trial.
Try Now












