Skip to content

Commit

Permalink
Add ADI
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus Hartvig Grønbech committed Jan 23, 2025
1 parent ec3252b commit 60e78da
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/System Application/App/AI/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"idRanges": [
{
"from": 7758,
"to": 7778
"to": 7780
}
],
"target": "OnPrem",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.AI;

/// <summary>
/// The supported model types for Azure OpenAI.
/// </summary>
enum 7779 "ADI Model Type"
{
Access = Public;
Extensible = false;

/// <summary>
/// Invoice model type.
/// </summary>
value(0; Invoice)
{
}

/// <summary>
/// Recepit model type.
/// </summary>
value(1; Recepit)
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.Azure.DI;

/// <summary>
/// Azure Document Intelligence implementation.
/// </summary>
codeunit 9970 "Azure DI"
{
Access = Public;
InherentEntitlements = X;
InherentPermissions = X;

var
AzureOpenAIImpl: Codeunit "Azure DI Impl.";


/// <summary>
/// Analyze the invoice.
/// </summary>
/// <param name="Base64Data">Data to analyze.</param>
/// <returns>The analyzed result.</returns>
[Scope('OnPrem')]
procedure AnalyzeInvoice(Base64Data: Text): Text
var
CallerModuleInfo: ModuleInfo;
begin
NavApp.GetCallerModuleInfo(CallerModuleInfo);
exit(AzureOpenAIImpl.AnalyzeInvoice(Base64Data, CallerModuleInfo));
end;

/// <summary>
/// Analyze the Receipt.
/// </summary>
/// <param name="Base64Data">Data to analyze.</param>
/// <returns>The analyzed result.</returns>
[Scope('OnPrem')]
procedure AnalyzeReceipt(Base64Data: Text): Text
var
CallerModuleInfo: ModuleInfo;
begin
NavApp.GetCallerModuleInfo(CallerModuleInfo);
exit(AzureOpenAIImpl.AnalyzeReceipt(Base64Data, CallerModuleInfo));
end;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.Azure.DI;
using System.AI;
using System.Globalization;
using System.Telemetry;

/// <summary>
/// Azure Document Intelligence implementation.
/// </summary>
codeunit 7779 "Azure DI Impl."
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;

/// <summary>
/// Analyze the invoice.
/// </summary>
/// <param name="Base64Data">Data to analyze.</param>
/// <param name="CallerModuleInfo">The module info of the caller.</param>
/// <returns>The analyzed result.</returns>
[NonDebuggable]
procedure AnalyzeInvoice(Base64Data: Text; CallerModuleInfo: ModuleInfo) Result: Text
var
CustomDimensions: Dictionary of [Text, Text];
begin
AddTelemetryCustomDimensions(CustomDimensions, CallerModuleInfo);

if not SendRequest(Base64Data, Enum::"ADI Model Type"::Invoice, CallerModuleInfo, Result) then begin
FeatureTelemetry.LogError('', AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeInvoiceFailureLbl, GetLastErrorText(), '', Enum::"AL Telemetry Scope"::All, CustomDimensions);
exit;
end;

FeatureTelemetry.LogUsage('', AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeInvoiceCompletedLbl, Enum::"AL Telemetry Scope"::All, CustomDimensions);

end;

/// <summary>
/// Analyze the receipt.
/// </summary>
/// <param name="Base64Data">Data to analyze.</param>
/// <param name="CallerModuleInfo">The module info of the caller.</param>
/// <returns>The analyzed result.</returns>
[NonDebuggable]
procedure AnalyzeReceipt(Base64Data: Text; CallerModuleInfo: ModuleInfo) Result: Text
var
CustomDimensions: Dictionary of [Text, Text];
begin
AddTelemetryCustomDimensions(CustomDimensions, CallerModuleInfo);

if not SendRequest(Base64Data, Enum::"ADI Model Type"::Recepit, CallerModuleInfo, Result) then begin
FeatureTelemetry.LogError('', AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeInvoiceFailureLbl, GetLastErrorText(), '', Enum::"AL Telemetry Scope"::All, CustomDimensions);
exit;
end;

FeatureTelemetry.LogUsage('', AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeInvoiceCompletedLbl, Enum::"AL Telemetry Scope"::All, CustomDimensions);
end;

[TryFunction]
[NonDebuggable]
local procedure SendRequest(Base64Data: Text; ModelType: Enum "ADI Model Type"; CallerModuleInfo: ModuleInfo; var Result: Text)
var
ALCopilotFunctions: DotNet ALCopilotFunctions;

Check failure on line 66 in src/System Application/App/AI/src/DocumentIntelligence/AzureDIImpl.Codeunit.al

View workflow job for this annotation

GitHub Actions / Build System Application and Tools (Translated) / System Application and Tools (Translated)

AL0185 DotNet 'ALCopilotFunctions' is missing

Check failure on line 66 in src/System Application/App/AI/src/DocumentIntelligence/AzureDIImpl.Codeunit.al

View workflow job for this annotation

GitHub Actions / Build System Application and Tools (Clean) / System Application and Tools (Clean)

AL0185 DotNet 'ALCopilotFunctions' is missing
ALCopilotCapability: DotNet ALCopilotCapability;

Check failure on line 67 in src/System Application/App/AI/src/DocumentIntelligence/AzureDIImpl.Codeunit.al

View workflow job for this annotation

GitHub Actions / Build System Application and Tools (Translated) / System Application and Tools (Translated)

AL0185 DotNet 'ALCopilotCapability' is missing

Check failure on line 67 in src/System Application/App/AI/src/DocumentIntelligence/AzureDIImpl.Codeunit.al

View workflow job for this annotation

GitHub Actions / Build System Application and Tools (Clean) / System Application and Tools (Clean)

AL0185 DotNet 'ALCopilotCapability' is missing
ALCopilotResponse: DotNet ALCopilotOperationResponse;

Check failure on line 68 in src/System Application/App/AI/src/DocumentIntelligence/AzureDIImpl.Codeunit.al

View workflow job for this annotation

GitHub Actions / Build System Application and Tools (Translated) / System Application and Tools (Translated)

AL0185 DotNet 'ALCopilotOperationResponse' is missing

Check failure on line 68 in src/System Application/App/AI/src/DocumentIntelligence/AzureDIImpl.Codeunit.al

View workflow job for this annotation

GitHub Actions / Build System Application and Tools (Clean) / System Application and Tools (Clean)

AL0185 DotNet 'ALCopilotOperationResponse' is missing
ErrorMsg: Text;
begin
ClearLastError();
ALCopilotCapability := ALCopilotCapability.ALCopilotCapability(CallerModuleInfo.Publisher(), CallerModuleInfo.Id(), Format(CallerModuleInfo.AppVersion()), AzureDocumentIntelligenceCapabilityTok);
case ModelType of
Enum::"ADI Model Type"::Invoice:
ALCopilotResponse := ALCopilotFunctions.GenerateInvoiceIntelligence(Base64Data, ALCopilotCapability);
Enum::"ADI Model Type"::Recepit:
ALCopilotResponse := ALCopilotFunctions.GenerateReceiptIntelligence(Base64Data, ALCopilotCapability);
end;
ErrorMsg := ALCopilotResponse.ErrorText();
if ErrorMsg <> '' then
Error(ErrorMsg);

if not ALCopilotResponse.IsSuccess() then
Error(GenerateRequestFailedErr);

Result := ALCopilotResponse.Result();
end;


local procedure AddTelemetryCustomDimensions(var CustomDimensions: Dictionary of [Text, Text]; CallerModuleInfo: ModuleInfo)
var
Language: Codeunit Language;
SavedGlobalLanguageId: Integer;
begin
SavedGlobalLanguageId := GlobalLanguage();
GlobalLanguage(Language.GetDefaultApplicationLanguageId());

CustomDimensions.Add('Capability', AzureDocumentIntelligenceCapabilityTok);
CustomDimensions.Add('AppId', CallerModuleInfo.Id);
CustomDimensions.Add('Publisher', CallerModuleInfo.Publisher);
CustomDimensions.Add('UserLanguage', Format(GlobalLanguage()));

GlobalLanguage(SavedGlobalLanguageId);
end;

var
FeatureTelemetry: Codeunit "Feature Telemetry";
AzureDocumentIntelligenceCapabilityTok: Label 'ADI', Locked = true;
TelemetryAnalyzeInvoiceFailureLbl: Label 'Analyze invoice failed.', Locked = true;
TelemetryAnalyzeInvoiceCompletedLbl: Label 'Analyze invoice completed.', Locked = true;
TelemetryAnalyzeReceiptFailureLbl: Label 'Analyze receipt failed.', Locked = true;
TelemetryAnalyzeReceiptCompletedLbl: Label 'Analyze receipt completed.', Locked = true;
GenerateRequestFailedErr: Label 'The request did not return a success status code.';

}

0 comments on commit 60e78da

Please sign in to comment.