Explain my Financial Report: Azure OpenAI Codeunit in Action

I just returned from beautiful Antwerp, were hold a session Smarter Apps in Less Time: Copilot and ChatGPT Integration for BC Developers at the BC TechDays 2023. 

What a pleasure was to return to that stage and share what’s i discovered for the last half year. 

Just in 2 hours before my session, during the keynote, Microsoft announced new GitHub repo: https://aka.ms/BCStartCodingWithAI, which you can find in the VS Code AL Home page.

Prepare your AL environment

If we look at the repo, Microsoft had prepared, we’ll find a short list of .al files

You can clone the repo and make changes directly, or you can just grab what you need to your repo and continue with your logic. We will go second way.

What we need are:

  • AzureOpenAI.Codeunit.al
  • AzureOpenAISetup.Page.al

Get Azure OpenAI key

Now you need to get Azure Open AI uri and a key. 

First, you need to be approved by Microsoft, to be able to create Open AI resource. This could take time, but that’s what we have now. To put yourself into the wait list fill the form here https://aka.ms/oaiapply

This will give you access to the most popular and cheap model, we will use in our demo GPT-3.5

If you need access to the more advanced (and more expensive) GPT-4, please also fill the form https://aka.ms/oai/get-gpt4

Second, create Open AI resource in the Azure portal

Now you are at the point, when you need to deploy a model. The choice of model depends on the task it’s going to solve. 

For this blog I’m going to solve “Explain my financial report” task. 

I’m going to use Azure OpenAI Codeunit, which is designed to call completions endpoint of gpt-35-turbo model.

When we created Azure OpenAI resource, we need to click on Deploy button. This will open Azure OpenAI Studio.

To get Azure OpenAI url and a key, go to Completions and then View Code

Create Prompt

To get a result from the deployed model, that solves our task we need to “tell” the model, what we want to receive in a simple natural language.

Think of a prompt as a technical task to an intern.

There are tons of content in the internet about How to create a good prompt. I will recommend this one https://learn.microsoft.com/en-us/azure/cognitive-services/openai/concepts/advanced-prompt-engineering

We want to “Explain out financial report” task. So, i will end in the prompt like this:

 

				
					You are CFO Assistant. 
Below is the financial report of a company.
Analyze the financial report, extract key internal risk factors, and key external risk factors.
Provide a concise explanation of the financial results along with identified risks and recommended actions.

FINANCIAL REPORT
"""
"""

EXPLANATION

				
			

You can experiment with Prompt in the Azure Open AI Studio, until you are ok with the final result

Call Azure OpenAI from AL

So the main function to get Financial Report Explanations from AL is this one

				
					var
        SuggestedExplanationText: Text;
        ExplainFinancialReportPromptTemplate: label 'You are CFO Assistant.\\Below is the financial report of a company.\\Analyze the financial report, extract key internal risk factors, and key external risk factors.\\Provide a concise explanation of the financial results along with identified risks and recommended actions.\\\\FINANCIAL REPORT\\"""\\%1\\"""\\EXPLANATION\\';

    procedure SuggestExplanation()
    var
        FinancialReportResults: Text;
        AzureOpenAi: Codeunit "Azure OpenAi";
    begin
        FinancialReportResults := CalculateFinancialReportResults(AccSchedLine, ColumnLayout, UseAmtsInAddCurr);
        SuggestedExplanationText := AzureOpenAi.GenerateCompletion(StrSubstNo(ExplainFinancialReportPromptTemplate, FinancialReportResults));
    end;

				
			

In the CalculateFinancialReportResults()  we will go through the Financial Report, calculate amounts row by row, column by column and save that in a text variable. Pretty same as the standard ExportToExcel() does. 

The actual code is on my GitHub, here is just a snippet to get overview of what’s happening

				
					    local procedure CalculateFinancialReportResults(var AccSchedLine: Record "Acc. Schedule Line"; var ColumnLayout: Record "Column Layout"; UseAmtsInAddCurr: Boolean): Text
    var
        FinancialReportResults: TextBuilder;
        // additional variables
    begin

        // Init TextBuilder for the Financial Report results
        FinancialReportResults.Append('Financial Report name: ');
        FinancialReportResults.Append(AccSchedName.Description);
        FinancialReportResults.AppendLine();
        FinancialReportResults.AppendLine();

        // Here should be code to fill the context for the Prompt. 
        // Calculate Financial Report Amounts and assign to TextBuilder
        
        exit(FinancialReportResults.ToText());
    end;

				
			

Setup Azure OpenAI in Business Central

No need to add a separate Setup action. When you call AzureOpenAi.GenerateCompletion() auto popup dialog will appear if you didn’t setup Azure OpenAI url and a key. 

Explain Financial Report in Action

Now we are ready to test altogether and bring the magic of AI to Financial Users!

… and soon we will have a Rich Text editor in Business Central, so this Explanation can become even more beautiful!

Share Post:

Leave a Reply

About Me

DMITRY KATSON

A Microsoft MVP, Business Central architect and a project manager, blogger and a speaker, husband and a twice a father. With more than 15 years in business, I went from developer to company owner. Having a great team, still love to put my hands on code and create AI powered Business Central Apps that just works.

Follow Me

Recent Posts

Tags