Skip to content

DevExpress-Examples/ai-powered-report-generation-in-console

Repository files navigation

DevExpress Reports - Generate a Report Based on a User Prompt Within a Console App

This example integrates multi-agent report generation for a .NET 8 console application using Azure OpenAI.

Console - Report Generation

The generated report is saved to the generatedReport.repx file.

Generated Report Layout

Prerequisites

Implementation Details

To implement report generation in your application, you must:

  1. Create an IChatClient instance for your AI provider:

    // Retrieve the Azure OpenAI endpoint, key, and model from user environment variables.
    string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT", EnvironmentVariableTarget.User)
        ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
    string apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY", EnvironmentVariableTarget.User)
        ?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY is not set.");
    string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT", EnvironmentVariableTarget.User)
        ?? "gpt-5-mini";
    // Create an Azure OpenAI client to work with the chat agent.
    IChatClient chatClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey))
        .GetChatClient(deploymentName)
        .AsIChatClient();

    For additional information, refer to the following help topic: AI-powered Extensions — Register AI Clients.

  2. Initialize an AI extensions container to register the AI client and reporting extension:

    using DevExpress.AIIntegration.Reporting.Common.Extensions;
    using DevExpress.AIIntegration;
    //
    AIExtensionsContainerDefault container = AIExtensionsContainerConsole.CreateDefaultAIExtensionContainer(chatClient);
    container.RegisterReportingExtensions();
  3. Create a class that implements IAIReportGenerationHost to handle clarification questions and to show progress/notifications:

    Implement and supply a host for interactive workflows (ConsoleAIReportGenerationHost.cs).

    namespace Reporting.Generation.Console {
        public class ConsoleAIReportGenerationHost : IAIReportGenerationHost {
            private string lastStatus = string.Empty;
    
            public Task<PromptClarificationAnswer> ClarifyPromptAsync(PromptClarificationQuestion request) {
                // Render the request in your UI (request.Text and request.Choices).
                // Return PromptClarificationAnswer.FromValue(selectedChoice).
    
                // Return PromptClarificationAnswer.Canceled() if the user cancels the operation.
                return Task.FromResult(PromptClarificationAnswer.Canceled());
            }
    
            public void NotifyAsync(string status, string reasoning) {
                bool isNewStatus = lastStatus != status;
                lastStatus = status;
    
                // Surface progress to users (console, logger, status bar, web socket, etc.).
                // Update the status when isNewStatus is true; otherwise refresh the reasoning only.
            }
        }
    }
  4. Generate the report from a prompt.

    In the Program.cs file, create a PromptToReportRequest instance with the user prompt, assign the host, and specify additional settings. Call AIReportingIntegration.GeneratePromptToReportAsync to obtain an XtraReport instance.

    using Reporting.Generation.Console;
    using DevExpress.AIIntegration.Reporting.Common.Extensions;
    using DevExpress.XtraReports.UI;
    // ...
    // Ask the user for a report description in natural language.
    Console.WriteLine("Specify a prompt to generate the report:");
    string prompt = Console.ReadLine();
    try {
        // Create a host to handle clarification questions and progress notifications.
        ConsoleAIReportGenerationHost host = new ConsoleAIReportGenerationHost();
        // Build a generation request from the user prompt.
        PromptToReportRequest generationRequest = new PromptToReportRequest(userPrompt: prompt, dataSourceSchema: null, report: null) {
            ReportGenerationHost = host,
            FixLayoutErrors = true
        };
        // Generate a report layout and save it to a REPX file.
        XtraReport report = await container.GeneratePromptToReportAsync(generationRequest, default);
        report.SaveLayoutToXml("generatedReport.repx");
    } catch (Exception ex) {
        Console.WriteLine($"Report generation failed: {ex.Message}");
    }

Files to Review

Documentation

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Generate a report based on the console user input.

Topics

Resources

License

Stars

Watchers

Forks

Contributors