IAIChatService Interface

Interface for getting chat completions from an AI model with conversation memory.

Namespace: Radzen

Assembly: Radzen.Blazor.dll

Syntax

public interface IAIChatService

Properties

StoreLink to this section

Gets the store the conversations are kept in.

Declaration
public IConversationStore Store { get; }
Property Value
Type Description
IConversationStoreGets the store the conversations are kept in.

Methods

CleanupOldSessionsLink to this section

Removes old conversation sessions from the cache of this service instance based on age.

Declaration
public abstract void CleanupOldSessions(int maxAgeHours)
Parameters
Type Name Description
int maxAgeHours Maximum age in hours for sessions to keep.

ClearSessionLink to this section

Clears the messages of a conversation and saves it.

Declaration
public abstract void ClearSession(string sessionId)
Parameters
Type Name Description
string sessionId The session ID.

ClearSessionAsyncLink to this section

Clears the messages of a conversation and saves it.

Declaration
public abstract Task ClearSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string sessionId The session ID.
Threading.CancellationToken cancellationToken A cancellation token.
Returns
Type Description
Task

DeleteSessionAsyncLink to this section

Deletes a conversation from the store.

Declaration
public abstract Task DeleteSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string sessionId The session ID.
Threading.CancellationToken cancellationToken A cancellation token.
Returns
Type Description
Task

GetActiveSessionsLink to this section

Gets the conversation sessions cached by this service instance. Prefer CancellationToken), which lists the store.

Declaration
public abstract IEnumerable<ConversationSession> GetActiveSessions()
Returns
Type Description
IEnumerable<ConversationSession>A list of active conversation sessions.

GetChatClientLink to this section

Gets the IChatClient the service sends requests with: the one registered in the service collection or an OpenAICompatibleChatClient configured from AIChatServiceOptions, wrapped in a FunctionInvokingChatClient. Use it for one-off completions outside of a conversation.

Declaration
public abstract Microsoft.Extensions.AI.IChatClient GetChatClient(string endpoint, string proxy, string apiKey, string apiKeyHeader)
Parameters
Type Name Description
string endpoint Optional endpoint URL to override the configured endpoint. Any override selects the built-in OpenAI-compatible client.
string proxy Optional proxy URL to override the configured proxy.
string apiKey Optional API key to override the configured API key.
string apiKeyHeader Optional API key header name to override the configured header.
Returns
Type Description
Microsoft.Extensions.AI.IChatClient

GetCompletionsAsyncLink to this section

Streams chat completion responses from the AI model asynchronously with conversation memory.

Declaration
public abstract 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 The user's input message to send to the AI model.
string sessionId Optional session ID to maintain conversation context. If null, a new session will be created.
Threading.CancellationToken cancellationToken A cancellation token that can be used to cancel the operation.
string model Optional model name to override the configured model.
string systemPrompt Optional system prompt to override the configured system prompt.
double? temperature Optional temperature to override the configured temperature.
int? maxTokens Optional maximum tokens to override the configured max tokens.
string endpoint Optional endpoint URL to override the configured endpoint.
string proxy Optional proxy URL to override the configured proxy.
string apiKey Optional API key to override the configured API key.
string apiKeyHeader Optional API key header name to override the configured header.
Returns
Type Description
IAsyncEnumerable<string>An async enumerable that yields streaming response chunks from the AI model.

GetEmbeddingGeneratorLink to this section

Gets the IEmbeddingGenerator<T1, T2> used for semantic search and similar scenarios: the one registered in the service collection or an OpenAICompatibleEmbeddingGenerator configured from AIChatServiceOptions.

Declaration
public abstract 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

Gets a conversation session from the store, or creates it. The session is cached for the lifetime of the service and saved to the store after every response.

Declaration
public abstract ConversationSession GetOrCreateSession(string sessionId)
Parameters
Type Name Description
string sessionId The session ID. If null, a new session will be created.
Returns
Type Description
ConversationSession

GetOrCreateSessionAsyncLink to this section

Gets a conversation session from the store, or creates it. The session is cached for the lifetime of the service and saved to the store after every response.

Declaration
public abstract Task<ConversationSession> GetOrCreateSessionAsync(string sessionId, string userId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string sessionId The session ID. If null, a new session will be created.
string userId The user the session belongs to, used when it is created.
Threading.CancellationToken cancellationToken A cancellation token.
Returns
Type Description
Task<ConversationSession>

GetSessionsAsyncLink to this section

Lists the conversations in the store, most recently updated first.

Declaration
public abstract Task<IReadOnlyList<ConversationSession>> GetSessionsAsync(string userId, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
string userId When specified only the conversations of that user are returned.
Threading.CancellationToken cancellationToken A cancellation token.
Returns
Type Description
Task<IReadOnlyList<ConversationSession>>

GetStreamingResponseAsyncLink to this section

Streams the response of the AI model to a message with conversation memory. Tools in Tools are invoked automatically; their calls and results are yielded as FunctionCallContent and FunctionResultContent and a tool wrapped in ApprovalRequiredAIFunction yields a ToolApprovalRequestContent that must be answered with a message carrying a ToolApprovalResponseContent.

Declaration
public abstract 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 The message to send. Usually a user message with text, or a user message carrying a ToolApprovalResponseContent.
string sessionId Optional session ID to maintain conversation context. If null, a new session will be created.
Microsoft.Extensions.AI.ChatOptions options Optional chat options. ModelId, Instructions, Temperature and MaxOutputTokens fall back to the configured AIChatServiceOptions when not set.
string endpoint Optional endpoint URL to override the configured endpoint. Ignored when an IChatClient is registered and no override is specified.
string proxy Optional proxy URL to override the configured proxy.
string apiKey Optional API key to override the configured API key.
string apiKeyHeader Optional API key header name to override the configured header.
string userId The user the session belongs to, used when the session is created.
Threading.CancellationToken cancellationToken A cancellation token that can be used to cancel the operation.
Returns
Type Description
IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate>An async enumerable that yields the streaming response updates of the AI model.

TryQueueMessageAsyncLink to this section

Offers the service a message the user sent while a response of the session is still streaming, when QueueMessages is true. Return true to take the message: the service gives it to the model as part of the running response, for example once the tool calls in progress finish, and the chat shows it as sent. The default returns false: the chat keeps the message and sends it with CancellationToken) once the response completes. AIChatService forwards to the IChatMessageQueue its chat client exposes through Object).

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 The message. Usually a user message with text and attachments as DataContent.
string sessionId The session whose response is streaming.
Threading.CancellationToken cancellationToken A cancellation token.
Returns
Type Description
Task<bool>
In This Article
An error has occurred. This app may no longer respond until reloaded. Reload 🗙