-
Notifications
You must be signed in to change notification settings - Fork 849
Introduce set of built-in Enrichers #6957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0285046
move code as is
adamsitnik 08a894d
solve the warnings
adamsitnik 4ebb495
avoid the need of using structured input and dependency on MEAI (MEAI…
adamsitnik 9bee9a2
add tests
adamsitnik 6248fba
add note about defaults
adamsitnik 991f649
Apply suggestions from code review
adamsitnik d6cb3e7
Add warning suppression for IDisposable implementation for the test p…
adamsitnik a13954f
address code review feedback:
adamsitnik 8e6e3de
address code review feedback: reject invalid keywords/classes, improv…
adamsitnik 11ada2b
address code review feedback
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/Libraries/Microsoft.Extensions.DataIngestion/Processors/ClassificationEnricher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Frozen; | ||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Extensions.DataIngestion; | ||
|
|
||
| /// <summary> | ||
| /// Enriches document chunks with a classification label based on their content. | ||
| /// </summary> | ||
| /// <remarks>This class uses a chat-based language model to analyze the content of document chunks and assign a | ||
| /// single, most relevant classification label. The classification is performed using a predefined set of classes, with | ||
| /// an optional fallback class for cases where no suitable classification can be determined.</remarks> | ||
| public sealed class ClassificationEnricher : IngestionChunkProcessor<string> | ||
| { | ||
| private readonly IChatClient _chatClient; | ||
| private readonly ChatOptions? _chatOptions; | ||
| private readonly FrozenSet<string> _predefinedClasses; | ||
| private readonly ChatMessage _systemPrompt; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ClassificationEnricher"/> class. | ||
| /// </summary> | ||
| /// <param name="chatClient">The chat client used for classification.</param> | ||
| /// <param name="predefinedClasses">The set of predefined classification classes.</param> | ||
| /// <param name="chatOptions">Options for the chat client.</param> | ||
| /// <param name="fallbackClass">The fallback class to use when no suitable classification is found. When not provided, it defaults to "Unknown".</param> | ||
| public ClassificationEnricher(IChatClient chatClient, ReadOnlySpan<string> predefinedClasses, | ||
| ChatOptions? chatOptions = null, string? fallbackClass = null) | ||
| { | ||
| _chatClient = Throw.IfNull(chatClient); | ||
| _chatOptions = chatOptions; | ||
| if (string.IsNullOrWhiteSpace(fallbackClass)) | ||
| { | ||
| fallbackClass = "Unknown"; | ||
| } | ||
|
|
||
| _predefinedClasses = CreatePredefinedSet(predefinedClasses, fallbackClass!); | ||
| _systemPrompt = CreateSystemPrompt(predefinedClasses, fallbackClass!); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the metadata key used to store the classification. | ||
| /// </summary> | ||
| public static string MetadataKey => "classification"; | ||
|
|
||
| /// <inheritdoc /> | ||
| public override async IAsyncEnumerable<IngestionChunk<string>> ProcessAsync(IAsyncEnumerable<IngestionChunk<string>> chunks, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
| { | ||
| _ = Throw.IfNull(chunks); | ||
|
|
||
| await foreach (IngestionChunk<string> chunk in chunks.WithCancellation(cancellationToken)) | ||
| { | ||
| var response = await _chatClient.GetResponseAsync( | ||
| [ | ||
| _systemPrompt, | ||
| new(ChatRole.User, chunk.Content) | ||
| ], _chatOptions, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
|
||
| chunk.Metadata[MetadataKey] = _predefinedClasses.Contains(response.Text) | ||
| ? response.Text | ||
| : throw new InvalidOperationException($"Classification returned an unexpected class: '{response.Text}'."); | ||
|
|
||
| yield return chunk; | ||
| } | ||
| } | ||
|
|
||
| private static FrozenSet<string> CreatePredefinedSet(ReadOnlySpan<string> predefinedClasses, string fallbackClass) | ||
| { | ||
| if (predefinedClasses.Length == 0) | ||
| { | ||
| Throw.ArgumentException(nameof(predefinedClasses), "Predefined classes must be provided."); | ||
| } | ||
|
|
||
| HashSet<string> predefinedClassesSet = new(StringComparer.Ordinal) { fallbackClass }; | ||
| foreach (string predefinedClass in predefinedClasses) | ||
| { | ||
| #if NET | ||
| if (predefinedClass.Contains(',', StringComparison.Ordinal)) | ||
| #else | ||
| if (predefinedClass.IndexOf(',') >= 0) | ||
| #endif | ||
| { | ||
| Throw.ArgumentException(nameof(predefinedClasses), $"Predefined class '{predefinedClass}' must not contain ',' character."); | ||
| } | ||
|
|
||
| if (!predefinedClassesSet.Add(predefinedClass)) | ||
| { | ||
| if (predefinedClass.Equals(fallbackClass, StringComparison.Ordinal)) | ||
| { | ||
| Throw.ArgumentException(nameof(predefinedClasses), $"Fallback class '{fallbackClass}' must not be one of the predefined classes."); | ||
| } | ||
|
|
||
| Throw.ArgumentException(nameof(predefinedClasses), $"Duplicate class found: '{predefinedClass}'."); | ||
| } | ||
| } | ||
|
|
||
| return predefinedClassesSet.ToFrozenSet(); | ||
| } | ||
|
|
||
| private static ChatMessage CreateSystemPrompt(ReadOnlySpan<string> predefinedClasses, string fallbackClass) | ||
| { | ||
| StringBuilder sb = new("You are a classification expert. Analyze the given text and assign a single, most relevant class. Use only the following predefined classes: "); | ||
|
|
||
| #if NET9_0_OR_GREATER | ||
| sb.AppendJoin(", ", predefinedClasses!); | ||
| #else | ||
| #pragma warning disable IDE0058 // Expression value is never used | ||
| for (int i = 0; i < predefinedClasses.Length; i++) | ||
| { | ||
| sb.Append(predefinedClasses[i]); | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (i < predefinedClasses.Length - 1) | ||
| { | ||
| sb.Append(", "); | ||
| } | ||
| } | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #endif | ||
| sb.Append(" and return ").Append(fallbackClass).Append(" when unable to classify."); | ||
| #pragma warning restore IDE0058 // Expression value is never used | ||
|
|
||
| return new(ChatRole.System, sb.ToString()); | ||
| } | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
src/Libraries/Microsoft.Extensions.DataIngestion/Processors/ImageAlternativeTextEnricher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Extensions.DataIngestion; | ||
|
|
||
| /// <summary> | ||
| /// Enriches <see cref="IngestionDocumentImage"/> elements with alternative text using an AI service, | ||
| /// so the generated embeddings can include the image content information. | ||
| /// </summary> | ||
| public sealed class ImageAlternativeTextEnricher : IngestionDocumentProcessor | ||
| { | ||
| private readonly IChatClient _chatClient; | ||
| private readonly ChatOptions? _chatOptions; | ||
| private readonly ChatMessage _systemPrompt; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ImageAlternativeTextEnricher"/> class. | ||
| /// </summary> | ||
| /// <param name="chatClient">The chat client used to get responses for generating alternative text.</param> | ||
| /// <param name="chatOptions">Options for the chat client.</param> | ||
| public ImageAlternativeTextEnricher(IChatClient chatClient, ChatOptions? chatOptions = null) | ||
| { | ||
| _chatClient = Throw.IfNull(chatClient); | ||
| _chatOptions = chatOptions; | ||
| _systemPrompt = new(ChatRole.System, "Write a detailed alternative text for this image with less than 50 words."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override async Task<IngestionDocument> ProcessAsync(IngestionDocument document, CancellationToken cancellationToken = default) | ||
| { | ||
| _ = Throw.IfNull(document); | ||
|
|
||
| foreach (var element in document.EnumerateContent()) | ||
| { | ||
| if (element is IngestionDocumentImage image) | ||
| { | ||
| await ProcessAsync(image, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| else if (element is IngestionDocumentTable table) | ||
| { | ||
| foreach (var cell in table.Cells) | ||
| { | ||
| if (cell is IngestionDocumentImage cellImage) | ||
| { | ||
| await ProcessAsync(cellImage, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return document; | ||
| } | ||
|
|
||
| private async Task ProcessAsync(IngestionDocumentImage image, CancellationToken cancellationToken) | ||
| { | ||
| if (image.Content.HasValue && !string.IsNullOrEmpty(image.MediaType) | ||
| && string.IsNullOrEmpty(image.AlternativeText)) | ||
| { | ||
| var response = await _chatClient.GetResponseAsync( | ||
| [ | ||
| _systemPrompt, | ||
| new(ChatRole.User, [new DataContent(image.Content.Value, image.MediaType!)]) | ||
| ], _chatOptions, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
|
||
| image.AlternativeText = response.Text; | ||
| } | ||
| } | ||
| } |
160 changes: 160 additions & 0 deletions
160
src/Libraries/Microsoft.Extensions.DataIngestion/Processors/KeywordEnricher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Frozen; | ||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Extensions.DataIngestion; | ||
|
|
||
| /// <summary> | ||
| /// Enriches chunks with keyword extraction using an AI chat model. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// It adds "keywords" metadata to each chunk. It's an array of strings representing the extracted keywords. | ||
| /// </remarks> | ||
| public sealed class KeywordEnricher : IngestionChunkProcessor<string> | ||
| { | ||
| private const int DefaultMaxKeywords = 5; | ||
| #if NET | ||
| private static readonly System.Buffers.SearchValues<char> _illegalCharacters = System.Buffers.SearchValues.Create([';', ',']); | ||
| #else | ||
| private static readonly char[] _illegalCharacters = [';', ',']; | ||
| #endif | ||
| private readonly IChatClient _chatClient; | ||
| private readonly ChatOptions? _chatOptions; | ||
| private readonly FrozenSet<string>? _predefinedKeywords; | ||
| private readonly ChatMessage _systemPrompt; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="KeywordEnricher"/> class. | ||
| /// </summary> | ||
| /// <param name="chatClient">The chat client used for keyword extraction.</param> | ||
| /// <param name="predefinedKeywords">The set of predefined keywords for extraction.</param> | ||
| /// <param name="chatOptions">Options for the chat client.</param> | ||
| /// <param name="maxKeywords">The maximum number of keywords to extract. When not provided, it defaults to 5.</param> | ||
| /// <param name="confidenceThreshold">The confidence threshold for keyword inclusion. When not provided, it defaults to 0.7.</param> | ||
| /// <remarks> | ||
| /// If no predefined keywords are provided, the model will extract keywords based on the content alone. | ||
| /// Such results may vary more significantly between different AI models. | ||
| /// </remarks> | ||
| public KeywordEnricher(IChatClient chatClient, ReadOnlySpan<string> predefinedKeywords, | ||
| ChatOptions? chatOptions = null, int? maxKeywords = null, double? confidenceThreshold = null) | ||
| { | ||
| _chatClient = Throw.IfNull(chatClient); | ||
| _chatOptions = chatOptions; | ||
| _predefinedKeywords = CreatePredfinedKeywords(predefinedKeywords); | ||
|
|
||
| double threshold = confidenceThreshold.HasValue | ||
| ? Throw.IfOutOfRange(confidenceThreshold.Value, 0.0, 1.0, nameof(confidenceThreshold)) | ||
| : 0.7; | ||
| int keywordsCount = maxKeywords.HasValue | ||
| ? Throw.IfLessThanOrEqual(maxKeywords.Value, 0, nameof(maxKeywords)) | ||
| : DefaultMaxKeywords; | ||
| _systemPrompt = CreateSystemPrompt(keywordsCount, predefinedKeywords, threshold); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the metadata key used to store the keywords. | ||
| /// </summary> | ||
| public static string MetadataKey => "keywords"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override async IAsyncEnumerable<IngestionChunk<string>> ProcessAsync(IAsyncEnumerable<IngestionChunk<string>> chunks, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
| { | ||
| _ = Throw.IfNull(chunks); | ||
|
|
||
| await foreach (IngestionChunk<string> chunk in chunks.WithCancellation(cancellationToken)) | ||
| { | ||
| // Structured response is not used here because it's not part of Microsoft.Extensions.AI.Abstractions. | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var response = await _chatClient.GetResponseAsync( | ||
| [ | ||
| _systemPrompt, | ||
| new(ChatRole.User, chunk.Content) | ||
| ], _chatOptions, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
|
||
| #pragma warning disable EA0009 // Use 'System.MemoryExtensions.Split' for improved performance | ||
| string[] keywords = response.Text.Split(';'); | ||
| if (_predefinedKeywords is not null) | ||
| { | ||
| foreach (var keyword in keywords) | ||
| { | ||
| if (!_predefinedKeywords.Contains(keyword)) | ||
| { | ||
| throw new InvalidOperationException($"The extracted keyword '{keyword}' is not in the predefined keywords list."); | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| chunk.Metadata[MetadataKey] = keywords; | ||
|
|
||
| yield return chunk; | ||
| } | ||
| } | ||
|
|
||
| private static FrozenSet<string>? CreatePredfinedKeywords(ReadOnlySpan<string> predefinedKeywords) | ||
| { | ||
| if (predefinedKeywords.Length == 0) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| HashSet<string> result = new(StringComparer.Ordinal); | ||
| foreach (string keyword in predefinedKeywords) | ||
| { | ||
| #if NET | ||
| if (keyword.AsSpan().ContainsAny(_illegalCharacters)) | ||
| #else | ||
| if (keyword.IndexOfAny(_illegalCharacters) >= 0) | ||
| #endif | ||
| { | ||
| Throw.ArgumentException(nameof(predefinedKeywords), $"Predefined keyword '{keyword}' contains an invalid character (';' or ',')."); | ||
| } | ||
|
|
||
| if (!result.Add(keyword)) | ||
| { | ||
| Throw.ArgumentException(nameof(predefinedKeywords), $"Duplicate keyword found: '{keyword}'"); | ||
| } | ||
| } | ||
|
|
||
| return result.ToFrozenSet(StringComparer.Ordinal); | ||
| } | ||
|
|
||
| private static ChatMessage CreateSystemPrompt(int maxKeywords, ReadOnlySpan<string> predefinedKeywords, double confidenceThreshold) | ||
| { | ||
| StringBuilder sb = new($"You are a keyword extraction expert. Analyze the given text and extract up to {maxKeywords} most relevant keywords. "); | ||
|
|
||
| if (predefinedKeywords.Length > 0) | ||
| { | ||
| #pragma warning disable IDE0058 // Expression value is never used | ||
| sb.Append("Focus on extracting keywords from the following predefined list: "); | ||
| #if NET9_0_OR_GREATER | ||
| sb.AppendJoin(", ", predefinedKeywords!); | ||
| #else | ||
| for (int i = 0; i < predefinedKeywords.Length; i++) | ||
| { | ||
| sb.Append(predefinedKeywords[i]); | ||
| if (i < predefinedKeywords.Length - 1) | ||
| { | ||
| sb.Append(", "); | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| sb.Append(". "); | ||
| } | ||
|
|
||
| sb.Append("Exclude keywords with confidence score below ").Append(confidenceThreshold).Append('.'); | ||
| sb.Append(" Return just the keywords separated with ';'."); | ||
| #pragma warning restore IDE0058 // Expression value is never used | ||
|
|
||
| return new(ChatRole.System, sb.ToString()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.