1
+ // ------------------------------------------------------------------------------------------------
2
+ // Copyright (c) Microsoft Corporation. All rights reserved.
3
+ // Licensed under the MIT License. See License.txt in the project root for license information.
4
+ // ------------------------------------------------------------------------------------------------
5
+ namespace System. AI. DocumentIntelligence;
6
+
7
+ using System. Telemetry;
8
+ using System;
9
+ using System. AI;
10
+
11
+ // / <summary>
12
+ // / Azure Document Intelligence implementation.
13
+ // / </summary>
14
+ codeunit 7779 "Azure DI Impl." implements "AI Service Name"
15
+ {
16
+ Access = Internal ;
17
+ InherentEntitlements = X;
18
+ InherentPermissions = X;
19
+
20
+ var
21
+ CopilotCapabilityImpl: Codeunit "Copilot Capability Impl";
22
+ FeatureTelemetry: Codeunit "Feature Telemetry";
23
+ AzureDocumentIntelligenceCapabilityTok: Label ' ADI' , Locked = true;
24
+ TelemetryAnalyzeInvoiceFailureLbl: Label ' Analyze invoice failed.' , Locked = true;
25
+ TelemetryAnalyzeInvoiceCompletedLbl: Label ' Analyze invoice completed.' , Locked = true;
26
+ TelemetryAnalyzeReceiptFailureLbl: Label ' Analyze receipt failed.' , Locked = true;
27
+ TelemetryAnalyzeReceiptCompletedLbl: Label ' Analyze receipt completed.' , Locked = true;
28
+ GenerateRequestFailedErr: Label ' The request did not return a success status code.' ;
29
+ AzureAiDocumentIntelligenceTxt: Label ' Azure AI Document Intelligence' , Locked = true;
30
+ CapabilityNotEnabledErr: Label ' Copilot capability '' %1'' has not been enabled. Please contact your system administrator.' , Comment = ' %1 is the name of the Copilot Capability' ;
31
+
32
+ procedure SetCopilotCapability( Capability: Enum "Copilot Capability"; CallerModuleInfo: ModuleInfo )
33
+ begin
34
+ CopilotCapabilityImpl. SetCopilotCapability( Capability, CallerModuleInfo, Enum ::"Azure AI Service Type"::"Azure Document Intelligence") ;
35
+ end ;
36
+
37
+ procedure RegisterCopilotCapability( CopilotCapability: Enum "Copilot Capability"; CopilotAvailability: Enum "Copilot Availability"; LearnMoreUrl: Text [2048 ]; CallerModuleInfo: ModuleInfo )
38
+ begin
39
+ CopilotCapabilityImpl. RegisterCapability( CopilotCapability, CopilotAvailability, Enum ::"Azure AI Service Type"::"Azure Document Intelligence", LearnMoreUrl, CallerModuleInfo) ;
40
+ end ;
41
+
42
+ // / <summary>
43
+ // / Analyze a single invoice.
44
+ // / </summary>
45
+ // / <param name="Base64Data">Data to analyze.</param>
46
+ // / <param name="CallerModuleInfo">The module info of the caller.</param>
47
+ // / <returns>The analyzed result.</returns>
48
+ procedure AnalyzeInvoice( Base64Data: Text ; CallerModuleInfo: ModuleInfo ) Result: Text
49
+ var
50
+ CustomDimensions: Dictionary of [Text , Text ];
51
+ begin
52
+ CopilotCapabilityImpl. CheckCapabilitySet() ;
53
+ if not CopilotCapabilityImpl. IsCapabilityActive( CallerModuleInfo) then
54
+ Error( CapabilityNotEnabledErr, CopilotCapabilityImpl. GetCapabilityName()) ;
55
+
56
+ CopilotCapabilityImpl. CheckCapabilityServiceType( Enum ::"Azure AI Service Type"::"Azure Document Intelligence") ;
57
+ CopilotCapabilityImpl. AddTelemetryCustomDimensions( CustomDimensions, CallerModuleInfo) ;
58
+
59
+ if not SendRequest( Base64Data, Enum ::"ADI Model Type"::Invoice, CallerModuleInfo, Result) then begin
60
+ FeatureTelemetry. LogError( ' 0000OLK' , AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeInvoiceFailureLbl, GetLastErrorText() , ' ' , Enum ::"AL Telemetry Scope"::All, CustomDimensions) ;
61
+ exit ;
62
+ end ;
63
+
64
+ FeatureTelemetry. LogUsage( ' 0000OLM' , AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeInvoiceCompletedLbl, Enum ::"AL Telemetry Scope"::All, CustomDimensions) ;
65
+ end ;
66
+
67
+ // / <summary>
68
+ // / Analyze a single receipt.
69
+ // / </summary>
70
+ // / <param name="Base64Data">Data to analyze.</param>
71
+ // / <param name="CallerModuleInfo">The module info of the caller.</param>
72
+ // / <returns>The analyzed result.</returns>
73
+ procedure AnalyzeReceipt( Base64Data: Text ; CallerModuleInfo: ModuleInfo ) Result: Text
74
+ var
75
+ CustomDimensions: Dictionary of [Text , Text ];
76
+ begin
77
+ CopilotCapabilityImpl. CheckCapabilitySet() ;
78
+ if not CopilotCapabilityImpl. IsCapabilityActive( CallerModuleInfo) then
79
+ Error( CapabilityNotEnabledErr, CopilotCapabilityImpl. GetCapabilityName()) ;
80
+
81
+ CopilotCapabilityImpl. AddTelemetryCustomDimensions( CustomDimensions, CallerModuleInfo) ;
82
+
83
+ if not SendRequest( Base64Data, Enum ::"ADI Model Type"::Receipt, CallerModuleInfo, Result) then begin
84
+ FeatureTelemetry. LogError( ' 0000OLL' , AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeReceiptFailureLbl, GetLastErrorText() , ' ' , Enum ::"AL Telemetry Scope"::All, CustomDimensions) ;
85
+ exit ;
86
+ end ;
87
+
88
+ FeatureTelemetry. LogUsage( ' 0000OLN' , AzureDocumentIntelligenceCapabilityTok, TelemetryAnalyzeReceiptCompletedLbl, Enum ::"AL Telemetry Scope"::All, CustomDimensions) ;
89
+ end ;
90
+
91
+ [TryFunction]
92
+ [NonDebuggable]
93
+ local procedure SendRequest( Base64Data: Text ; ModelType: Enum "ADI Model Type"; CallerModuleInfo: ModuleInfo ; var Result: Text )
94
+ var
95
+ ALCopilotFunctions: DotNet ALCopilotFunctions;
96
+ ALCopilotCapability: DotNet ALCopilotCapability;
97
+ ALCopilotResponse: DotNet ALCopilotOperationResponse;
98
+ ErrorMsg: Text ;
99
+ begin
100
+ ClearLastError() ;
101
+ ALCopilotCapability := ALCopilotCapability. ALCopilotCapability( CallerModuleInfo. Publisher() , CallerModuleInfo. Id() , Format( CallerModuleInfo. AppVersion()) , AzureDocumentIntelligenceCapabilityTok) ;
102
+ case ModelType of
103
+ Enum ::"ADI Model Type"::Invoice:
104
+ ALCopilotResponse := ALCopilotFunctions. GenerateInvoiceIntelligence( GenerateJsonForSingleInput( Base64Data) , ALCopilotCapability) ;
105
+ Enum ::"ADI Model Type"::Receipt:
106
+ ALCopilotResponse := ALCopilotFunctions. GenerateReceiptIntelligence( GenerateJsonForSingleInput( Base64Data) , ALCopilotCapability) ;
107
+ end ;
108
+ ErrorMsg := ALCopilotResponse. ErrorText() ;
109
+ if ErrorMsg <> ' ' then
110
+ Error( ErrorMsg) ;
111
+
112
+ if not ALCopilotResponse. IsSuccess() then
113
+ Error( GenerateRequestFailedErr) ;
114
+
115
+ Result := ALCopilotResponse. Result() ;
116
+ end ;
117
+
118
+ local procedure GenerateJsonForSingleInput( Base64: Text ) : Text
119
+ var
120
+ JsonObject : JsonObject ;
121
+ InputsObject: JsonObject ;
122
+ InnerObject: JsonObject ;
123
+ JsonText: Text ;
124
+ begin
125
+ // Create the inner object with the base64Encoded property
126
+ InnerObject. Add ( ' base64_encoded' , Base64) ;
127
+ // Create the inputs object and add the inner object to it
128
+ InputsObject. Add ( ' 1' , InnerObject) ;
129
+ // Create the main JSON object and add the inputs object to it
130
+ JsonObject . Add ( ' inputs' , InputsObject) ;
131
+ // Convert the JSON object to text
132
+ JsonObject . WriteTo( JsonText) ;
133
+ // Return the JSON text
134
+ exit ( JsonText) ;
135
+ end ;
136
+
137
+ procedure GetServiceName() : Text [250 ]
138
+ begin
139
+ exit ( AzureAiDocumentIntelligenceTxt) ;
140
+ end ;
141
+
142
+ procedure GetServiceId() : Code [50 ];
143
+ begin
144
+ exit ( AzureAiDocumentIntelligenceTxt) ;
145
+ end ;
146
+
147
+ }
0 commit comments