From 55cd8d4b242673ff4d2aaf68b9a69662fd0063b2 Mon Sep 17 00:00:00 2001 From: Mario Ramos Date: Wed, 14 Aug 2024 12:46:03 +0200 Subject: [PATCH 1/3] Check FinishReason in `SkVisionImageDocumentConnector` --- .../SkVisionImageDocumentConnector.cs | 9 ++++ .../Exceptions/DocumentTooLargeException.cs | 47 +++++++++++++++++++ .../IServiceCollectionExtensions.cs | 10 ++-- .../Resources/ExceptionMessages.Designer.cs | 9 ++++ .../Resources/ExceptionMessages.resx | 3 ++ 5 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Exceptions/DocumentTooLargeException.cs diff --git a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Connectors/SkVisionImageDocumentConnector.cs b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Connectors/SkVisionImageDocumentConnector.cs index 15bbfc4..57f4dcc 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Connectors/SkVisionImageDocumentConnector.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Connectors/SkVisionImageDocumentConnector.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Diagnostics; +using Encamina.Enmarcha.SemanticKernel.Connectors.Document.Exceptions; using Encamina.Enmarcha.SemanticKernel.Connectors.Document.Utils; using Microsoft.SemanticKernel; @@ -81,6 +82,14 @@ public virtual string ReadText(Stream stream) // TODO: We can improve that making an async version of IEnmarchaDocumentConnector. var response = chatCompletionService.GetChatMessageContentAsync(history).GetAwaiter().GetResult(); + // Check if the the model has exceeded the output capacity. + if (response.Metadata?.TryGetValue(@"FinishReason", out var finishReason) == true && + finishReason is string finishReasonString && + finishReasonString.Equals(@"length", StringComparison.Ordinal)) + { + throw new DocumentTooLargeException(); + } + return response?.Content ?? string.Empty; } diff --git a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Exceptions/DocumentTooLargeException.cs b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Exceptions/DocumentTooLargeException.cs new file mode 100644 index 0000000..2fbb8c1 --- /dev/null +++ b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Exceptions/DocumentTooLargeException.cs @@ -0,0 +1,47 @@ +using System.Runtime.Serialization; + +namespace Encamina.Enmarcha.SemanticKernel.Connectors.Document.Exceptions; + +/// +/// The exception that is thrown when there has been an error with the document size. +/// +[Serializable] +public class DocumentTooLargeException : Exception +{ + /// + /// Initializes a new instance of the class. + /// + public DocumentTooLargeException() : base(Resources.ExceptionMessages.DocumentTooLargeExceptionMessage) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes this exception. + public DocumentTooLargeException(string message) : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes this exception. + /// + /// The exception that is the cause of the current exception. + /// If the parameter is not , then the + /// current exception is raised in a catch block that handles the inner exception. + /// + public DocumentTooLargeException(string message, Exception innerException) : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The object that holds the serialized object data. + /// The contextual information about the source or destination. + protected DocumentTooLargeException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } +} diff --git a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Extensions/IServiceCollectionExtensions.cs b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Extensions/IServiceCollectionExtensions.cs index 81d51ab..c8d62bf 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Extensions/IServiceCollectionExtensions.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Extensions/IServiceCollectionExtensions.cs @@ -17,7 +17,7 @@ public static class IServiceCollectionExtensions /// The so that additional calls can be chained. public static IServiceCollection AddDefaultDocumentContentExtractor(this IServiceCollection services) { - return services.AddSingleton(); + return services.AddScoped(); } /// @@ -33,13 +33,13 @@ public static IServiceCollection AddDefaultDocumentContentExtractor(this IServic } /// - /// Adds a default implementation of Semantic to the specified as a singleton service. + /// Adds a default implementation of Semantic to the specified as a scoped service. /// /// The to add services to. /// The so that additional calls can be chained. public static IServiceCollection AddDefaultDocumentContentSemanticExtractor(this IServiceCollection services) { - return services.AddDefaultDocumentContentSemanticExtractor(ServiceLifetime.Singleton); + return services.AddDefaultDocumentContentSemanticExtractor(ServiceLifetime.Scoped); } /// @@ -60,7 +60,7 @@ public static IServiceCollection AddDefaultDocumentContentSemanticExtractor(this /// The so that additional calls can be chained. public static IServiceCollection AddDefaultDocumentConnectorProvider(this IServiceCollection services) { - return services.AddSingleton(); + return services.AddScoped(); } /// @@ -202,7 +202,7 @@ public static IServiceCollection AddCsvTsvDocumentConnector(this IServiceCollect /// The so that additional calls can be chained. public static IServiceCollection AddSkVisionImageDocumentConnector(this IServiceCollection services) { - return services.AddSingleton(); + return services.AddScoped(); } /// diff --git a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.Designer.cs b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.Designer.cs index dc48578..b9586fd 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.Designer.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.Designer.cs @@ -60,6 +60,15 @@ internal ExceptionMessages() { } } + /// + /// Looks up a localized string similar to The document is too large or contains too much content.. + /// + internal static string DocumentTooLargeExceptionMessage { + get { + return ResourceManager.GetString("DocumentTooLargeExceptionMessage", resourceCulture); + } + } + /// /// Looks up a localized string similar to File extension '{0}' is not supported!. /// diff --git a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.resx b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.resx index c22db3a..32fd933 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.resx +++ b/src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Resources/ExceptionMessages.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + The document is too large or contains too much content. + File extension '{0}' is not supported! From ca86618eef0b36e17af5db8c8f4ccaa18fdb55ac Mon Sep 17 00:00:00 2001 From: Mario Ramos Date: Wed, 14 Aug 2024 13:21:31 +0200 Subject: [PATCH 2/3] Bump version to 8.1.8 preview-04 --- CHANGELOG.md | 1 + Directory.Build.props | 2 +- .../Encamina.Enmarcha.SemanticKernel.Connectors.Document.csproj | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7917f72..28e14e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Previous classification is not required if changes are simple or all belong to t ### Minor Changes - Added `SetRecipients` method to `IEmailBuilder` interface. +- Added `DocumentTooLargeException` class to handle exceptions when the document is too large to be processed. ## [8.1.7] diff --git a/Directory.Build.props b/Directory.Build.props index be88312..1fc4d74 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -17,7 +17,7 @@ 8.1.8 - preview-03 + preview-04