Codeunit 7771 Azure OpenAI in 26

App
System Application
Namespace
System.AI

Procedures, 18

Versions171819202122232425262728latest

Source242526272829

Source in 26

src/System Application/App/AI/src/Azure OpenAI/AzureOpenAI.Codeunit.al307 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 System.AI;

/// <summary>
/// Provides functionality for using the Azure OpenAI API.
/// </summary>
codeunit 7771 "Azure OpenAI"
{
    Access = Public;
    InherentEntitlements = X;
    InherentPermissions = X;

    var
        AzureOpenAIImpl: Codeunit "Azure OpenAI Impl";

    /// <summary>
    /// Checks if the Azure OpenAI API is enabled for the environment and if the capability is active on the environment.
    /// </summary>
    /// <param name="CopilotCapability">The copilot capability to check.</param>
    /// <returns>True if API and capability is enabled for environment.</returns>
    procedure IsEnabled(CopilotCapability: Enum "Copilot Capability"): Boolean
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.IsEnabled(CopilotCapability, CallerModuleInfo));
    end;

    /// <summary>
    /// Checks if the Azure OpenAI API is enabled for the environment and if the capability is active on the environment.
    /// </summary>
    /// <param name="CopilotCapability">The copilot capability to check.</param>
    /// <param name="Silent">If true, no error message will be shown if API is not enabled.</param>
    /// <returns>True if API and capability is enabled for environment.</returns>
    procedure IsEnabled(CopilotCapability: Enum "Copilot Capability"; Silent: Boolean): Boolean
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.IsEnabled(CopilotCapability, Silent, CallerModuleInfo));
    end;

    /// <summary>
    /// Checks if the Azure OpenAI API is enabled for the environment and if the capability is active on the environment.
    /// </summary>
    /// <param name="CopilotCapability">The copilot capability to check.</param>
    /// <param name="Silent">If true, no error message will be shown if API is not enabled.</param>
    /// <param name="AppId">The id of the app, which registered the copilot capability.</param>
    /// <returns>True if API and capability is enabled for environment.</returns>
    procedure IsEnabled(CopilotCapability: Enum "Copilot Capability"; Silent: Boolean; AppId: Guid): Boolean
    var
        AppModuleInfo: ModuleInfo;
    begin
        if not NavApp.GetModuleInfo(AppId, AppModuleInfo) then
            exit(false);
        exit(AzureOpenAIImpl.IsEnabled(CopilotCapability, Silent, AppModuleInfo));
    end;

    /// <summary>
    /// Checks if the Azure OpenAI API authorization is configured for the environment.
    /// </summary>
    /// <param name="ModelType">The model type to check authorization for.</param>
    /// <returns>True if API is authorized for environment.</returns>
    procedure IsAuthorizationConfigured(ModelType: Enum "AOAI Model Type"): Boolean
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.IsAuthorizationConfigured(ModelType, CallerModuleInfo));
    end;

    /// <summary>
    /// Checks if the Azure OpenAI API is enabled for the environment and authorization is configured for the modeltype.
    /// </summary>
    /// <param name="CopilotCapability">The copilot capability to check.</param>
    /// <param name="ModelType">The model type to check authorization for.</param>
    /// <returns>True if the API is enabled and model authorization has been configured.</returns>
    procedure IsInitialized(CopilotCapability: Enum "Copilot Capability"; ModelType: Enum "AOAI Model Type"): Boolean
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.IsInitialized(CopilotCapability, ModelType, CallerModuleInfo));
    end;

#if not CLEAN26
    /// <summary>
    /// Sets the managed Azure OpenAI API authorization to use for a specific model type.
    /// This will send the Azure OpenAI call to the deployment specified in <paramref name="ManagedResourceDeployment"/>, and will use the other parameters to verify that you have access to Azure OpenAI.
    /// </summary>
    /// <param name="ModelType">The model type to set authorization for.</param>
    /// <param name="Endpoint">The endpoint to use to verify access to Azure OpenAI. This is used only for verification, not for actual Azure OpenAI calls.</param>
    /// <param name="Deployment">The deployment to use to verify access to Azure OpenAI. This is used only for verification, not for actual Azure OpenAI calls.</param>
    /// <param name="ApiKey">The API key to use  to verify access to Azure OpenAI. This is used only for verification, not for actual Azure OpenAI calls.</param>
    /// <param name="ManagedResourceDeployment">The managed deployment to use for the model type.</param>
    /// <remarks> NOTE: This function is currently only available to selected partners.
    /// Endpoint would look like: https://resource-name.openai.azure.com/
    /// Deployment would look like: gpt-35-turbo-16k
    /// </remarks>
    [NonDebuggable]
    [Obsolete('Using Managed AI resources now requires different input parameters. Use the other overload for SetManagedResourceAuthorization instead.', '26.0')]
    procedure SetManagedResourceAuthorization(ModelType: Enum "AOAI Model Type"; Endpoint: Text; Deployment: Text; ApiKey: SecretText; ManagedResourceDeployment: Text)
    begin
        AzureOpenAIImpl.SetManagedResourceAuthorization(ModelType, Endpoint, Deployment, ApiKey, ManagedResourceDeployment);
    end;
#endif

    /// <summary>
    /// Sets the managed Azure OpenAI API authorization to use for a specific model type.
    /// This will send the Azure OpenAI call to the deployment specified in <paramref name="ManagedResourceDeployment"/>, and will use the other parameters to verify that you have access to Azure OpenAI.
    /// </summary>
    /// <param name="ModelType">The model type to set authorization for.</param>
    /// <param name="AOAIAccountName">Name of the Azure Open AI resource like "MyAzureOpenAIResource"</param>
    /// <param name="ApiKey">The API key to access the Azure Open AI resource. This is used only for verification of access, not for actual Azure Open AI calls.</param>
    /// <param name="ManagedResourceDeployment">The managed deployment to use for the model type.</param>
    [NonDebuggable]
    procedure SetManagedResourceAuthorization(ModelType: Enum "AOAI Model Type"; AOAIAccountName: Text; ApiKey: SecretText; ManagedResourceDeployment: Text)
    begin
        AzureOpenAIImpl.SetManagedResourceAuthorization(ModelType, AOAIAccountName, ApiKey, ManagedResourceDeployment);
    end;

    /// <summary>
    /// Sets the Azure OpenAI API authorization to use for a specific model type and endpoint.
    /// </summary>
    /// <param name="ModelType">The model type to set authorization for.</param>
    /// <param name="Endpoint">The endpoint to use for the model type.</param>
    /// <param name="Deployment">The deployment to use for the endpoint.</param>
    /// <param name="ApiKey">The API key to use for the endpoint.</param>
    /// <remarks>Endpoint would look like: https://resource-name.openai.azure.com/
    /// Deployment would look like: gpt-35-turbo-16k
    /// </remarks>
    [NonDebuggable]
    procedure SetAuthorization(ModelType: Enum "AOAI Model Type"; Endpoint: Text; Deployment: Text; ApiKey: SecretText)
    begin
        AzureOpenAIImpl.SetAuthorization(ModelType, Endpoint, Deployment, ApiKey);
    end;

    /// <summary>
    /// Sets the Azure OpenAI API authorization to use for a specific model type.
    /// </summary>
    /// <param name="ModelType">The model type to set authorization for.</param>
    /// <param name="Deployment">The deployment name to use for the model type.</param>
    /// <remarks>Deployment would look like: gpt-35-turbo-16k</remarks>
    [NonDebuggable]
    procedure SetAuthorization(ModelType: Enum "AOAI Model Type"; Deployment: Text)
    begin
        AzureOpenAIImpl.SetAuthorization(ModelType, Deployment);
    end;

    /// <summary>
    /// Generates a text completion given a prompt.
    /// </summary>
    /// <param name="Prompt">The prompt to generate completion for.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated completion.</returns>
    /// <error>The completion authentication was not configured.</error>
    /// <error>The completion generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateTextCompletion(Prompt: SecretText; var AOAIOperationResponse: Codeunit "AOAI Operation Response"): Text
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.GenerateTextCompletion(Prompt, AOAIOperationResponse, CallerModuleInfo));
    end;

    /// <summary>
    /// Generates a completion given a prompt and completion parameters.
    /// </summary>
    /// <param name="Prompt">The prompt to generate completion for.</param>
    /// <param name="AOAICompletionParams">The optional completion parameters to use.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated completion.</returns>
    /// <error>The completion authentication was not configured.</error>
    /// <error>The completion generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateTextCompletion(Prompt: SecretText; AOAICompletionParams: Codeunit "AOAI Text Completion Params"; var AOAIOperationResponse: Codeunit "AOAI Operation Response"): Text
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.GenerateTextCompletion(Prompt, AOAICompletionParams, AOAIOperationResponse, CallerModuleInfo));
    end;

    /// <summary>
    /// Generates a text completion given a prompt.
    /// </summary>
    /// <param name="Metaprompt">The metaprompt to be appended with the prompt.</param>
    /// <param name="Prompt">The prompt to generate completion for.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated completion.</returns>
    /// <error>The completion authentication was not configured.</error>
    /// <error>The completion generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateTextCompletion(Metaprompt: SecretText; Prompt: SecretText; var AOAIOperationResponse: Codeunit "AOAI Operation Response"): Text
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.GenerateTextCompletion(Metaprompt, Prompt, AOAIOperationResponse, CallerModuleInfo));
    end;

    /// <summary>
    /// Generates a completion given a prompt and completion parameters.
    /// </summary>
    /// <param name="Metaprompt">The metaprompt to be appended with the prompt.</param>
    /// <param name="Prompt">The prompt to generate completion for.</param>
    /// <param name="AOAICompletionParams">The optional completion parameters to use.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated completion.</returns>
    /// <error>The completion authentication was not configured.</error>
    /// <error>The completion generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateTextCompletion(Metaprompt: SecretText; Prompt: SecretText; AOAICompletionParams: Codeunit "AOAI Text Completion Params"; var AOAIOperationResponse: Codeunit "AOAI Operation Response"): Text
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.GenerateTextCompletion(Metaprompt, Prompt, AOAICompletionParams, AOAIOperationResponse, CallerModuleInfo));
    end;


    /// <summary>
    /// Generates embeddings given an input.
    /// </summary>
    /// <param name="Input">The input to generate embedding for.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated list of embeddings.</returns>
    /// <error>The embedding authentication was not configured.</error>
    /// <error>The embedding generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateEmbeddings(Input: SecretText; var AOAIOperationResponse: Codeunit "AOAI Operation Response"): List of [Decimal]
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        exit(AzureOpenAIImpl.GenerateEmbeddings(Input, AOAIOperationResponse, CallerModuleInfo));
    end;

    /// <summary>
    /// Generates a chat completion given a list of chat messages.
    /// </summary>
    /// <param name="AOAIChatMessages">The chat messages to generate completion for.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated chat completion.</returns>
    /// <error>The chat completion authentication was not configured.</error>
    /// <error>The chat completion generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateChatCompletion(var AOAIChatMessages: Codeunit "AOAI Chat Messages"; var AOAIOperationResponse: Codeunit "AOAI Operation Response")
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        AzureOpenAIImpl.GenerateChatCompletion(AOAIChatMessages, AOAIOperationResponse, CallerModuleInfo);
    end;

    /// <summary>
    /// Generates a chat completion given a list of chat messages and completion parameters.
    /// </summary>
    /// <param name="AOAIChatMessages">The chat messages to generate completion for.</param>
    /// <param name="AOAIChatCompletionParams">The optional chat completion parameters to use.</param>
    /// <param name="AOAIOperationResponse">The response of the operation upon successful or failure execution.</param>
    /// <returns>The generated chat completion.</returns>
    /// <error>The chat completion authentication was not configured.</error>
    /// <error>The chat completion generation failed with status code %1.</error>
    [NonDebuggable]
    procedure GenerateChatCompletion(var AOAIChatMessages: Codeunit "AOAI Chat Messages"; AOAIChatCompletionParams: Codeunit "AOAI Chat Completion Params"; var AOAIOperationResponse: Codeunit "AOAI Operation Response")
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        AzureOpenAIImpl.GenerateChatCompletion(AOAIChatMessages, AOAIChatCompletionParams, AOAIOperationResponse, CallerModuleInfo);
    end;

    /// <summary>
    /// Sets the copilot capability that the API is running for.
    /// </summary>
    /// <param name="CopilotCapability">The copilot capability to set.</param>
    [NonDebuggable]
    procedure SetCopilotCapability(CopilotCapability: Enum "Copilot Capability")
    var
        CallerModuleInfo: ModuleInfo;
    begin
        NavApp.GetCallerModuleInfo(CallerModuleInfo);
        AzureOpenAIImpl.SetCopilotCapability(CopilotCapability, CallerModuleInfo);
    end;

#if not CLEAN24
    /// <summary>
    /// Gets the approximate token count for the input.
    /// </summary>
    /// <param name="Input">The input to get the approximate token count for.</param>
    /// <returns>The approximate token count.</returns>
    [NonDebuggable]
    [Obsolete('Use the AOAI Token codeunit.', '24.0')]
    procedure ApproximateTokenCount(Input: Text): Integer
    begin
#pragma warning disable AL0432
        AzureOpenAIImpl.ApproximateTokenCount(Input);
#pragma warning restore AL0432
    end;
#endif

}

Procedures, 18

NameParametersReturnsAccessObsolete
IsEnabled(Enum Copilot Capability)Booleanpublic-
IsEnabled(Enum Copilot Capability, Boolean)Booleanpublic-
IsAuthorizationConfigured(Enum AOAI Model Type)Booleanpublic-
IsInitialized(Enum Copilot Capability, Enum AOAI Model Type)Booleanpublic-
SetAuthorization(Enum AOAI Model Type, Text, Text, SecretText)public-
SetAuthorization(Enum AOAI Model Type, Text)public-
GenerateTextCompletion(SecretText, var Codeunit AOAI Operation Response)Textpublic-
GenerateTextCompletion(SecretText, Codeunit AOAI Text Completion Params, var Codeunit AOAI Operation Response)Textpublic-
GenerateTextCompletion(SecretText, SecretText, var Codeunit AOAI Operation Response)Textpublic-
GenerateTextCompletion(SecretText, SecretText, Codeunit AOAI Text Completion Params, var Codeunit AOAI Operation Response)Textpublic-
GenerateEmbeddings(SecretText, var Codeunit AOAI Operation Response)Listpublic-
GenerateChatCompletion(var Codeunit AOAI Chat Messages, var Codeunit AOAI Operation Response)public-
GenerateChatCompletion(var Codeunit AOAI Chat Messages, Codeunit AOAI Chat Completion Params, var Codeunit AOAI Operation Response)public-
SetCopilotCapability(Enum Copilot Capability)public-
ApproximateTokenCount(Text)IntegerpublicPending 24.0
Use the AOAI Token codeunit.
SetManagedResourceAuthorization(Enum AOAI Model Type, Text, Text, SecretText, Text)publicPending 26.0
Using Managed AI resources now requires different input parameters. Use the other overload for SetManagedResourceAuthorization instead.
IsEnabled(Enum Copilot Capability, Boolean, Guid)Booleanpublic-
SetManagedResourceAuthorization(Enum AOAI Model Type, Text, SecretText, Text)public-