# RadzenAIChat Class

RadzenAIChat component that provides a modern chat interface with AI integration and conversation memory.

Namespace: [Radzen.Blazor](https://blazor.radzen.com/docs/api/Radzen.Blazor.md)

Assembly: Radzen.Blazor.dll

## Inheritance

- Object
- ComponentBase
- [RadzenComponent](https://blazor.radzen.com/docs/api/Radzen.RadzenComponent.md)
- RadzenAIChat

## Implements

- IComponent
- IHandleEvent
- IHandleAfterRender

## Syntax

```csharp
public class RadzenAIChat : RadzenComponent, IComponent, IHandleEvent, IHandleAfterRender
```

## Examples

```razor
<RadzenAIChat Title="AI Assistant" Placeholder="Type your message..." @bind-Messages="@chatMessages" SessionId="@sessionId" />
```

## Constructors

| Constructor | Description |
|-------------|-------------|
| `public RadzenAIChat()` | RadzenAIChat component that provides a modern chat interface with AI integration and conversation memory. |

## Properties

| Property | Type | Description |
|------|------|-------------|
| AcceptedAttachmentTypes | `string` | Gets or sets the accepted attachment types as an accept attribute value, for example image/*,.txt,.csv,.pdf. Default is image/*. |
| AllowAttachments | `bool` | Gets or sets whether the user can attach files to messages. Images are sent to the model as image content and need a multimodal model; text files (txt, csv, md, json, xml and similar) are sent as text, PDF files as file content for providers that support it. Default is false. |
| AllowMultipleToolCalls | `bool?` | Gets or sets whether the model may call several tools in one response. null (the default) leaves it to the provider. Set to false when using tools that require approval so that each call is approved on its own. |
| ApiKey | `string` | Gets or sets the API key, overriding ApiKey. Blazor Server only: the component throws when a key is set in a WebAssembly application, because it would be visible to every user. Keep the key on the server and use Proxy. |
| ApiKeyHeader | `string` | Gets or sets the header the API key is sent with, overriding ApiKeyHeader. |
| AssistantAvatarText | `string` | Gets or sets the text displayed in the assistant avatar. |
| AttachmentAdding | `EventCallback<ChatAttachmentAddingEventArgs>` | Event callback that is invoked with every file the user picks before it is attached. Set Cancel to reject it or replace Attachment, for example with a scaled down image. Files added from code with ChatAttachment) do not raise it. |
| ChatCleared | `EventCallback` | Event callback that is invoked when the chat is cleared. |
| CitationTemplate | `RenderFragment<ChatCitation>` | Gets or sets the template used to render a source. The default renders a numbered link with the title and the snippet as tooltip. |
| DateSeparatorFormat | `string` | Gets or sets the format used to render the date separator. Defaults to "D" (long date pattern). |
| DateSeparatorTemplate | `RenderFragment<DateTime>` | Optional template to render the date separator shown between messages when the day changes. Receives the DateTime of the following message. |
| Disabled | `bool` | Gets or sets whether the chat is disabled. |
| EmptyMessage | `string` | Gets or sets the message displayed when there are no messages. |
| EmptyTemplate | `RenderFragment` | Gets or sets the empty template shown when there are no messages. |
| Endpoint | `string` | Gets or sets the endpoint URL of the AI provider, overriding Endpoint. In a WebAssembly application use Proxy instead, so the request goes through your server. |
| Error | `EventCallback<Exception>` | Event callback that is invoked when an attachment cannot be read, for example because it exceeds MaxAttachmentSize. |
| HeaderTemplate | `RenderFragment` | Gets or sets the content rendered in the chat header between Title and the clear button, for example a conversation switcher. The header is rendered when a title, the clear button or this template is set. |
| Input | `string` | Gets or sets the text of the input. Bind it with @bind-Input to read what the user has typed or to set the text from code, for example to offer a prompt. |
| InputAttributes | `IReadOnlyDictionary<string, object>` | Specifies additional custom attributes that will be rendered by the input. |
| InputChanged | `EventCallback<string>` | Event callback that is invoked when the text of the input changes. |
| InputFooterTemplate | `RenderFragment` | Gets or sets the content rendered in the input area below the text box, for example a model picker or action buttons. |
| InputHeaderTemplate | `RenderFragment` | Gets or sets the content rendered in the input area above the text box, below the pending attachments, for example a status line or a notice about the attachments. |
| MaxAttachmentSize | `long` | Gets or sets the maximum size of an attachment in bytes. Default is 4 MB. |
| MaxMessages | `int` | Gets or sets the maximum number of messages to keep in the chat. |
| MaxTokens | `int?` | Gets or sets the max tokens. |
| MaxTotalAttachmentSize | `long?` | Gets or sets the maximum total size in bytes of the attachments of a message. A file the user picks that would push the pending attachments over it is not attached and Error is raised. Checked after AttachmentAdding, on the attachment it leaves. Attachments added from code with ChatAttachment) are not checked. Default is null: no limit. |
| MessageAdded | `EventCallback<ChatMessage>` | Event callback that is invoked when a new message is added. |
| MessageFooterTemplate | `RenderFragment<ChatMessage>` | Gets or sets the template rendered at the end of every message, after its text and sources, for example the files an answer changed. |
| MessageSent | `EventCallback<string>` | Event callback that is invoked when a message is sent. |
| MessageTemplate | `RenderFragment<ChatMessage>` | Gets or sets the template that renders every message in place of the default rendering. Call ChatMessage) from it to keep the default rendering for some messages, for example to render only the entries the application adds with ChatMessage) your way, or to add content under a message. |
| Model | `string` | Gets or sets the model name. |
| PendingAttachments | `IReadOnlyList<ChatAttachment>` | Gets the attachments that will be sent with the next message. |
| Placeholder | `string` | Gets or sets the placeholder text for the input field. |
| Proxy | `string` | Gets or sets the URL of a server endpoint that forwards the requests to the AI provider and adds the API key, overriding Proxy. |
| QueueMessages | `bool` | Gets or sets whether messages sent while a response is being generated are accepted instead of ignored. Default is false. Such a message appears in the conversation right away, marked as queued, and is offered to the service with CancellationToken) as a request whose MessageId is the Id of the displayed message. A service that takes it tells the chat when the model reads it by yielding a ChatResponseUpdate with User and that id: the message then moves to the end of the conversation, loses the queued mark and the rest of the response goes into a new assistant message under it. A service that does not take the message is sent it with the next request once the response completes; Stop drops the messages waiting for that and returns their text to the input, while messages a service took stay for the service to remove with ChatMessage). The send button stays next to the stop button while the response streams. |
| QueuedMessages | `IReadOnlyList<ChatMessage>` | Gets the messages waiting to be sent once the current response completes. See QueueMessages. |
| ReadOnly | `bool` | Gets or sets whether the input is read-only. |
| ResponseReceived | `EventCallback<string>` | Event callback that is invoked when the AI response is received. |
| SendButtonIcon | `string` | Gets or sets the icon of the send button. Default is send. |
| SendButtonText | `string` | Gets or sets the text of the send button. Default is null: the button shows only its icon. |
| SessionId | `string` | Gets or sets the session ID for maintaining conversation memory. If null, a new session will be created. |
| SessionIdChanged | `EventCallback<string>` | Event callback that is invoked when a session ID is created or retrieved. |
| ShowAvatars | `bool` | Gets or sets whether the avatars of the user and the assistant are rendered next to the messages. Default is true. |
| ShowCitations | `bool` | Gets or sets whether the sources of an answer are rendered under it. Default is true. Sources are the CitationAnnotation annotations the provider attaches to the response and the [ChatCitation](https://blazor.radzen.com/docs/api/Radzen.Blazor.ChatCitation.md) instances tools return. |
| ShowClearButton | `bool` | Gets or sets whether to show the clear chat button. |
| ShowDateSeparator | `bool` | Gets or sets whether a date separator is rendered between messages when the day changes. |
| ShowReasoning | `bool` | Gets or sets whether the reasoning of models that expose it is rendered as a collapsible block in assistant messages. Default is true. |
| ShowToolCalls | `bool` | Gets or sets whether tool calls are rendered in assistant messages. Default is true. |
| ShowUsage | `bool` | Gets or sets whether the token usage reported by the model is shown next to the time of assistant messages. Default is false. |
| StopButtonIcon | `string` | Gets or sets the icon of the stop button. Default is stop_circle. |
| StopButtonText | `string` | Gets or sets the text of the stop button shown while a response streams. Default is null: the button shows only its icon. |
| SystemPrompt | `string` | Gets or sets the system prompt. |
| Temperature | `double?` | Gets or sets the temperature. |
| TimestampFormat | `string` | Gets or sets the format used to render the timestamp shown next to each message. Defaults to "HH:mm". |
| Title | `string` | Gets or sets the title displayed in the chat header. |
| ToolCallCompleted | `EventCallback<ChatToolCall>` | Event callback that is invoked when a tool call completes, fails or is rejected. |
| ToolCallTemplate | `RenderFragment<ChatToolCall>` | Gets or sets the template used to render a tool call. The template is responsible for rendering approval UI for calls in the AwaitingApproval state; call ChatToolCall) or ChatToolCall) to answer them. |
| Tools | `IEnumerable<Microsoft.Extensions.AI.AITool>` | Gets or sets the tools the model can call. Create them with AIFunctionFactory; wrap a tool in ApprovalRequiredAIFunction to ask the user before it runs. Tools are invoked automatically and rendered in the assistant message; use ToolCallTemplate to customize how. |
| UsageTemplate | `RenderFragment<ChatMessage>` | Gets or sets the template rendered next to the time of an assistant message instead of the default token count, when ShowUsage is true and the message has Usage. Use it to show what AdditionalCounts carries, for example credits. |
| UserAvatarText | `string` | Gets or sets the text displayed in the user avatar. |
| UserId | `string` | Gets or sets the user the conversation belongs to. Stored in UserId so that [IConversationStore](https://blazor.radzen.com/docs/api/Radzen.IConversationStore.md) implementations can list the conversations of a user. |

## Methods

| Method | Returns | Description |
|------|---------|-------------|
| `AddAttachment(ChatAttachment attachment)` | `void` | Adds an attachment to the next message as it is: AttachmentAdding, MaxAttachmentSize and MaxTotalAttachmentSize apply only to files the user picks. Parameters: `attachment`: The attachment. |
| `AddMessage(ChatMessage message)` | [`ChatMessage`](https://blazor.radzen.com/docs/api/Radzen.Blazor.ChatMessage.md) | Adds a message to the chat. Returns: The created message. |
| `AddMessage(string content, bool isUser)` | [`ChatMessage`](https://blazor.radzen.com/docs/api/Radzen.Blazor.ChatMessage.md) | Adds a message to the chat. Parameters: `content`: The message content. `isUser`: Whether the message is from the user. Returns: The created message. |
| `ApproveToolCall(ChatToolCall call)` | `Task` | Approves a tool call that is awaiting the user's approval. The tool is invoked and the model continues its response. When the same message holds other calls that still await approval the answer is sent once all of them are answered. Parameters: `call`: The tool call. |
| `BuildRenderTree(Rendering.RenderTreeBuilder __builder)` | `void` |  |
| `ClearAttachments()` | `void` | Removes all pending attachments. |
| `ClearChat()` | `Task` | Clears all messages from the chat. |
| `Dispose()` | `void` |  |
| `FocusAsync()` | `ValueTask` | Focuses the input. |
| `GetComponentCssClass()` | `string` |  |
| `GetMessages()` | `IReadOnlyList<ChatMessage>` | Gets the current list of messages. |
| `GetSessionId()` | `string` | Gets the current session ID. |
| `LoadConversationHistory()` | `Task` | Loads conversation history from the AI service session. |
| `LoadMessages(IEnumerable<ChatMessage> messages)` | `Task` | Loads messages into the chat, replacing any existing ones. Preserves the timestamp of each message — use this to restore conversation history. Parameters: `messages`: The messages to load. |
| `OnAfterRenderAsync(bool firstRender)` | `Task` |  |
| `OnInitializedAsync()` | `Task` |  |
| `OnParametersSetAsync()` | `Task` |  |
| `Refresh()` | `void` | Renders the chat again, for example after changing the Content of a message it shows. |
| `RejectToolCall(ChatToolCall call)` | `Task` | Rejects a tool call that is awaiting the user's approval. The model is told the call was rejected and continues its response. Parameters: `call`: The tool call. |
| `RemoveAttachment(ChatAttachment attachment)` | `void` | Removes an attachment from the next message. Parameters: `attachment`: The attachment. |
| `RemoveMessage(ChatMessage message)` | `bool` | Removes a message from the chat, for example a Progress entry that ended. The conversation sent to the model is not affected. Parameters: `message`: The message. Returns: true when the message was in the chat. |
| `RenderMessage(ChatMessage message)` | `RenderFragment` | Renders a message the way the chat does by default: the avatar, time, usage, attachments, reasoning, text and tool calls in order, sources and MessageFooterTemplate, or the status, progress or error entry for the other [ChatMessageKind](https://blazor.radzen.com/docs/api/Radzen.Blazor.ChatMessageKind.md) values. Use it in MessageTemplate for the messages you do not render yourself. Parameters: `message`: The message. |
| `SendMessage(string content)` | `Task` | Sends a message programmatically. Parameters: `content`: The message content to send. |
| `SendMessage(string content, string model, string systemPrompt, double? temperature, int? maxTokens, string endpoint, string proxy, string apiKey, string apiKeyHeader)` | `Task` | Sends a message programmatically. Parameters: `content`: The message content to send. |
| `SetParametersAsync(ParameterView parameters)` | `Task` |  |
| `Stop()` | `void` | Stops the response that is being generated. The text received so far is kept. |

## Inherited Members

- From [RadzenComponent](https://blazor.radzen.com/docs/api/Radzen.RadzenComponent.md): AddContextMenu, Attributes, ContextMenu, Culture, CurrentStyle, Debounce, DefaultCulture, DefaultUICulture, Element, GetCssClass, GetId, GetIdAttribute, IsJSRuntimeAvailable, JSRuntime, Localize, MouseEnter, MouseLeave, OnBecameInvisible, OnContextMenu, OnInitialized, OnMouseEnter, OnMouseLeave, RaiseContextMenu, RaiseMouseEnter, RaiseMouseLeave, Reference, Style, UICulture, UniqueID, Visible
- From ComponentBase: Assets, AssignedRenderMode, DispatchExceptionAsync, InvokeAsync, OnAfterRender, OnParametersSet, RendererInfo, ShouldRender, StateHasChanged

