Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -114,7 +114,7 @@
"idRanges": [
{
"from": 7757,
"to": 7786
"to": 7788
}
],
"target": "OnPrem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"

SendTokenCountTelemetry(AOAIToken.GetGPT4TokenCount(Metaprompt), AOAIToken.GetGPT4TokenCount(Prompt), CustomDimensions);

if not SendRequest(Enum::"AOAI Model Type"::"Text Completions", TextCompletionsAOAIAuthorization, PayloadText, AOAIOperationResponse, CallerModuleInfo) then begin
if not SendRequest(Enum::"AOAI Model Type"::"Text Completions", TextCompletionsAOAIAuthorization, PayloadText, AOAIOperationResponse, CallerModuleInfo, '') then begin
FeatureTelemetry.LogError('0000KVD', GetAzureOpenAICategory(), TelemetryGenerateTextCompletionLbl, CompletionsFailedWithCodeErr, '', Enum::"AL Telemetry Scope"::All, CustomDimensions);
exit;
end;
Expand All @@ -220,7 +220,7 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"

CopilotCapabilityImpl.AddTelemetryCustomDimensions(CustomDimensions, CallerModuleInfo);
SendTokenCountTelemetry(0, AOAIToken.GetAdaTokenCount(Input), CustomDimensions);
if not SendRequest(Enum::"AOAI Model Type"::Embeddings, EmbeddingsAOAIAuthorization, PayloadText, AOAIOperationResponse, CallerModuleInfo) then begin
if not SendRequest(Enum::"AOAI Model Type"::Embeddings, EmbeddingsAOAIAuthorization, PayloadText, AOAIOperationResponse, CallerModuleInfo, '') then begin
FeatureTelemetry.LogError('0000KVE', GetAzureOpenAICategory(), TelemetryGenerateEmbeddingLbl, EmbeddingsFailedWithCodeErr, '', Enum::"AL Telemetry Scope"::All, CustomDimensions);
exit;
end;
Expand Down Expand Up @@ -262,6 +262,7 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"
PayloadText, ToolChoice : Text;
MetapromptTokenCount: Integer;
PromptTokenCount: Integer;
AzureOpenAIPolicy: Text;
begin
GuiCheck(ChatCompletionsAOAIAuthorization);

Expand All @@ -271,6 +272,9 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"
CopilotCapabilityImpl.AddTelemetryCustomDimensions(CustomDimensions, CallerModuleInfo);

AOAIChatCompletionParams.AddChatCompletionsParametersToPayload(Payload);

AzureOpenAIPolicy := AOAIChatCompletionParams.GetAOAIPolicyParams().GetAOAIPolicy();

Payload.Add('messages', ChatMessages.AssembleHistory(MetapromptTokenCount, PromptTokenCount));

if ChatMessages.ToolsExists() then begin
Expand All @@ -293,7 +297,7 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"
Payload.WriteTo(PayloadText);

SendTokenCountTelemetry(MetapromptTokenCount, PromptTokenCount, CustomDimensions);
if not SendRequest(Enum::"AOAI Model Type"::"Chat Completions", ChatCompletionsAOAIAuthorization, PayloadText, AOAIOperationResponse, CallerModuleInfo) then begin
if not SendRequest(Enum::"AOAI Model Type"::"Chat Completions", ChatCompletionsAOAIAuthorization, PayloadText, AOAIOperationResponse, CallerModuleInfo, AzureOpenAIPolicy) then begin
FeatureTelemetry.LogError('0000KVF', GetAzureOpenAICategory(), TelemetryGenerateChatCompletionLbl, ChatCompletionsFailedWithCodeErr, '', Enum::"AL Telemetry Scope"::All, CustomDimensions);
exit;
end;
Expand Down Expand Up @@ -449,7 +453,7 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"

[TryFunction]
[NonDebuggable]
local procedure SendRequest(ModelType: Enum "AOAI Model Type"; AOAIAuthorization: Codeunit "AOAI Authorization"; Payload: Text; var AOAIOperationResponse: Codeunit "AOAI Operation Response"; CallerModuleInfo: ModuleInfo)
local procedure SendRequest(ModelType: Enum "AOAI Model Type"; AOAIAuthorization: Codeunit "AOAI Authorization"; Payload: Text; var AOAIOperationResponse: Codeunit "AOAI Operation Response"; CallerModuleInfo: ModuleInfo; AzureOpenAIPolicy: Text)
var
CopilotNotifications: Codeunit "Copilot Notifications";
ALCopilotAuthorization: DotNet ALCopilotAuthorization;
Expand Down Expand Up @@ -483,7 +487,7 @@ codeunit 7772 "Azure OpenAI Impl" implements "AI Service Name"
Enum::"AOAI Model Type"::Embeddings:
ALCopilotOperationResponse := ALCopilotFunctions.GenerateEmbedding(Payload, ALCopilotAuthorization, ALCopilotCapability);
Enum::"AOAI Model Type"::"Chat Completions":
ALCopilotOperationResponse := ALCopilotFunctions.GenerateChatCompletion(Payload, ALCopilotAuthorization, ALCopilotCapability);
ALCopilotOperationResponse := ALCopilotFunctions.GenerateChatCompletion(Payload, ALCopilotAuthorization, ALCopilotCapability, AzureOpenAIPolicy);
else
Error(InvalidModelTypeErr)
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
InherentPermissions = X;

var
AOAIPolicyParams: Codeunit "AOAI Policy Params";
Initialized: Boolean;
Temperature: Decimal;
MaxTokens: Integer;
Expand Down Expand Up @@ -71,6 +72,22 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
exit(FrequencyPenalty);
end;

procedure GetAOAIPolicyParams(): Codeunit "AOAI Policy Params"
begin
if not Initialized then
InitializeDefaults();

exit(AOAIPolicyParams);
end;

procedure SetAOAIPolicyParams(NewAOAIPolicyParams: Codeunit "AOAI Policy Params")
begin
if not Initialized then
InitializeDefaults();

AOAIPolicyParams := NewAOAIPolicyParams;
end;

procedure SetTemperature(NewTemperature: Decimal)
begin
if not Initialized then
Expand Down Expand Up @@ -149,6 +166,8 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
end;

local procedure InitializeDefaults()
var
DefaultPolicyParams: Codeunit "AOAI Policy Params";
begin
Initialized := true;

Expand All @@ -158,5 +177,8 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
SetMaxTokens(0);
SetMaxHistory(10);
SetJsonMode(false);

DefaultPolicyParams.InitializeDefaults();
SetAOAIPolicyParams(DefaultPolicyParams);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ codeunit 7761 "AOAI Chat Completion Params"
exit(AOAIChatComplParamsImpl.GetFrequencyPenalty());
end;

/// <summary>
/// Gets the current AOAI policy parameters containing harms severity and XPIA detection settings.
/// </summary>
/// <returns>The AOAI policy parameters.</returns>
procedure GetAOAIPolicyParams(): Codeunit "AOAI Policy Params"
begin
exit(AOAIChatComplParamsImpl.GetAOAIPolicyParams());
end;

/// <summary>
/// Sets the sampling temperature to use, between 0 and 2. A higher temperature increases the likelihood that the next most probable token will not be selected. When requesting structured data, set the temperature to 0. For human sounding speech, 0.7 is a typical value
/// </summary>
Expand Down Expand Up @@ -127,6 +136,15 @@ codeunit 7761 "AOAI Chat Completion Params"
AOAIChatComplParamsImpl.SetFrequencyPenalty(NewFrequencyPenalty);
end;

/// <summary>
/// Sets the AOAI policy parameters containing harms severity and XPIA detection settings.
/// </summary>
/// <param name="PolicyParams">The AOAI policy parameters containing harms severity and XPIA detection settings.</param>
procedure SetAOAIPolicyParams(PolicyParams: Codeunit "AOAI Policy Params")
begin
AOAIChatComplParamsImpl.SetAOAIPolicyParams(PolicyParams);
end;

/// <summary>
/// Adds the Chat Completion parameters to the payload.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ codeunit 7763 "AOAI Chat Messages"
exit(AOAIToolsImpl.GetToolInvokePreference());
end;

/// <summary>
/// Adds XPIA Detection tags to an Input text.
/// </summary>
/// <returns>Input text with XPIA Detection tags added.</returns>
/// <remarks>Please make sure to configure the AOAI policy to support XPIA detection before using this method. Also, remember to add the 'Input text with XPIA detection tags added' to a message.</remarks>
[NonDebuggable]
procedure AddXPIADetectionTags(var Input: Text)
begin
AOAIChatMessagesImpl.AddXPIADetectionTags(Input);
end;

/// <summary>
/// Prepares the Tools to be sent to the deployment model.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ codeunit 7764 "AOAI Chat Messages Impl"
MessagesTokenCount := AOAIToken.GetGPT4TokenCount(TotalMessages);
end;

procedure AddXPIADetectionTags(var Input: Text)
begin
Input := '"""<documents>' + Input + '</documents>""" End';
end;

local procedure Initialize()
begin
if Initialized then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ------------------------------------------------------------------------------------------------
// 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;

enum 7787 "AOAI Policy"
{
Extensible = false;
Access = Internal;

/// <summary>
/// Requests containing harms with a low severity are blocked and XPIA detection is enabled.
/// </summary>
value(1; "ConservativeWithXPIA")
{
Caption = 'ConservativeWithXPIA', Locked = true;
}

/// <summary>
/// Requests containing harms with a low severity are blocked and XPIA detection is disabled.
/// </summary>
value(2; "ConservativeWithoutXPIA")
{
Caption = 'ConservativeWithoutXPIA', Locked = true;
}

/// <summary>
/// Requests containing harms with a medium severity are blocked and XPIA detection is enabled.
/// </summary>
value(3; "MediumWithXPIA")
{
Caption = 'MediumWithXPIA', Locked = true;
}

/// <summary>
/// Requests containing harms with a medium severity are blocked and XPIA detection is disabled.
/// </summary>
value(4; "MediumWithoutXPIA")
{
Caption = 'Default', Locked = true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace System.AI;

enum 7788 "AOAI Policy Harms Severity"
{
Extensible = false;

/// <summary>
/// Recommended. Strictest policy controls: Requests containing harms with a low severity are blocked.
/// For more information, see https://learn.microsoft.com/en-us/azure/ai-foundry/openai/concepts/content-filter-severity-levels?view=foundry-classic&amp;tabs=definitions#hate-and-fairness-severity-levels
/// </summary>
value(1; Low)
{
Caption = 'Low', Locked = true;
}

/// <summary>
/// Moderately strict policy controls: Requests containing harms with a medium severity are blocked.
/// For more information, see https://learn.microsoft.com/en-us/azure/ai-foundry/openai/concepts/content-filter-severity-levels?view=foundry-classic&amp;tabs=definitions#hate-and-fairness-severity-levels
/// </summary>
value(2; Medium)
{
Caption = 'Medium', Locked = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// ------------------------------------------------------------------------------------------------
// 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>
/// Represents the AOAI Policy management for combining Harms Severity and XPIA Detection settings.
/// </summary>
codeunit 7787 "AOAI Policy Params"
{
InherentEntitlements = X;
InherentPermissions = X;

var
AOAIPolicyParamsImpl: Codeunit "AOAI Policy Params Impl";

/// <summary>
/// Gets the current AOAI Policy Harms Severity setting.
/// </summary>
/// <returns>The current AOAI Policy Harms Severity.</returns>
procedure GetHarmsSeverity(): Enum "AOAI Policy Harms Severity"
begin
exit(AOAIPolicyParamsImpl.GetHarmsSeverity());
end;

/// <summary>
/// Gets the current AOAI Policy XPIA Detection setting.
/// </summary>
/// <returns>Gets the status of AOAI Policy XPIA Detection.</returns>
procedure GetXPIADetection(): Boolean
begin
exit(AOAIPolicyParamsImpl.GetXPIADetection());
end;

/// <summary>
/// Sets the AOAI Policy Harms Severity.
/// </summary>
/// <param name="HarmsSeverity">The AOAI Policy Harms Severity to set.</param>
procedure SetHarmsSeverity(HarmsSeverity: Enum "AOAI Policy Harms Severity")
begin
AOAIPolicyParamsImpl.SetHarmsSeverity(HarmsSeverity);
end;

/// <summary>
/// Sets the AOAI Policy XPIA Detection.
/// </summary>
/// <param name="IsEnabled">Enable/Disable AOAI Policy XPIA Detection</param>
/// <remarks>When XPIA detection is enabled, use <see cref="AOAI Chat Messages.EnforceXPIADetection"/> to mark messages for XPIA detection.</remarks>
procedure SetXPIADetection(IsEnabled: Boolean)
begin
AOAIPolicyParamsImpl.SetXPIADetection(IsEnabled);
end;

/// <summary>
/// Gets the value for the custom AOAI policy, if set using SetCustomAOAIPolicy
/// </summary>
procedure GetCustomAOAIPolicy(): Text
begin
exit(AOAIPolicyParamsImpl.GetCustomAOAIPolicy());
end;

/// <summary>
/// Sets a Custom AOAI policy.
/// </summary>
procedure SetCustomAOAIPolicy(CustomAOAIPolicyParams: Text)
begin
AOAIPolicyParamsImpl.SetCustomAOAIPolicy(CustomAOAIPolicyParams);
end;

/// <summary>
/// Initializes the AOAI Policy parameters to their default values.
/// </summary>
procedure InitializeDefaults()
begin
AOAIPolicyParamsImpl.InitializeDefaults();
end;

/// <summary>
/// Gets the AOAI policy as text based on the provided policy parameters or a Custom one.
/// </summary>
internal procedure GetAOAIPolicy(): Text
begin
exit(AOAIPolicyParamsImpl.GetAOAIPolicy());
end;
}
Loading
Loading