AIChatService Class

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.

Inheritance

Object

AIChatService

Implements

Namespace: Radzen

Assembly: Radzen.Blazor.dll

Syntax

public class AIChatService : IAIChatService

Constructors

AIChatServiceLink to this section

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.

Declaration
public AIChatService(IServiceProvider serviceProvider, Microsoft.Extensions.Options.IOptions<AIChatServiceOptions> options)
Parameters
Type Name Description
IServiceProvider serviceProvider
Microsoft.Extensions.Options.IOptions<AIChatServiceOptions> options

Properties

OptionsLink to this section

Gets the configuration options for the chat streaming service.

Declaration
public AIChatServiceOptions Options { get; }
Property Value
Type Description
AIChatServiceOptionsGets the configuration options for the chat streaming service.

StoreLink to this section

Declaration
public IConversationStore Store { get; }
Property Value
Type Description
IConversationStore

Methods

ApplyCitationsLink to this section

Adds the citations a piece of response content carries to a displayed message, skipping sources the message already cites.

Declaration
public static void ApplyCitations(ChatMessage message, Microsoft.Extensions.AI.AIContent content)
Parameters
Type Name Description
ChatMessage message The displayed message.
Microsoft.Extensions.AI.AIContent content The content.

ApplyContentLink to this section

Applies a piece of response content to a displayed message: appends text and tracks tool calls, their results and approval requests.

Declaration
public static void ApplyContent(ChatMessage message, Microsoft.Extensions.AI.AIContent content, Text.StringBuilder text)
Parameters
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.

CleanupOldSessionsLink to this section

Declaration
public void CleanupOldSessions(int maxAgeHours)
Parameters
Type Name Description
int maxAgeHours

ClearSessionLink to this section

Declaration
public void ClearSession(string sessionId)
Parameters
Type Name Description
string sessionId

ClearSessionAsyncLink to this section

Declaration
public Task ClearSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string sessionId
Threading.CancellationToken cancellationToken
Returns
Type Description
Task

DeleteSessionAsyncLink to this section

Declaration
public Task DeleteSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string sessionId
Threading.CancellationToken cancellationToken
Returns
Type Description
Task

GetActiveSessionsLink to this section

Declaration
public IEnumerable<ConversationSession> GetActiveSessions()
Returns
Type Description
IEnumerable<ConversationSession>

GetChatClientLink to this section

Declaration
public Microsoft.Extensions.AI.IChatClient GetChatClient(string endpoint, string proxy, string apiKey, string apiKeyHeader)
Parameters
Type Name Description
string endpoint
string proxy
string apiKey
string apiKeyHeader
Returns
Type Description
Microsoft.Extensions.AI.IChatClient

GetCompletionsAsyncLink to this section

Declaration
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)
Parameters
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
Returns
Type Description
IAsyncEnumerable<string>

GetEmbeddingGeneratorLink to this section

Declaration
public Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>> GetEmbeddingGenerator()
Returns
Type Description
Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>>

GetOrCreateSessionLink to this section

Declaration
public ConversationSession GetOrCreateSession(string sessionId)
Parameters
Type Name Description
string sessionId
Returns
Type Description
ConversationSession

GetOrCreateSessionAsyncLink to this section

Declaration
public Task<ConversationSession> GetOrCreateSessionAsync(string sessionId, string userId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string sessionId
string userId
Threading.CancellationToken cancellationToken
Returns
Type Description
Task<ConversationSession>

GetSessionsAsyncLink to this section

Declaration
public Task<IReadOnlyList<ConversationSession>> GetSessionsAsync(string userId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string userId
Threading.CancellationToken cancellationToken
Returns
Type Description
Task<IReadOnlyList<ConversationSession>>

GetStreamingResponseAsyncLink to this section

Declaration
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)
Parameters
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
Returns
Type Description
IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate>

ToDisplayMessageLink to this section

Builds the ChatMessage shown for a model response: its text and the tool calls it made.

Declaration
public static ChatMessage ToDisplayMessage(Microsoft.Extensions.AI.ChatResponse response)
Parameters
Type Name Description
Microsoft.Extensions.AI.ChatResponse response The response.
Returns
Type Description
ChatMessageThe message, or null when the response has neither text nor tool calls.

TryQueueMessageAsyncLink to this section

Declaration
public virtual Task<bool> TryQueueMessageAsync(Microsoft.Extensions.AI.ChatMessage message, string sessionId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
Microsoft.Extensions.AI.ChatMessage message
string sessionId
Threading.CancellationToken cancellationToken
Returns
Type Description
Task<bool>
An error has occurred. This app may no longer respond until reloaded. Reload 🗙