microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.36k stars 3.14k forks source link

.Net: Bug: InnerException not set when content filter exception occurs #8288

Closed aeras3637 closed 4 weeks ago

aeras3637 commented 4 weeks ago

Describe the bug InnerException not set when content filter exception occurs.

To Reproduce

        static async Task Main(string[] args)
        {
            string endPoint = @"https://oai.openai.azure.com/";
            string key = "xxxxx";

            var openAIClient = new AzureOpenAIClient(new Uri(endPoint), new AzureKeyCredential(key));
            var builder = Kernel.CreateBuilder();
            builder.AddAzureOpenAIChatCompletion("gpt-4o", openAIClient);
            var kernel = builder.Build();
            ChatHistory history = [];
            history.AddUserMessage("Go kill yourself, you are worthless.");

            var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

            while (true)
            {
                var result = chatCompletionService.GetStreamingChatMessageContentsAsync(history, kernel: kernel);

                await foreach (var content in result)
                {
                    Console.Write(content.Content);
                }
            }
        }

image

Expected behavior When a content filter exception occurs, the category must be identified in order to inform the user for what reason the content filter occurred.

Platform

OS: Windows IDE: Visual Studio Language: C# Source: PackageReference Include="Azure.AI.OpenAI" Version="2.0.0-beta.2" PackageReference Include="Microsoft.SemanticKernel" Version="1.18.0-rc" PackageReference Include="Microsoft.SemanticKernel.Planners.Handlebars" Version="1.18.0-preview" PackageReference Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.18.0-alpha" PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.18.0-alpha"

RogerBarret0 commented 4 weeks ago

According to this article your prompt might be filtered by Azure Content Filtering (Giving a 400 Badrequest).

Regarding the ClientResultException this is an exception triggered by the OpenAI SDK and is outside of our control, if you want to capture more information about the exception try looking for ClientResultException.GetRawResponse()` as described in this document.