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:
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
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.
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
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\\';
AzureOpenAi: Codeunit "Azure OpenAi";
FinancialReportResults := CalculateFinancialReportResults(AccSchedLine, ColumnLayout, UseAmtsInAddCurr);
SuggestedExplanationText := AzureOpenAi.GenerateCompletion(StrSubstNo(ExplainFinancialReportPromptTemplate, FinancialReportResults));
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
// additional variables
// Init TextBuilder for the Financial Report results
FinancialReportResults.Append('Financial Report name: ');
// Here should be code to fill the context for the Prompt.
// Calculate Financial Report Amounts and assign to TextBuilder
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!