-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathDocumentTokenChunker.cs
More file actions
163 lines (141 loc) · 6.12 KB
/
DocumentTokenChunker.cs
File metadata and controls
163 lines (141 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Microsoft.ML.Tokenizers;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.DataIngestion.Chunkers
{
/// <summary>
/// Processes a document by tokenizing its content and dividing it into overlapping chunks of tokens.
/// </summary>
/// <remarks>
/// <para>This class uses a tokenizer to convert the document's content into tokens and then splits the
/// tokens into chunks of a specified size, with a configurable overlap between consecutive chunks.</para>
/// <para>Note that tables may be split mid-row.</para>
/// </remarks>
public sealed class DocumentTokenChunker : IngestionChunker<string>
{
private readonly Tokenizer _tokenizer;
private readonly int _maxTokensPerChunk;
private readonly int _chunkOverlap;
/// <summary>
/// Initializes a new instance of the <see cref="DocumentTokenChunker"/> class with the specified options.
/// </summary>
/// <param name="options">The options used to configure the chunker, including tokenizer and chunk sizes.</param>
public DocumentTokenChunker(IngestionChunkerOptions options)
{
_ = Throw.IfNull(options);
_tokenizer = options.Tokenizer;
_maxTokensPerChunk = options.MaxTokensPerChunk;
_chunkOverlap = options.OverlapTokens;
}
/// <inheritdoc/>
public override async IAsyncEnumerable<IngestionChunk<string>> ProcessAsync(IngestionDocument document, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
_ = Throw.IfNull(document);
int stringBuilderTokenCount = 0;
StringBuilder stringBuilder = new();
Dictionary<string, object>? accumulatedMetadata = null;
foreach (IngestionDocumentElement element in document.EnumerateContent())
{
cancellationToken.ThrowIfCancellationRequested();
string? elementContent = element.GetSemanticContent();
if (string.IsNullOrEmpty(elementContent))
{
continue;
}
AccumulateMetadata(element, ref accumulatedMetadata);
int contentToProcessTokenCount = _tokenizer.CountTokens(elementContent!, considerNormalization: false);
ReadOnlyMemory<char> contentToProcess = elementContent.AsMemory();
while (stringBuilderTokenCount + contentToProcessTokenCount >= _maxTokensPerChunk)
{
int index = _tokenizer.GetIndexByTokenCount(
text: contentToProcess.Span,
maxTokenCount: _maxTokensPerChunk - stringBuilderTokenCount,
out string? _,
out int _,
considerNormalization: false);
unsafe
{
fixed (char* ptr = &MemoryMarshal.GetReference(contentToProcess.Span))
{
_ = stringBuilder.Append(ptr, index);
}
}
yield return FinalizeChunk(ref accumulatedMetadata);
contentToProcess = contentToProcess.Slice(index);
contentToProcessTokenCount = _tokenizer.CountTokens(contentToProcess.Span, considerNormalization: false);
}
_ = stringBuilder.Append(contentToProcess);
stringBuilderTokenCount += contentToProcessTokenCount;
}
if (stringBuilder.Length > 0)
{
yield return FinalizeChunk(ref accumulatedMetadata);
}
yield break;
IngestionChunk<string> FinalizeChunk(ref Dictionary<string, object>? metadata)
{
IngestionChunk<string> chunk = new IngestionChunk<string>(
content: stringBuilder.ToString(),
document: document,
context: string.Empty);
if (metadata is { Count: > 0 })
{
foreach (var kvp in metadata)
{
chunk.Metadata[kvp.Key] = kvp.Value;
}
metadata = null;
}
_ = stringBuilder.Clear();
stringBuilderTokenCount = 0;
if (_chunkOverlap > 0)
{
int index = _tokenizer.GetIndexByTokenCountFromEnd(
text: chunk.Content,
maxTokenCount: _chunkOverlap,
out string? _,
out stringBuilderTokenCount,
considerNormalization: false);
ReadOnlySpan<char> overlapContent = chunk.Content.AsSpan().Slice(index);
unsafe
{
fixed (char* ptr = &MemoryMarshal.GetReference(overlapContent))
{
_ = stringBuilder.Append(ptr, overlapContent.Length);
}
}
}
return chunk;
}
}
private static void AccumulateMetadata(IngestionDocumentElement element, ref Dictionary<string, object>? accumulated)
{
if (!element.HasMetadata)
{
return;
}
accumulated ??= [];
foreach (var kvp in element.Metadata)
{
if (kvp.Value is not null)
{
#if NET
accumulated.TryAdd(kvp.Key, kvp.Value);
#else
if (!accumulated.ContainsKey(kvp.Key))
{
accumulated[kvp.Key] = kvp.Value;
}
#endif
}
}
}
}
}