Skip to main content
All docs
V26.1
  • Manage Multiple Chat Clients

    • 3 minutes to read

    The WinForms AIChatControl supports multiple AI services in a single application, which enables you to compare AI providers, separate conversation contexts, and provide specialized AI assistants for different user tasks.

    Register Multiple AI Services

    Register multiple AI services and service providers, and distinguish them by a unique key.

    using System;
    using System.ClientModel;
    using System.Windows.Forms;
    using Azure.AI.OpenAI;
    using OllamaSharp;
    using Microsoft.Extensions.AI;
    using Microsoft.Extensions.DependencyInjection;
    using DevExpress.AIIntegration;
    using DevExpress.AIIntegration.Chat;
    
    namespace DXWinFormsChatApp {
        internal static class Program {
            [STAThread]
            static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                // Create an Azure OpenAI chat client.
                var azureOpenAiClient = new AzureOpenAIClient(AzureOpenAIEndpoint, AzureOpenAIKey)
                    .GetChatClient(ModelId)
                    .AsIChatClient();
    
                // Create an Ollama API client.
                IChatClient ollamaClient = new OllamaApiClient(
                    new Uri("http://localhost:11434/"),
                    "gemma3:4b"
                );
    
                // Create a service collection to register AI chat clients.
                var serviceCollection = new ServiceCollection();
    
                // Register a keyed IChatResponseProvider for Azure OpenAI.
                // This provider is resolved when "azureAiProviderServiceKey" is specified.
                serviceCollection.AddKeyedScoped<IChatResponseProvider>(
                    "azureAiProviderServiceKey",
                    (serviceProvider, _) => azureOpenAiClient.AsIChatResponseProvider()
                );
    
                // Register the Azure OpenAI chat client as a keyed IChatClient.
                serviceCollection.AddKeyedScoped<IChatClient>(
                    "azureAiClient",
                    (serviceProvider, _) => azureOpenAiClient
                );
    
                // Register a keyed IChatResponseProvider for the Ollama client.
                // This provider is resolved when "ollamaAiProviderServiceKey" is specified.
                serviceCollection.AddKeyedScoped<IChatResponseProvider>(
                    "ollamaAiProviderServiceKey",
                    (serviceProvider, _) => ollamaClient.AsIChatResponseProvider()
                );
    
                // Register the Ollama chat client as a keyed IChatClient.
                serviceCollection.AddKeyedScoped<IChatClient>(
                    "ollamaAiClient",
                    (serviceProvider, _) => ollamaClient
                );
    
                // Register a default (non-keyed) IChatResponseProvider.
                // This provider is used when no ChatResponseProviderServiceKey is specified.
                serviceCollection.AddScoped<IChatResponseProvider>(
                    (serviceProvider) => azureOpenAiClient.AsIChatResponseProvider()
                );
    
                // Add DevExpress AI Desktop services (required for AIChatControl).
                serviceCollection.AddDevExpressAIDesktop();
    
                Application.Run(new Form1());
            }
            private static Uri AzureOpenAIEndpoint = new Uri("YOUR_AZURE_END_POINT");
            private static ApiKeyCredential AzureOpenAIKey = new ApiKeyCredential("YOUR_OPENAI_API_KEY");
            static string ModelId = "MODEL_NAME"; // For example, gpt-4o-mini
        }
    }
    

    Run Independent AI Chats Side by Side

    Place several AIChatControl instances on a form and specify a different ChatResponseProviderServiceKey for each chat control to interact with different AI services.

    using DevExpress.AIIntegration.WinForms.Chat;
    using DevExpress.XtraEditors;
    using System.Windows.Forms;
    
    namespace DXWinFormsChatApp {
        public partial class Form1 : XtraForm {
            AIChatControl azureChat, ollamaChat;
            public Form1() {
                InitializeComponent();
    
                // Create and configure an Azure OpenAI chat control.
                azureChat = new AIChatControl() {
                    ChatResponseProviderServiceKey = "azureAiProviderServiceKey",
                    Dock = DockStyle.Right
                };
    
                // Create and configure an Ollama chat control.
                ollamaChat = new AIChatControl() {
                    ChatResponseProviderServiceKey = "ollamaAiProviderServiceKey",
                    Dock = DockStyle.Left
                };
    
                this.Controls.AddRange(new Control[] { azureChat, ollamaChat });
            }
        }
    }
    

    The following animation illustrates the result:

    Multiple Chat Clients in a WinForms Application, DevExpress

    Switch Between AI Services or AI Agents

    Use a single AIChatControl and modify its ChatResponseProviderServiceKey property to switch between AI services or AI agents.

    Troubleshooting

    The IChatResponseProvider service is not registered

    Add IChatResponseProvider to the service collection at application startup:

    // Create an Azure OpenAI chat client.
    var azureOpenAiClient = new AzureOpenAIClient(AzureOpenAIEndpoint, AzureOpenAIKey)
        .GetChatClient(ModelId)
        .AsIChatClient();
    // Register a keyed IChatResponseProvider.
    serviceCollection.AddKeyedScoped<IChatResponseProvider>(
        "azureAiProviderServiceKey",
        (serviceProvider, _) => azureOpenAiClient.AsIChatResponseProvider()
    );
    // Register the Azure OpenAI chat client as a keyed IChatClient.
    serviceCollection.AddKeyedScoped<IChatClient>(
        "azureAiClient",
        (serviceProvider, _) => azureOpenAiClient
    );
    

    There is no registered service of type Microsoft.Extensions.AI.IChatClient

    This error occurs when the AIChatControl cannot find a registered chat client that matches its configuration.

    Do one of the following:

    Check ChatResponseProviderServiceKey
    Ensure that the AIChatControl.ChatResponseProviderServiceKey property matches the key of one of the chat client providers you registered in the service collection.
    Register a Default Chat Client

    If you do not assign a ChatResponseProviderServiceKey to the chat control, register a default chat client in the desktop container to ensure that the chat control can automatically resolve the AI service:

    AIExtensionsContainerDesktop.Default.RegisterChatClient(chatClient);
    

    See the following help topic for more information: Register AI Client.

    See Also