diff --git a/src/Microsoft.ML.Tokenizers/Model/BPETokenizer.cs b/src/Microsoft.ML.Tokenizers/Model/BPETokenizer.cs index b9592d2e2b..7baa972819 100644 --- a/src/Microsoft.ML.Tokenizers/Model/BPETokenizer.cs +++ b/src/Microsoft.ML.Tokenizers/Model/BPETokenizer.cs @@ -280,6 +280,91 @@ public static async Task CreateAsync( return new BpeTokenizer(result.vocab, result.merges, preTokenizer, normalizer, specialTokens, unknownToken, continuingSubwordPrefix, endOfWordSuffix, fuseUnknownTokens); } + + /// + /// Create a new Bpe tokenizer object to use for text encoding. + /// + /// The JSON stream containing the dictionary of string keys and their ids. + /// The stream containing the tokens's pairs list. + /// The pre-tokenizer to use. + /// The normalizer to use. + /// The dictionary mapping special tokens to Ids. + /// The unknown token to be used by the model. + /// The prefix to attach to sub-word units that don’t represent a beginning of word. + /// The suffix to attach to sub-word units that represent an end of word. + /// Indicate whether allowing multiple unknown tokens get fused. + /// Indicate whether to handle the input text in byte level. + /// The beginning of sentence token. + /// The end of sentence token. + /// + /// When creating the tokenizer, ensure that the vocabulary stream is sourced from a trusted provider. + /// + public static BpeTokenizer Create( + Stream vocabStream, + Stream? mergesStream, + PreTokenizer? preTokenizer = null, + Normalizer? normalizer = null, + IReadOnlyDictionary? specialTokens = null, + string? unknownToken = null, + string? continuingSubwordPrefix = null, + string? endOfWordSuffix = null, + bool fuseUnknownTokens = false, + bool byteLevel = false, + string? beginningOfSentenceToken = null, + string? endOfSentenceToken = null) + { + if (vocabStream is null) + { + throw new ArgumentNullException(nameof(vocabStream)); + } + + (Dictionary? vocab, Vec<(string, string)> merges) result = ReadModelDataAsync(vocabStream, mergesStream, useAsync: false).GetAwaiter().GetResult(); + + return new BpeTokenizer(result.vocab, result.merges, preTokenizer, normalizer, specialTokens, unknownToken, continuingSubwordPrefix, endOfWordSuffix, fuseUnknownTokens, byteLevel, beginningOfSentenceToken, endOfSentenceToken); + } + + /// + /// Create a new Bpe tokenizer object asynchronously to use for text encoding. + /// + /// The JSON stream containing the dictionary of string keys and their ids. + /// The stream containing the tokens's pairs list. + /// The pre-tokenizer to use. + /// The normalizer to use. + /// The dictionary mapping special tokens to Ids. + /// The unknown token to be used by the model. + /// The prefix to attach to sub-word units that don’t represent a beginning of word. + /// The suffix to attach to sub-word units that represent an end of word. + /// Indicate whether allowing multiple unknown tokens get fused. + /// Indicate whether to handle the input text in byte level. + /// The beginning of sentence token. + /// The end of sentence token. + /// + /// When creating the tokenizer, ensure that the vocabulary stream is sourced from a trusted provider. + /// + public static async Task CreateAsync( + Stream vocabStream, + Stream? mergesStream, + PreTokenizer? preTokenizer = null, + Normalizer? normalizer = null, + IReadOnlyDictionary? specialTokens = null, + string? unknownToken = null, + string? continuingSubwordPrefix = null, + string? endOfWordSuffix = null, + bool fuseUnknownTokens = false, + bool byteLevel = false, + string? beginningOfSentenceToken = null, + string? endOfSentenceToken = null) + { + if (vocabStream is null) + { + throw new ArgumentNullException(nameof(vocabStream)); + } + + (Dictionary? vocab, Vec<(string, string)> merges) result = await ReadModelDataAsync(vocabStream, mergesStream, useAsync: true).ConfigureAwait(false); + + return new BpeTokenizer(result.vocab, result.merges, preTokenizer, normalizer, specialTokens, unknownToken, continuingSubwordPrefix, endOfWordSuffix, fuseUnknownTokens, byteLevel, beginningOfSentenceToken, endOfSentenceToken); + } + /// /// Construct a new Bpe model object to use for text encoding. ///