Back to Devexpress

PromptToExpressionExtension Class

corelibraries-devexpress-dot-aiintegration-dot-extensions-44aa9822.md

latest5.5 KB
Original Source

PromptToExpressionExtension Class

An AI-powered extension that converts natural language into a valid DevExpress filter or expression.

Namespace : DevExpress.AIIntegration.Extensions

Assembly : DevExpress.AIIntegration.v25.2.dll

NuGet Package : DevExpress.AIIntegration

Declaration

csharp
public class PromptToExpressionExtension :
    PromptToExpressionExtensionBase<PromptToExpressionRequest>
vb
Public Class PromptToExpressionExtension
    Inherits PromptToExpressionExtensionBase(Of PromptToExpressionRequest)

Remarks

The PromptToExpressionExtension class converts natural-language input into valid DevExpress expressions. It allows users to describe filtering or calculation logic in plain text and receive a syntactically correct expression that can be applied to filters, calculated fields, or unbound columns.

The extension sends a PromptToExpressionRequest to the registered AI service and returns an expression that conforms to the DevExpress expression language.

The AI service analyzes:

  • The user prompt
  • The current expression
  • Available columns
  • Supported functions, parameters, and constants

The following code snippet registers an Azure OpenAI client to use the AI-powered Prompt to Expression extension in a .NET Console application to generate a valid filter expression:

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

// Register an Azure OpenAI client.
AIExtensionsContainerDefault defaultAIExtensionsContainer = RegisterAzureOpenAIClient(
    Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"),
    Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY")
);

// PromptToExpressionAsync converts a human-readable request into
// a machine-readable expression based on the available columns and functions.
var response = await defaultAIExtensionsContainer.PromptToExpressionAsync(
    new PromptToExpressionRequest(
        "Display orders from the last month where the total amount is greater than $500.", // The user's instruction written in natural language.
        string.Empty,
        new List<PromptToExpressionRequestBase.ColumnInfo>(){ // Describes data fields that the AI is allowed
            new PromptToExpressionRequestBase.ColumnInfo(){ ColumnName = "OrderID" }, // to reference in the generated expression.
            new PromptToExpressionRequestBase.ColumnInfo(){ ColumnName = "Price" },
            new PromptToExpressionRequestBase.ColumnInfo(){ ColumnName = "OrderDate"}
        },
        null) // (optional) Specifies custom or supported functions
                                                                                            // that the AI can use when building expressions.
);

Console.WriteLine("Generated Expression: " + response);

// Expected output (the actual output may vary):
// Generated Expression: [OrderDate] >= DateTime.Now.AddMonths(-1) && [Price] > 500

AIExtensionsContainerDefault RegisterAzureOpenAIClient(string azureOpenAIEndpoint, string azureOpenAIKey) {
    IChatClient client = new AzureOpenAIClient(new Uri(azureOpenAIEndpoint),
        new System.ClientModel.ApiKeyCredential(azureOpenAIKey)).GetChatClient("gpt-4o-mini").AsIChatClient();

    return AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(client);
}

void SetEnvironmentVariables() {
    Environment.SetEnvironmentVariable("AZURE_OPENAI_ENDPOINT", {SPECIFY_YOUR_AZURE_ENDPOINT});
    Environment.SetEnvironmentVariable("AZURE_OPENAI_APIKEY", {SPECIFY_YOU_AZURE_KEY});
}

Implements

IAIExtension

Inheritance

Object DevExpress.AIIntegration.Extensions.AIExtensionBase<PromptToExpressionRequest, DevExpress.AIIntegration.Extensions.PromptToExpressionResponse> DevExpress.AIIntegration.Extensions.ChatAIExtensionBase<PromptToExpressionRequest, DevExpress.AIIntegration.Extensions.PromptToExpressionResponse> DevExpress.AIIntegration.Extensions.PromptToExpressionExtensionBase<PromptToExpressionRequest> PromptToExpressionExtension

See Also

AI Integration

Prompt to Expression (WinForms)

Generate Expressions from Prompts (WinForms Report Designer)

Generate Expressions From Prompts (Web Report Designer)

PromptToExpressionExtension Members

DevExpress.AIIntegration.Extensions Namespace