From c78aade96fbcc0218d89438c18a8eacf7dfc3e86 Mon Sep 17 00:00:00 2001 From: Craig Loewen Date: Fri, 22 Nov 2024 00:00:08 -0600 Subject: [PATCH] Added an explicit OpenAI moderation call to Advanced Paste --- Directory.Packages.props | 1 + .../AdvancedPaste/AdvancedPaste.csproj | 1 + .../Helpers/AICompletionsHelper.cs | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index a0d29eb311..e6680a6590 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -57,6 +57,7 @@ + diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPaste.csproj b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPaste.csproj index c8ea965a4d..f021076e98 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPaste.csproj +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPaste.csproj @@ -62,6 +62,7 @@ + diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AICompletionsHelper.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AICompletionsHelper.cs index d9586638b8..2b4ebe3bee 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AICompletionsHelper.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/AICompletionsHelper.cs @@ -12,6 +12,7 @@ using Azure.AI.OpenAI; using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Telemetry; +using OpenAI.Moderations; using Windows.Security.Credentials; namespace AdvancedPaste.Helpers @@ -76,13 +77,28 @@ namespace AdvancedPaste.Helpers { OpenAIClient azureAIClient = new OpenAIClient(_openAIKey); + string inputString = systemInstructions + "\n\n" + userMessage; + + ModerationClient moderationClient = new("omni-moderation-latest", _openAIKey); + + // TODO: Run this as async along with the chat completion result to maintain speed + ModerationResult moderationResult = moderationClient.ClassifyText(inputString); + + if (moderationResult.Flagged) + { +#pragma warning disable CA2201 // Use explicit type + // TODO: Use a more explicit type and handle the error more gracefully + throw new Exception("Flagged by moderation"); +#pragma warning restore CA2201 // Use explicit type + } + var response = azureAIClient.GetCompletions( new CompletionsOptions() { DeploymentName = _modelName, Prompts = { - systemInstructions + "\n\n" + userMessage, + inputString, }, Temperature = 0.01F, MaxTokens = 2000,