Service for interacting with AI chat models to get completions with conversation memory. Uses the IChatClient registered in the service collection, or an OpenAICompatibleChatClient configured from AIChatServiceOptions when none is registered. Tools passed via Tools are invoked automatically with a FunctionInvokingChatClient.
Object
Namespace: Radzen
Assembly: Radzen.Blazor.dll
public class AIChatService : IAIChatServiceService for interacting with AI chat models to get completions with conversation memory. Uses the IChatClient registered in the service collection, or an OpenAICompatibleChatClient configured from AIChatServiceOptions when none is registered. Tools passed via Tools are invoked automatically with a FunctionInvokingChatClient.
public AIChatService(IServiceProvider serviceProvider, Microsoft.Extensions.Options.IOptions<AIChatServiceOptions> options)
| Type | Name | Description |
|---|---|---|
| IServiceProvider | serviceProvider | |
| Microsoft.Extensions.Options.IOptions<AIChatServiceOptions> | options |
Gets the configuration options for the chat streaming service.
public AIChatServiceOptions Options { get; }
| Type | Description |
|---|---|
| AIChatServiceOptions | Gets the configuration options for the chat streaming service. |
public IConversationStore Store { get; }
| Type | Description |
|---|---|
| IConversationStore |
Adds the citations a piece of response content carries to a displayed message, skipping sources the message already cites.
public static void ApplyCitations(ChatMessage message, Microsoft.Extensions.AI.AIContent content)
| Type | Name | Description |
|---|---|---|
| ChatMessage | message | The displayed message. |
| Microsoft.Extensions.AI.AIContent | content | The content. |
Applies a piece of response content to a displayed message: appends text and tracks tool calls, their results and approval requests.
public static void ApplyContent(ChatMessage message, Microsoft.Extensions.AI.AIContent content, Text.StringBuilder text)
| Type | Name | Description |
|---|---|---|
| ChatMessage | message | The displayed message. |
| Microsoft.Extensions.AI.AIContent | content | The content. |
| Text.StringBuilder | text | The builder that accumulates the message text. |
public void CleanupOldSessions(int maxAgeHours)
| Type | Name | Description |
|---|---|---|
| int | maxAgeHours |
public void ClearSession(string sessionId)
| Type | Name | Description |
|---|---|---|
| string | sessionId |
public Task ClearSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | sessionId | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| Task |
public Task DeleteSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | sessionId | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| Task |
public IEnumerable<ConversationSession> GetActiveSessions()
| Type | Description |
|---|---|
| IEnumerable<ConversationSession> |
public Microsoft.Extensions.AI.IChatClient GetChatClient(string endpoint, string proxy, string apiKey, string apiKeyHeader)
| Type | Name | Description |
|---|---|---|
| string | endpoint | |
| string | proxy | |
| string | apiKey | |
| string | apiKeyHeader |
| Type | Description |
|---|---|
| Microsoft.Extensions.AI.IChatClient |
public IAsyncEnumerable<string> GetCompletionsAsync(string userInput, string sessionId, Threading.CancellationToken cancellationToken, string model, string systemPrompt, double? temperature, int? maxTokens, string endpoint, string proxy, string apiKey, string apiKeyHeader)
| Type | Name | Description |
|---|---|---|
| string | userInput | |
| string | sessionId | |
| Threading.CancellationToken | cancellationToken | |
| string | model | |
| string | systemPrompt | |
| double? | temperature | |
| int? | maxTokens | |
| string | endpoint | |
| string | proxy | |
| string | apiKey | |
| string | apiKeyHeader |
| Type | Description |
|---|---|
| IAsyncEnumerable<string> |
public Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>> GetEmbeddingGenerator()
| Type | Description |
|---|---|
| Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>> |
public ConversationSession GetOrCreateSession(string sessionId)
| Type | Name | Description |
|---|---|---|
| string | sessionId |
| Type | Description |
|---|---|
| ConversationSession |
public Task<ConversationSession> GetOrCreateSessionAsync(string sessionId, string userId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | sessionId | |
| string | userId | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| Task<ConversationSession> |
public Task<IReadOnlyList<ConversationSession>> GetSessionsAsync(string userId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | userId | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| Task<IReadOnlyList<ConversationSession>> |
public IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate> GetStreamingResponseAsync(Microsoft.Extensions.AI.ChatMessage message, string sessionId, Microsoft.Extensions.AI.ChatOptions options, string endpoint, string proxy, string apiKey, string apiKeyHeader, string userId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| Microsoft.Extensions.AI.ChatMessage | message | |
| string | sessionId | |
| Microsoft.Extensions.AI.ChatOptions | options | |
| string | endpoint | |
| string | proxy | |
| string | apiKey | |
| string | apiKeyHeader | |
| string | userId | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate> |
Builds the ChatMessage shown for a model response: its text and the tool calls it made.
public static ChatMessage ToDisplayMessage(Microsoft.Extensions.AI.ChatResponse response)
| Type | Name | Description |
|---|---|---|
| Microsoft.Extensions.AI.ChatResponse | response | The response. |
| Type | Description |
|---|---|
| ChatMessage | The message, or null when the response has neither text nor tool calls. |
public virtual Task<bool> TryQueueMessageAsync(Microsoft.Extensions.AI.ChatMessage message, string sessionId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| Microsoft.Extensions.AI.ChatMessage | message | |
| string | sessionId | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| Task<bool> |