An IChatClient for OpenAI-compatible chat completion endpoints (OpenAI, Azure OpenAI, Ollama, Cloudflare Workers AI, LM Studio and others). Supports streaming responses and tool calling. Pair it with FunctionInvokingChatClient to invoke AIFunction tools automatically.
Object
Microsoft.Extensions.AI.IChatClient
Namespace: Radzen
Assembly: Radzen.Blazor.dll
public sealed class OpenAICompatibleChatClient : Microsoft.Extensions.AI.IChatClientbuilder.Services.AddChatClient(services =>
new OpenAICompatibleChatClient(services.GetRequiredService<HttpClient>(), "https://api.openai.com/v1/chat/completions", apiKey, defaultModel: "gpt-4o-mini"))
.UseFunctionInvocation();Initializes a new instance of the OpenAICompatibleChatClient class.
public OpenAICompatibleChatClient(Net.Http.HttpClient httpClient, string endpoint, string apiKey, string apiKeyHeader, string defaultModel)
| Type | Name | Description |
|---|---|---|
| Net.Http.HttpClient | httpClient | The HttpClient used to send requests. It is not disposed by the client. |
| string | endpoint | The chat completions URL, for example https://api.openai.com/v1/chat/completions. A relative URL is resolved against the BaseAddress. |
| string | apiKey | The API key. When null or empty no authentication header is sent. |
| string | apiKeyHeader | The header that carries the API key. Authorization (the default) sends it as a bearer token. |
| string | defaultModel | The model used when ModelId is not set. |
Gets the metadata that describes the client.
public Microsoft.Extensions.AI.ChatClientMetadata Metadata { get; }
| Type | Description |
|---|---|
| Microsoft.Extensions.AI.ChatClientMetadata | Gets the metadata that describes the client. |
public Task<Microsoft.Extensions.AI.ChatResponse> GetResponseAsync(IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages, Microsoft.Extensions.AI.ChatOptions options, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| IEnumerable<Microsoft.Extensions.AI.ChatMessage> | messages | |
| Microsoft.Extensions.AI.ChatOptions | options | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| Task<Microsoft.Extensions.AI.ChatResponse> |
public object GetService(Type serviceType, object serviceKey)
| Type | Name | Description |
|---|---|---|
| Type | serviceType | |
| object | serviceKey |
| Type | Description |
|---|---|
| object |
public IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages, Microsoft.Extensions.AI.ChatOptions options, Threading.CancellationToken cancellationToken)
| Type | Name | Description |
|---|---|---|
| IEnumerable<Microsoft.Extensions.AI.ChatMessage> | messages | |
| Microsoft.Extensions.AI.ChatOptions | options | |
| Threading.CancellationToken | cancellationToken |
| Type | Description |
|---|---|
| IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate> |