Back to Devexpress

Smart Autocomplete

windowsforms-405208-ai-powered-extensions-smart-auto-complete.md

latest4.9 KB
Original Source

Smart Autocomplete

  • Jan 13, 2025
  • 3 minutes to read

The AI-powered “Smart Autocomplete” feature intelligently predicts and suggests words or phrases based on the user’s current input.

Applies To

MemoEdit

How It Works

DevExpress UI controls seamlessly integrate Smart Autocomplete. When Smart Autocomplete is activated, as you type, the AI model analyzes the context of the text and makes relevant suggestions in real time. Press Tab or click the suggestion to append it to the text. Press the Esc key to hide the suggestion.

Activate Smart Autocomplete

Install DevExpress NuGet Packages

  1. DevExpress.AIIntegration.WinForms
  2. DevExpress.Win.Design (enables design-time features for DevExpress UI controls)

Read the following help topics for information on how to obtain the DevExpress NuGet Feed and install DevExpress NuGet packages:

Register AI Client

See the following help topic for information on required NuGet packages and system requirements: Register an AI Client.

The following code snippet registers the Azure OpenAI client:

csharp
using Microsoft.Extensions.AI;
using DevExpress.AIIntegration;

internal static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        IChatClient azureChatClient = new Azure.AI.OpenAI.AzureOpenAIClient(new Uri(AzureOpenAIEndpoint),
            new System.ClientModel.ApiKeyCredential(AzureOpenAIKey))
            .GetChatClient(ModelId).AsIChatClient();
        AIExtensionsContainerDesktop.Default.RegisterChatClient(azureChatClient);
        // Uncomment the following line if your project targets the .NET Framework and
        // you create AI-powered behaviors in code.
        // DevExpress.AIIntegration.WinForms.BehaviorInitializer.Initialize();
        Application.Run(new Form1());
    }
    static string AzureOpenAIEndpoint { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); } }
    static string AzureOpenAIKey { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY"); } }
    static string ModelId { get { return "MODEL_NAME"; } }
}
vb
Imports Microsoft.Extensions.AI
Imports DevExpress.AIIntegration

Friend Module Program
    <STAThread>
    Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        ' Register Azure.OpenAI
        Dim azureChatClient As IChatClient = New Azure.AI.OpenAI.AzureOpenAIClient(New Uri(AzureOpenAIEndpoint),
            New System.ClientModel.ApiKeyCredential(AzureOpenAIKey)).
            GetChatClient(ModelId).AsIChatClient()
        AIExtensionsContainerDesktop.Default.RegisterChatClient(azureChatClient)
        ' Uncomment the following line if your project targets the .NET Framework and
        ' you create AI-powered behaviors in code.
        ' DevExpress.AIIntegration.WinForms.BehaviorInitializer.Initialize()
        Application.Run(New Form1())
    End Sub
    Private ReadOnly Property AzureOpenAIEndpoint() As String
        Get
            Return Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
        End Get
    End Property
    Private ReadOnly Property AzureOpenAIKey() As String
        Get
            Return Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY")
        End Get
    End Property
    Private ReadOnly Property ModelId As String
        Get
            Return "MODEL_NAME"
        End Get
    End Property
End Module

Create and Configure Smart Autocomplete Behavior

  1. Drop the BehaviorManager component from the Toolbox onto a Form. The Form should contain the DevExpress MemoEdit.
  2. Add a SmartAutoCompleteBehavior, configure the TypingPauseDelay setting if needed, and attach the behavior to the MemoEdit. You can do this at design time or in code:

Tip

Use SmartAutoCompleteRequest to manually initiate a request to complete the text:

csharp
var response = await defaultAIExtensionsContainer.SmartAutoCompleteAsync(
    new SmartAutoCompleteRequest(startText)
);

See Also

DevExpress AI-powered Extensions for WinForms