Skip to content
Draft
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 @@ -11,13 +11,15 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
InherentPermissions = X;

var
AOAIPolicyParams: Codeunit "AOAI Policy Params";
Initialized: Boolean;
Temperature: Decimal;
MaxTokens: Integer;
JsonMode: Boolean;
MaxHistory: Integer;
PresencePenalty: Decimal;
FrequencyPenalty: Decimal;

TemperatureErr: Label 'Temperature must be between 0.0 and 2.0.';
PresencePenaltyErr: Label 'Presence penalty must be between -2.0 and 2.0.';
FrequencyPenaltyErr: Label 'Frequency penalty must be between -2.0 and 2.0.';
Expand Down Expand Up @@ -71,6 +73,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 @@ -138,6 +156,7 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
Payload.Add('temperature', GetTemperature());
Payload.Add('presence_penalty', GetPresencePenalty());
Payload.Add('frequency_penalty', GetFrequencyPenalty());
Payload.Add('aoai_policy', Format(AOAIPolicyParams.GetAOAIPolicy()));

if IsJsonMode() then
Payload.Add('response_format', GetJsonResponseFormat());
Expand All @@ -149,6 +168,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 +179,9 @@ codeunit 7762 "AOAI Chat Compl Params Impl"
SetMaxTokens(0);
SetMaxHistory(10);
SetJsonMode(false);

DefaultPolicyParams.SetHarmsSeverity("AOAI Policy Harms Severity"::Low);
DefaultPolicyParams.SetXPIADetection(true);
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,15 @@ codeunit 7763 "AOAI Chat Messages"
exit(AOAIToolsImpl.GetToolInvokePreference());
end;

/// <summary>
/// Enforce XPIA Detection for an Input text.
/// </summary>
[NonDebuggable]
procedure EnforceXPIADetection(var Input: Text)
begin
AOAIChatMessagesImpl.EnforceXPIADetection(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,12 @@ codeunit 7764 "AOAI Chat Messages Impl"
MessagesTokenCount := AOAIToken.GetGPT4TokenCount(TotalMessages);
end;

[NonDebuggable]
procedure EnforceXPIADetection(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,39 @@
// ------------------------------------------------------------------------------------------------
// 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>
/// Low harms severity with XPIA detection enabled
/// </summary>
value(1; "ConservativeWithXPIA")
{
}

/// <summary>
/// Low harms severity with XPIA detection disabled
/// </summary>
value(2; "Conservative")
{
}

/// <summary>
/// Medium harms severity with XPIA detection enabled
/// </summary>
value(3; "MediumWithXPIA")
{
}

/// <summary>
/// Medium harms severity with XPIA detection disabled
/// </summary>
value(4; "Default")
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace System.AI;

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

/// <summary>
/// Applies the strictest policy controls.
/// </summary>
value(1; Low)
{
}

/// <summary>
/// Applies moderately strict policy controls.
/// </summary>
value(2; Medium)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ------------------------------------------------------------------------------------------------
// 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 AOAI policy enum based on the provided policy parameters.
/// </summary>
/// <returns>The corresponding AOAI policy enum value.</returns>
internal procedure GetAOAIPolicy(): Enum "AOAI Policy"
begin
exit(AOAIPolicyParamsImpl.GetAOAIPolicy());
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ------------------------------------------------------------------------------------------------
// 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;

codeunit 7788 "AOAI Policy Params Impl"
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;

var
HarmsSeverity: Enum "AOAI Policy Harms Severity";
IsXPIADetectionEnabled: Boolean;
Initialized: Boolean;

procedure GetHarmsSeverity(): Enum "AOAI Policy Harms Severity"
begin
if not Initialized then
InitializeDefaults();

exit(HarmsSeverity);
end;

procedure GetXPIADetection(): Boolean
begin
if not Initialized then
InitializeDefaults();

exit(IsXPIADetectionEnabled);
end;

procedure SetHarmsSeverity(NewHarmsSeverity: Enum "AOAI Policy Harms Severity")
begin
if not Initialized then
InitializeDefaults();

HarmsSeverity := NewHarmsSeverity;
end;

procedure SetXPIADetection(IsEnabled: Boolean)
begin
if not Initialized then
InitializeDefaults();

IsXPIADetectionEnabled := IsEnabled;
end;

internal procedure GetAOAIPolicy(): Enum "AOAI Policy"
var
AOAIPolicyHarmsSeverity: Enum "AOAI Policy Harms Severity";
AOAIPolicyXPIADetection: Boolean;
CombinationKey: Text;
begin
AOAIPolicyHarmsSeverity := GetHarmsSeverity();
AOAIPolicyXPIADetection := GetXPIADetection();

// Create readable combination key
CombinationKey := 'Harms' + Format(AOAIPolicyHarmsSeverity) + '|XPIA' + (AOAIPolicyXPIADetection = true ? 'Enabled' : 'Disabled');

case CombinationKey of
'HarmsLow|XPIAEnabled':
exit("AOAI Policy"::"ConservativeWithXPIA");
'HarmsLow|XPIADisabled':
exit("AOAI Policy"::"Conservative");
'HarmsMedium|XPIAEnabled':
exit("AOAI Policy"::"MediumWithXPIA");
'HarmsMedium|XPIADisabled':
exit("AOAI Policy"::"Default");
end;
end;

local procedure InitializeDefaults()
begin
Initialized := true;
HarmsSeverity := "AOAI Policy Harms Severity"::Low;
IsXPIADetectionEnabled := true;
end;
}
Loading