From 3d6cb2b4fc2af8c89110eef24489280644447fb1 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh Date: Wed, 19 Jul 2023 15:56:08 +0100 Subject: [PATCH] OpenAIInvalidResponseException is replaced by SKException --- .../Connectors.AI.OpenAI/AzureSdk/ClientBase.cs | 14 +++++++------- .../OpenAIInvalidResponseException{T}.cs | 17 ----------------- .../AzureOpenAIImageGeneration.cs | 6 +++--- 3 files changed, 10 insertions(+), 27 deletions(-) delete mode 100644 dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/OpenAIInvalidResponseException{T}.cs diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs index 8fb4870c79b1..dea5728bc952 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs @@ -59,14 +59,14 @@ public abstract class ClientBase if (response == null) { - throw new OpenAIInvalidResponseException(null, "Text completions null response"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Text completions null response"); } var responseData = response.Value; if (responseData.Choices.Count == 0) { - throw new OpenAIInvalidResponseException(responseData, "Text completions not found"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Text completions not found"); } return responseData.Choices.Select(choice => new TextResult(responseData, choice)).ToList(); @@ -119,12 +119,12 @@ await foreach (StreamingChoice choice in streamingChatCompletions.GetChoicesStre if (response == null) { - throw new OpenAIInvalidResponseException(null, "Text embedding null response"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Text embedding null response"); } if (response.Value.Data.Count == 0) { - throw new OpenAIInvalidResponseException(response.Value, "Text embedding not found"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Text embedding not found"); } EmbeddingItem x = response.Value.Data[0]; @@ -158,12 +158,12 @@ await foreach (StreamingChoice choice in streamingChatCompletions.GetChoicesStre if (response == null) { - throw new OpenAIInvalidResponseException(null, "Chat completions null response"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Chat completions null response"); } if (response.Value.Choices.Count == 0) { - throw new OpenAIInvalidResponseException(response.Value, "Chat completions not found"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Chat completions not found"); } return response.Value.Choices.Select(chatChoice => new ChatResult(response.Value, chatChoice)).ToList(); @@ -193,7 +193,7 @@ await foreach (StreamingChoice choice in streamingChatCompletions.GetChoicesStre if (response is null) { - throw new OpenAIInvalidResponseException(null, "Chat completions null response"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Chat completions null response"); } using StreamingChatCompletions streamingChatCompletions = response.Value; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/OpenAIInvalidResponseException{T}.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/OpenAIInvalidResponseException{T}.cs deleted file mode 100644 index 3b2c5042853a..000000000000 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/OpenAIInvalidResponseException{T}.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using Microsoft.SemanticKernel.AI; - -#pragma warning disable RCS1194 // Implement exception constructors. - -namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; - -internal sealed class OpenAIInvalidResponseException : AIException -{ - public T? ResponseData { get; } - - public OpenAIInvalidResponseException(T? responseData, string? message = null) : base(ErrorCodes.InvalidResponseContent, message) - { - this.ResponseData = responseData; - } -} diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ImageGeneration/AzureOpenAIImageGeneration.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ImageGeneration/AzureOpenAIImageGeneration.cs index 910ae36dbb99..fdd65f8f43b3 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ImageGeneration/AzureOpenAIImageGeneration.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ImageGeneration/AzureOpenAIImageGeneration.cs @@ -109,12 +109,12 @@ public async Task GenerateImageAsync(string description, int width, int if (result.Result == null) { - throw new AzureSdk.OpenAIInvalidResponseException(null, "Azure Image Generation null response"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Azure Image Generation null response"); } if (result.Result.Images.Count == 0) { - throw new AzureSdk.OpenAIInvalidResponseException(result, "Azure Image Generation result not found"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, "Azure Image Generation result not found"); } return result.Result.Images.First().Url; @@ -184,7 +184,7 @@ private async Task GetImageGenerationResultAsync(s } else if (this.IsFailedOrCancelled(result.Status)) { - throw new AzureSdk.OpenAIInvalidResponseException(result, $"Azure OpenAI image generation {result.Status}"); + throw new AIException(AIException.ErrorCodes.InvalidResponseContent, $"Azure OpenAI image generation {result.Status}"); } if (response.Headers.TryGetValues("retry-after", out var afterValues) && long.TryParse(afterValues.FirstOrDefault(), out var after))