Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V25.2
  • 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 distinguish them by a unique key.

    using System;
    using System.ClientModel;
    using System.Windows.Forms;
    using OllamaSharp;
    using Microsoft.Extensions.AI;
    using Microsoft.Extensions.DependencyInjection;
    using DevExpress.AIIntegration;
    
    namespace DXWinFormsChatApp {
        internal static class Program {
            [STAThread]
            static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                // Create an Azure OpenAI client.
                var azureOpenAiClient = new Azure.AI.OpenAI.AzureOpenAIClient(AzureOpenAIEndpoint, AzureOpenAIKey);
    
                // Create an Ollama API client.
                IChatClient ollamaClient = new OllamaApiClient(new Uri("http://localhost:11434/"), "MODEL_NAME");
    
                // Create a service collection to register AI chat clients.
                var serviceCollection = new ServiceCollection();
    
                // Register the Azure OpenAI client with a unique key.
                serviceCollection.AddKeyedChatClient("azureOpenAIClient", azureOpenAiClient.GetChatClient(ModelId).AsIChatClient());
    
                // Register the Ollama client with a different key.
                serviceCollection.AddKeyedChatClient("ollamaClient", ollamaClient);
    
                // 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 ChatClientServiceKey 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() {
                    ChatClientServiceKey = "azureOpenAIClient", // Connect to the Azure OpenAI service.
                    Dock = DockStyle.Right
                };
    
                // Create and configure an Ollama chat control.
                ollamaChat = new AIChatControl() {
                    ChatClientServiceKey = "ollamaClient", // Connect to the Ollama service.
                    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 ChatClientServiceKey property to switch between AI services or AI agents.

    Troubleshooting

    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 ChatClientServiceKey
    Ensure that the AIChatControl.ChatClientServiceKey property matches the key of one of the chat clients you registered in the service collection.
    Register a Default Chat Client

    If you do not assign a ChatClientServiceKey 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