Codeunit 6195 E-Doc. AI Tool Processor

E-Document Core, Microsoft.eServices.EDocument.Processing.AI

src/Apps/W1/EDocument/App/src/Processing/AI/EDocAIToolProcessor.Codeunit.al282 lines, Copyright (c) Microsoft Corporation. MIT

// ------------------------------------------------------------------------------------------------// Copyright (c) Microsoft Corporation. All rights reserved.// Licensed under the MIT License. See License.txt in the project root for license information.// ------------------------------------------------------------------------------------------------namespace Microsoft.eServices.EDocument.Processing.AI;using System.AI;using System.Azure.KeyVault;using System.Environment;using System.Environment.Configuration;using System.Globalization;using System.Telemetry;/// <summary>/// Codeunit for processing E-Document AI tasks./// </summary>codeunit 6195 "E-Doc. AI Tool Processor"{    Access = Public;    InherentPermissions = X;    InherentEntitlements = X;    var        AzureOpenAI: Codeunit "Azure OpenAI";        AOAIChatCompletionParams: Codeunit "AOAI Chat Completion Params";        AOAIChatMessages: Codeunit "AOAI Chat Messages";        FeatureTelemetry: Codeunit "Feature Telemetry";        AISystem: Interface IEDocAISystem;        TelemetryDimensions: Dictionary of [Text, Text];        ModelSwitchSecretNameTok: Label 'EDocAIModelSwitch', Locked = true;        GPT56CeresTok: Label 'GPT56Ceres', Locked = true;    /// <summary>    /// Initializes the AI system for E-Document processing.    /// This method sets up the Azure OpenAI parameters, system chat messages, and telemetry dimensions.    /// </summary>    /// <param name="EDocAISystem">The E-Document AI System to be configured.</param>    /// <returns>True if setup was successful, false otherwise.</returns>    procedure Setup(EDocAISystem: Interface IEDocAISystem): Boolean    var        CopilotCapability: Codeunit "Copilot Capability";        DeploymentName: Text;        UseCase: Option "Matching Assistance","PDF Extraction";    begin        Clear(TelemetryDimensions);        RegisterCapabilityIfNeeded();        if not CopilotCapability.IsCapabilityRegistered(Enum::"Copilot Capability"::"E-Document Matching Assistance") then            exit(false);        if not CopilotCapability.IsCapabilityActive(Enum::"Copilot Capability"::"E-Document Matching Assistance") then            exit(false);        // Use the same resolved deployment for authorization and telemetry.        DeploymentName := GetChatCompletionsDeployment(UseCase::"Matching Assistance");        AzureOpenAI.SetAuthorization(Enum::"AOAI Model Type"::"Chat Completions", DeploymentName);        AzureOpenAI.SetCopilotCapability(Enum::"Copilot Capability"::"E-Document Matching Assistance");        // Setup parameters        AOAIChatCompletionParams.SetMaxTokens(GetDefaultMaxOutputTokens());        AOAIChatCompletionParams.SetTemperature(GetDefaultTemperature());        // Setup AI system and messages        AISystem := EDocAISystem;        SetupChatMessages();        // Setup telemetry        TelemetryDimensions.Add('Model', DeploymentName);        TelemetryDimensions.Add('MaxTokens', Format(GetDefaultMaxOutputTokens()));        TelemetryDimensions.Add('Temperature', Format(GetDefaultTemperature()));        LogFeatureUptake();        exit(true);    end;    /// <summary>    /// Processes the input text using the configured AI system and returns the response.    /// </summary>    /// <param name="Input">User input text to be processed by the AI system.</param>    /// <param name="AOAIResponse">AOAI Operation Response containing the result of the AI processing.</param>    /// <returns>True if processing was successful, false otherwise.</returns>    procedure Process(Input: Text; var AOAIResponse: Codeunit "AOAI Operation Response"): Boolean    var        AOAIToken: Codeunit "AOAI Token";        StartTime: DateTime;        Success: Boolean;        EstimateTokenCount: Integer;        EmptyInputErr: Label 'Input cannot be empty';        TokenLimitExceededTok: Label 'Token limit exceeded. Input of %1 tokens exceeds maximum token limit of %2 tokens', Comment = '%1 = Current token count, %2 = Maximum token limit', Locked = true;        TokenLimitEventTok: Label 'Token Limit Exceeded', Locked = true;    begin        if Input = '' then            Error(EmptyInputErr);        // Add user message        AOAIChatMessages.AddUserMessage(Input);        EstimateTokenCount := AOAIToken.GetGPT4TokenCount(Input);        TelemetryDimensions.Add('Token Count', Format(EstimateTokenCount));        if EstimateTokenCount > GetDefaultMaxInputTokens() then begin            LogError(TokenLimitEventTok, StrSubstNo(TokenLimitExceededTok, EstimateTokenCount, GetDefaultMaxInputTokens()));            exit(false);        end;        // Process with timing        StartTime := CurrentDateTime();        AzureOpenAI.GenerateChatCompletion(AOAIChatMessages, AOAIChatCompletionParams, AOAIResponse);        TelemetryDimensions.Add('ProcessingTime', Format(CurrentDateTime() - StartTime));        // Handle response        Success := HandleResponse(AOAIResponse);        LogResult(Success);        exit(Success);    end;    [NonDebuggable]    local procedure SetupChatMessages()    var        TempUserSettings: Record "User Settings" temporary;        UserSettingsCU: Codeunit "User Settings";        LanguageCU: Codeunit Language;        LanguageName: Text;        Tool: Interface "AOAI Function";    begin        UserSettingsCU.GetUserSettings(UserSecurityId(), TempUserSettings);        LanguageName := LanguageCU.GetWindowsLanguageName(TempUserSettings."Language ID");        // Set system prompt        AOAIChatMessages.SetPrimarySystemMessage(AISystem.GetSystemPrompt(LanguageName));        // Add tools        foreach Tool in AISystem.GetTools() do            AOAIChatMessages.AddTool(Tool);        AOAIChatMessages.SetToolChoice('auto');    end;    [NonDebuggable]    internal procedure SetLanguageInPrompt(Prompt: SecretText; UserLanguage: Text): SecretText    begin        if UserLanguage = '' then            UserLanguage := 'English';        exit(SecretStrSubstNo(Prompt.Unwrap(), UserLanguage));    end;    local procedure HandleResponse(var AOAIResponse: Codeunit "AOAI Operation Response"): Boolean    var        APIErrorTok: Label 'Status: %1, Error: %2', Comment = '%1 = Status code, %2 = Error message', Locked = true;        EmptyResponseTok: Label 'API Empty response', Locked = true;        APIErrorEventTok: Label 'API Error', Locked = true;    begin        if not AOAIResponse.IsSuccess() then begin            LogError(APIErrorEventTok, StrSubstNo(APIErrorTok, AOAIResponse.GetStatusCode(), AOAIResponse.GetError()));            exit(false);        end;        if AOAIResponse.GetResult() = '' then begin            LogError(EmptyResponseTok, 'AI returned empty result');            exit(false);        end;        if AOAIResponse.IsFunctionCall() then            exit(HandleFunctionCalls(AOAIResponse));        TelemetryDimensions.Add('ResponseLength', Format(StrLen(AOAIResponse.GetResult())));        exit(true);    end;    local procedure HandleFunctionCalls(var AOAIResponse: Codeunit "AOAI Operation Response"): Boolean    var        AOAIFunctionResponse: Codeunit "AOAI Function Response";        Success: Boolean;        ExecutedFunctions: Text;        FunctionCallExecuteFailedTok: Label 'Function %1 failed to execute', Comment = '%1 = Function name', Locked = true;        FailedFunctionEventTok: Label 'Function Execution Failed', Locked = true;        FunctionCallFailedTok: Label 'Function call failed', Locked = true;    begin        Success := true;        TelemetryDimensions.Add('Number of Function Calls', Format(AOAIResponse.GetFunctionResponses().Count()));        foreach AOAIFunctionResponse in AOAIResponse.GetFunctionResponses() do            if AOAIFunctionResponse.IsSuccess() then begin                // Function was executed successfully by the AI model                if ExecutedFunctions <> '' then                    ExecutedFunctions += ', ';                ExecutedFunctions += AOAIFunctionResponse.GetFunctionName();            end else begin                Success := false;                TelemetryDimensions.Add('FailedFunction', AOAIFunctionResponse.GetFunctionName());                LogError(FailedFunctionEventTok, StrSubstNo(FunctionCallExecuteFailedTok, AOAIFunctionResponse.GetFunctionName()));            end;        if Success then            TelemetryDimensions.Add('ExecutedFunctions', ExecutedFunctions)        else            LogError(FunctionCallFailedTok, 'One or more function calls failed');        exit(Success);    end;    local procedure LogFeatureUptake()    begin        FeatureTelemetry.LogUptake('0000PUJ', AISystem.GetFeatureName(), Enum::"Feature Uptake Status"::"Set up");        FeatureTelemetry.LogUptake('0000PUK', AISystem.GetFeatureName(), Enum::"Feature Uptake Status"::Used);    end;    local procedure LogResult(Success: Boolean)    begin        if Success then            FeatureTelemetry.LogUsage('0000PUL', AISystem.GetFeatureName(), 'AI Processing Success', TelemetryDimensions)        else            FeatureTelemetry.LogError('0000PUH', AISystem.GetFeatureName(), 'AI Processing Failed', 'Processing failed', '', TelemetryDimensions);    end;    local procedure LogError(EventName: Text; ErrorMessage: Text)    begin        FeatureTelemetry.LogError('0000PUI', AISystem.GetFeatureName(), EventName, ErrorMessage, '', TelemetryDimensions);    end;    local procedure RegisterCapabilityIfNeeded()    var        CopilotCapability: Codeunit "Copilot Capability";        EnvironmentInformation: Codeunit "Environment Information";        LearnMoreUrlTxt: Label 'https://go.microsoft.com/fwlink/?linkid=2262630', Locked = true;    begin        if not EnvironmentInformation.IsSaaSInfrastructure() then            exit;        if not CopilotCapability.IsCapabilityRegistered(Enum::"Copilot Capability"::"E-Document Matching Assistance") then            CopilotCapability.RegisterCapability(Enum::"Copilot Capability"::"E-Document Matching Assistance", LearnMoreUrlTxt);    end;    local procedure GetDefaultMaxInputTokens(): Integer    begin        exit(125000); // 125k token limit    end;    local procedure GetDefaultMaxOutputTokens(): Integer    begin        exit(32000); // 32k token limit    end;    local procedure GetDefaultTemperature(): Decimal    begin        exit(0);    end;    internal procedure GetChatCompletionsDeployment(UseCase: Option "Matching Assistance","PDF Extraction"): Text    var        AOAIDeployments: Codeunit "AOAI Deployments";        AzureKeyVault: Codeunit "Azure Key Vault";        ModelSwitchValue: Text;        UseGPT56: Boolean;    begin        if not AzureKeyVault.GetAzureKeyVaultSecret(ModelSwitchSecretNameTok, ModelSwitchValue) then            ModelSwitchValue := '';        UseGPT56 := UpperCase(ModelSwitchValue.Trim()) = UpperCase(GPT56CeresTok);        case UseCase of            UseCase::"Matching Assistance":                if UseGPT56 then                    exit(AOAIDeployments.GetGPT56CeresLatest())                else                    exit(AOAIDeployments.GetGPT41Latest());            UseCase::"PDF Extraction":                if UseGPT56 then                    exit(AOAIDeployments.GetGPT56CeresPreview())                else                    exit(AOAIDeployments.GetGPT41MiniPreview());            else                exit(AOAIDeployments.GetGPT41Latest());        end;    end;    procedure GetEDocumentMatchingAssistanceName(): Text    begin        exit('E-Document Matching Assistance');    end;}