Skip to content

Structured Output

Step 6 of 8

Overview

This section focuses on converting AI-generated text into structured formats usable within Business Central. Properly processing and presenting AI responses is essential for integrating them into practical business workflows.

Objective

Learn how to transform unstructured text output from Azure OpenAI into structured records and tables that are actionable and visible in the Copilot UI (PromptDialog).

Importance

AI always provides responses in plain text. Developers must interpret this text and convert it into structured data that fits within the application's database structures. This conversion is crucial for displaying AI-generated solutions in a user-friendly manner.

Implementation Steps

1. Response Understanding

The AI delivers responses in plain text based on the provided system message instructions. These responses must be parsed into structured data.

2. Conversion Logic

Create logic to parse the AI's text response into fields and records. This process involves using temporary XML buffers to load and interpret data from the AI's response.

Procedure Details

The procedure ConvertTextResponseToJobProposalRecords is key for this transformation. It processes the Azure OpenAI response, loads it into an XML buffer, and populates the GPT Copilot Job Proposal record with structured data.

Info

The AI response is expected in XML format, following our instructions. XML is chosen for its ease of use and flexibility in conversion.

Tip

While XML is used here for simplicity, JSON or YAML could also be employed depending on your needs.

Note

The GPT Copilot Job Proposal is a custom temporary table in Business Central designed to store AI-generated project plans.

Key Actions

  • Initialization: Set up streams and buffers for XML handling.

    var
        TempXmlBuffer: Record "XML Buffer" temporary;
        TempBlob: Codeunit "Temp Blob";
        InStr: InStream;
        OutStr: OutStream;
    

  • XML Parsing: Load the response into an XML structure and parse it to extract meaningful data. Use the following code snippet to load the AI response into an XML buffer:

        // Create a instream to hold the response text
        TempBlob.CreateOutStream(OutStr);
        OutStr.WriteText(ResponseText);
        TempBlob.CreateInStream(InStr);
    
        // Load the XML buffer with the response text
        TempXmlBuffer.DeleteAll();
        TempXmlBuffer.LoadFromStream(InStr);
    

  • Data Insertion: Insert the parsed data into GPT Copilot Job Proposal records in the InsertJobProposalRecords procedure to reflect in the UI.

3. Lines Updates

Review the InsertJobProposalRecords procedure to see how xml data is inserted into GPT Copilot Job Proposal records, populating fields like Job Description, Task Details, Start Date, and End Date based on the AI's suggestions.

4. Header Updates

Examine the SetJobDescriptionAndFullDescription procedure to understand how Job Description and Full Description are updated based on the AI's response. This ensures the AI-generated content is properly displayed in the Copilot UI.

Conclusion

By completing this section, you'll be capable of transforming plain text AI responses into structured data that can be used within your Business Central application. This ability is crucial for making AI-generated content practical and valuable in real business settings.

Next Steps

Proceed to the next step to learn how to display the proposed project plan in the Copilot UI for user review and approval.