Condividi tramite


Usare Microsoft.ML.Tokenizers per la tokenizzazione del testo

La libreria Microsoft.ML.Tokenizers offre un set completo di strumenti per la tokenizzazione del testo nelle applicazioni .NET. La tokenizzazione è essenziale quando si lavora con modelli di linguaggio di grandi dimensioni, in quanto consente di gestire i conteggi dei token, stimare i costi e pre-elaborare il testo per i modelli di intelligenza artificiale.

Questo articolo illustra come usare le funzionalità principali della libreria e usare modelli di tokenizer diversi.

Prerequisiti

Annotazioni

La libreria Microsoft.ML.Tokenizers supporta anche .NET Standard 2.0, rendendola compatibile con .NET Framework 4.6.1 e versioni successive.

Installare il pacchetto

Installare il pacchetto NuGet di Microsoft.ML.Tokenizers.

dotnet add package Microsoft.ML.Tokenizers

Per i modelli Tiktoken (ad esempio GPT-4), è anche necessario installare il pacchetto di dati corrispondente:

dotnet add package Microsoft.ML.Tokenizers.Data.O200kBase

Funzionalità principali

La libreria Microsoft.ML.Tokenizers fornisce:

  • Architettura del tokenizzatore estendibile: consente la specializzazione dei componenti Normalizer, PreTokenizer, Model/Encoder e Decoder.
  • Più algoritmi di tokenizzazione: supporta BPE (codifica byte-pair), Tiktoken, Llama, CodeGen e altro ancora.
  • Conteggio e stima dei token: consente di gestire i costi e i limiti di contesto quando si lavora con i servizi di intelligenza artificiale.
  • Opzioni di codifica flessibili: fornisce metodi per codificare il testo in ID token, contare i token e decodificare i token in testo.

Usare il tokenizer Tiktoken

Il tokenizer Tiktoken viene comunemente usato con modelli OpenAI come GPT-4. L'esempio seguente illustra come inizializzare un tokenizzatore Tiktoken ed eseguire operazioni comuni:

// Initialize the tokenizer for the gpt-4o model.
Tokenizer tokenizer = TiktokenTokenizer.CreateForModel("gpt-4o");

string source = "Text tokenization is the process of splitting a string into a list of tokens.";

// Count the tokens in the text.
Console.WriteLine($"Tokens: {tokenizer.CountTokens(source)}");
// Output: Tokens: 16

// Encode text to token IDs.
IReadOnlyList<int> ids = tokenizer.EncodeToIds(source);
Console.WriteLine($"Token IDs: {string.Join(", ", ids)}");
// Output: Token IDs: 1279, 6602, 2860, 382, 290, 2273, 328, 87130, 261, 1621, 1511, 261, 1562, 328, 20290, 13

// Decode token IDs back to text.
string? decoded = tokenizer.Decode(ids);
Console.WriteLine($"Decoded: {decoded}");
// Output: Decoded: Text tokenization is the process of splitting a string into a list of tokens.

Per prestazioni migliori, è consigliabile memorizzare nella cache e riutilizzare l'istanza del tokenizer in tutta l'app.

Quando si usano i moduli APM, spesso è necessario gestire il testo entro i limiti dei token. L'esempio seguente illustra come tagliare il testo a un numero di token specifico:

Tokenizer tokenizer = TiktokenTokenizer.CreateForModel("gpt-4o");

string source = "Text tokenization is the process of splitting a string into a list of tokens.";

// Get the last 5 tokens from the text.
var trimIndex = tokenizer.GetIndexByTokenCountFromEnd(source, 5, out string? processedText, out _);
processedText ??= source;
Console.WriteLine($"Last 5 tokens: {processedText.Substring(trimIndex)}");
// Output: Last 5 tokens:  a list of tokens.

// Get the first 5 tokens from the text.
trimIndex = tokenizer.GetIndexByTokenCount(source, 5, out processedText, out _);
processedText ??= source;
Console.WriteLine($"First 5 tokens: {processedText.Substring(0, trimIndex)}");
// Output: First 5 tokens: Text tokenization is the

Usare il tokenizer Llama

Il tokenizer Llama è progettato per la famiglia Llama di modelli. Richiede un file di modello del tokenizer, che è possibile scaricare dai repository di modelli come Hugging Face:

// Open a stream to the remote Llama tokenizer model data file.
using HttpClient httpClient = new();
const string modelUrl = @"https://huggingface.co/hf-internal-testing/llama-tokenizer/resolve/main/tokenizer.model";
using Stream remoteStream = await httpClient.GetStreamAsync(modelUrl);

// Create the Llama tokenizer using the remote stream.
Tokenizer llamaTokenizer = LlamaTokenizer.Create(remoteStream);

string input = "Hello, world!";

// Encode text to token IDs.
IReadOnlyList<int> ids = llamaTokenizer.EncodeToIds(input);
Console.WriteLine($"Token IDs: {string.Join(", ", ids)}");
// Output: Token IDs: 1, 15043, 29892, 3186, 29991

// Count the tokens.
Console.WriteLine($"Tokens: {llamaTokenizer.CountTokens(input)}");
// Output: Tokens: 5

// Decode token IDs back to text.
string? decoded = llamaTokenizer.Decode(ids);
Console.WriteLine($"Decoded: {decoded}");
// Output: Decoded: Hello, world!

Tutti i tokenizer supportano opzioni di codifica avanzate, ad esempio il controllo della normalizzazione e la pretokenizzazione:

ReadOnlySpan<char> textSpan = "Hello World".AsSpan();

// Bypass normalization during encoding.
ids = llamaTokenizer.EncodeToIds(textSpan, considerNormalization: false);

// Bypass pretokenization during encoding.
ids = llamaTokenizer.EncodeToIds(textSpan, considerPreTokenization: false);

// Bypass both normalization and pretokenization.
ids = llamaTokenizer.EncodeToIds(textSpan, considerNormalization: false, considerPreTokenization: false);

Usare il tokenizzatore BPE

La codifica della coppia di byte (BPE) è l'algoritmo sottostante usato da molti tokenizer, tra cui Tiktoken. BPE è stato inizialmente sviluppato come algoritmo per la compressione dei testi e successivamente usato da OpenAI per la tokenizzazione quando ha preaddestrato il modello GPT. L'esempio seguente illustra la tokenizzazione BPE:

// BPE (Byte Pair Encoding) tokenizer can be created from vocabulary and merges files.
// Download the GPT-2 tokenizer files from Hugging Face.
using HttpClient httpClient = new();
const string vocabUrl = @"https://huggingface.co/openai-community/gpt2/raw/main/vocab.json";
const string mergesUrl = @"https://huggingface.co/openai-community/gpt2/raw/main/merges.txt";

using Stream vocabStream = await httpClient.GetStreamAsync(vocabUrl);
using Stream mergesStream = await httpClient.GetStreamAsync(mergesUrl);

// Create the BPE tokenizer using the vocabulary and merges streams.
Tokenizer bpeTokenizer = BpeTokenizer.Create(vocabStream, mergesStream);

string text = "Hello, how are you doing today?";

// Encode text to token IDs.
IReadOnlyList<int> ids = bpeTokenizer.EncodeToIds(text);
Console.WriteLine($"Token IDs: {string.Join(", ", ids)}");

// Count tokens.
int tokenCount = bpeTokenizer.CountTokens(text);
Console.WriteLine($"Token count: {tokenCount}");

// Get detailed token information.
IReadOnlyList<EncodedToken> tokens = bpeTokenizer.EncodeToTokens(text, out string? normalizedString);
Console.WriteLine("Tokens:");
foreach (EncodedToken token in tokens)
{
    Console.WriteLine($"  ID: {token.Id}, Value: '{token.Value}'");
}

// Decode tokens back to text.
string? decoded = bpeTokenizer.Decode(ids);
Console.WriteLine($"Decoded: {decoded}");

// Note: BpeTokenizer might not always decode IDs to the exact original text
// as it can remove spaces during tokenization depending on the model configuration.

La libreria fornisce anche tokenizer specializzati come BpeTokenizer e EnglishRobertaTokenizer che è possibile configurare con vocabolari personalizzati per modelli specifici.

Per altre informazioni su BPE, vedere Tokenizzazione della codifica byte-pair.

Operazioni comuni del tokenizzatore

Tutti i tokenizer nella libreria implementano la Tokenizer classe base. Nella tabella seguente vengono illustrati i metodi disponibili.

Metodo Description
EncodeToIds Converte il testo in un elenco di ID token.
Decode Converte nuovamente gli ID token in testo.
CountTokens Restituisce il numero di token in una stringa di testo.
EncodeToTokens Restituisce informazioni dettagliate sul token, inclusi valori e ID.
GetIndexByTokenCount Trova l'indice dei caratteri per un numero di token specifico dall'inizio.
GetIndexByTokenCountFromEnd Trova l'indice dei caratteri per un numero di token specifico dalla fine.

Eseguire la migrazione da altre librerie

Se attualmente si usa DeepDev.TokenizerLib o SharpToken, valutare la possibilità di eseguire la migrazione a Microsoft.ML.Tokenizers. La libreria è stata migliorata per coprire gli scenari di tali librerie e offre prestazioni e supporto migliori. Per indicazioni sulla migrazione, vedere la guida alla migrazione.