Interface for getting chat completions from an AI model with conversation memory.
Namespace: Radzen
Assembly: Radzen.Blazor.dll
public interface IAIChatServiceGets the store the conversations are kept in.
public IConversationStore Store { get; }
| Type | Description |
|---|---|
| IConversationStore | Gets the store the conversations are kept in. |
Removes old conversation sessions from the cache of this service instance based on age.
public abstract void CleanupOldSessions(int maxAgeHours)
| Type | Name | Description |
|---|---|---|
| int | maxAgeHours | Maximum age in hours for sessions to keep. |
Clears the messages of a conversation and saves it.
public abstract void ClearSession(string sessionId)
| Type | Name | Description |
|---|---|---|
| string | sessionId | The session ID. |
Clears the messages of a conversation and saves it.
public abstract Task ClearSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | sessionId | The session ID. |
| Threading.CancellationToken | cancellationToken | A cancellation token. |
| Type | Description |
|---|---|
| Task |
Deletes a conversation from the store.
public abstract Task DeleteSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | sessionId | The session ID. |
| Threading.CancellationToken | cancellationToken | A cancellation token. |
| Type | Description |
|---|---|
| Task |
Gets the conversation sessions cached by this service instance. Prefer CancellationToken), which lists the store.
public abstract IEnumerable<ConversationSession> GetActiveSessions()
| Type | Description |
|---|---|
| IEnumerable<ConversationSession> | A list of active conversation sessions. |
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.
public abstract Microsoft.Extensions.AI.IChatClient GetChatClient(string endpoint, string proxy, string apiKey, string apiKeyHeader)
| 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. |
| Type | Description |
|---|---|
| Microsoft.Extensions.AI.IChatClient |
Streams chat completion responses from the AI model asynchronously with conversation memory.
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)
| 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. |
| Type | Description |
|---|---|
| IAsyncEnumerable<string> | An async enumerable that yields streaming response chunks from the AI model. |
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.
public abstract Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>> GetEmbeddingGenerator()
| Type | Description |
|---|---|
| Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>> |
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.
public abstract ConversationSession GetOrCreateSession(string sessionId)
| Type | Name | Description |
|---|---|---|
| string | sessionId | The session ID. If null, a new session will be created. |
| Type | Description |
|---|---|
| ConversationSession |
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.
public abstract Task<ConversationSession> GetOrCreateSessionAsync(string sessionId, string userId, Threading.CancellationToken cancellationToken)
| 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. |
| Type | Description |
|---|---|
| Task<ConversationSession> |
Lists the conversations in the store, most recently updated first.
public abstract Task<IReadOnlyList<ConversationSession>> GetSessionsAsync(string userId, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| string | userId | When specified only the conversations of that user are returned. |
| Threading.CancellationToken | cancellationToken | A cancellation token. |
| Type | Description |
|---|---|
| Task<IReadOnlyList<ConversationSession>> |
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.
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)
| 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. |
| Type | Description |
|---|---|
| IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate> | An async enumerable that yields the streaming response updates of the AI model. |