# IAIChatService Interface

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

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

Assembly: Radzen.Blazor.dll

## Syntax

```csharp
public interface IAIChatService
```

## Properties

| Property | Type | Description |
|------|------|-------------|
| Store | [`IConversationStore`](https://blazor.radzen.com/docs/api/Radzen.IConversationStore.md) | Gets the store the conversations are kept in. |

## Methods

| Method | Returns | Description |
|------|---------|-------------|
| `CleanupOldSessions(int maxAgeHours)` | `void` | Removes old conversation sessions from the cache of this service instance based on age. Parameters: `maxAgeHours`: Maximum age in hours for sessions to keep. |
| `ClearSession(string sessionId)` | `void` | Clears the messages of a conversation and saves it. Parameters: `sessionId`: The session ID. |
| `ClearSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)` | `Task` | Clears the messages of a conversation and saves it. Parameters: `sessionId`: The session ID. `cancellationToken`: A cancellation token. |
| `DeleteSessionAsync(string sessionId, Threading.CancellationToken cancellationToken)` | `Task` | Deletes a conversation from the store. Parameters: `sessionId`: The session ID. `cancellationToken`: A cancellation token. |
| `GetActiveSessions()` | `IEnumerable<ConversationSession>` | Gets the conversation sessions cached by this service instance. Prefer CancellationToken), which lists the store. Returns: A list of active conversation sessions. |
| `GetChatClient(string endpoint, string proxy, string apiKey, string apiKeyHeader)` | `Microsoft.Extensions.AI.IChatClient` | Gets the IChatClient the service sends requests with: the one registered in the service collection or an [OpenAICompatibleChatClient](https://blazor.radzen.com/docs/api/Radzen.OpenAICompatibleChatClient.md) configured from [AIChatServiceOptions](https://blazor.radzen.com/docs/api/Radzen.AIChatServiceOptions.md), wrapped in a FunctionInvokingChatClient. Use it for one-off completions outside of a conversation. Parameters: `endpoint`: Optional endpoint URL to override the configured endpoint. Any override selects the built-in OpenAI-compatible client. `proxy`: Optional proxy URL to override the configured proxy. `apiKey`: Optional API key to override the configured API key. `apiKeyHeader`: Optional API key header name to override the configured header. |
| `GetCompletionsAsync(string userInput, string sessionId, Threading.CancellationToken cancellationToken, string model, string systemPrompt, double? temperature, int? maxTokens, string endpoint, string proxy, string apiKey, string apiKeyHeader)` | `IAsyncEnumerable<string>` | Streams chat completion responses from the AI model asynchronously with conversation memory. Parameters: `userInput`: The user's input message to send to the AI model. `sessionId`: Optional session ID to maintain conversation context. If null, a new session will be created. `cancellationToken`: A cancellation token that can be used to cancel the operation. `model`: Optional model name to override the configured model. `systemPrompt`: Optional system prompt to override the configured system prompt. `temperature`: Optional temperature to override the configured temperature. `maxTokens`: Optional maximum tokens to override the configured max tokens. `endpoint`: Optional endpoint URL to override the configured endpoint. `proxy`: Optional proxy URL to override the configured proxy. `apiKey`: Optional API key to override the configured API key. `apiKeyHeader`: Optional API key header name to override the configured header. Returns: An async enumerable that yields streaming response chunks from the AI model. |
| `GetEmbeddingGenerator()` | `Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>>` | Gets the IEmbeddingGenerator<T1, T2> used for semantic search and similar scenarios: the one registered in the service collection or an [OpenAICompatibleEmbeddingGenerator](https://blazor.radzen.com/docs/api/Radzen.OpenAICompatibleEmbeddingGenerator.md) configured from [AIChatServiceOptions](https://blazor.radzen.com/docs/api/Radzen.AIChatServiceOptions.md). |
| `GetOrCreateSession(string sessionId)` | [`ConversationSession`](https://blazor.radzen.com/docs/api/Radzen.ConversationSession.md) | 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. Parameters: `sessionId`: The session ID. If null, a new session will be created. |
| `GetOrCreateSessionAsync(string sessionId, string userId, Threading.CancellationToken cancellationToken)` | `Task<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. Parameters: `sessionId`: The session ID. If null, a new session will be created. `userId`: The user the session belongs to, used when it is created. `cancellationToken`: A cancellation token. |
| `GetSessionsAsync(string userId, Threading.CancellationToken cancellationToken)` | `Task<IReadOnlyList<ConversationSession>>` | Lists the conversations in the store, most recently updated first. Parameters: `userId`: When specified only the conversations of that user are returned. `cancellationToken`: A cancellation token. |
| `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)` | `IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate>` | 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. Parameters: `message`: The message to send. Usually a user message with text, or a user message carrying a ToolApprovalResponseContent. `sessionId`: Optional session ID to maintain conversation context. If null, a new session will be created. `options`: Optional chat options. ModelId, Instructions, Temperature and MaxOutputTokens fall back to the configured [AIChatServiceOptions](https://blazor.radzen.com/docs/api/Radzen.AIChatServiceOptions.md) when not set. `endpoint`: Optional endpoint URL to override the configured endpoint. Ignored when an IChatClient is registered and no override is specified. `proxy`: Optional proxy URL to override the configured proxy. `apiKey`: Optional API key to override the configured API key. `apiKeyHeader`: Optional API key header name to override the configured header. `userId`: The user the session belongs to, used when the session is created. `cancellationToken`: A cancellation token that can be used to cancel the operation. Returns: An async enumerable that yields the streaming response updates of the AI model. |
| `TryQueueMessageAsync(Microsoft.Extensions.AI.ChatMessage message, string sessionId, Threading.CancellationToken cancellationToken)` | `Task<bool>` | 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](https://blazor.radzen.com/docs/api/Radzen.AIChatService.md) forwards to the [IChatMessageQueue](https://blazor.radzen.com/docs/api/Radzen.IChatMessageQueue.md) its chat client exposes through Object). Parameters: `message`: The message. Usually a user message with text and attachments as DataContent. `sessionId`: The session whose response is streaming. `cancellationToken`: A cancellation token. |

