Install
dotnet add package Tokenizers.DotNet --version 1.1.2
| Product | Versions |
|---|---|
| .NET |
README
Tokenizers.DotNet
.NET wrapper of HuggingFace Tokenizers library
Nuget Package list
| Package | main | Description |
|---|---|---|
| Tokenizers.DotNet | Core library | |
| Tokenizers.DotNet.runtime.win-x64 | Native bindings for windows x64 | |
| Tokenizers.DotNet.runtime.win-arm64 | Native bindings for windows arm64 |
Requirements
- .NET 6 or above
- (Build) Latest Rust
Supported functionalities
- Download tokenizer files from Hugginface Hub
- Load tokenizer file(
.json) from local - Encode string to tokens
- Decode tokens to string
How to use
(1) Install the packages
- From the NuGet, install
Tokenizers.DotNetpackage - And then, install
Tokenizers.DotNet.runtime.win-<ARCH>package too (win-x64orwin-arm64)
(2) Write the code
Check following example code:
using Tokenizers.DotNet;
// Download skt/kogpt2-base-v2/tokenizer.json from the hub
var hubName = "skt/kogpt2-base-v2";
var filePath = "tokenizer.json";
var fileFullPath = await HuggingFace.GetFileFromHub(hubName, filePath, "deps");
Console.WriteLine($"Downloaded {fileFullPath}");
// Create a tokenizer instance
var tokenizer = new Tokenizer(vocabPath: fileFullPath);
var text = "์, ์ด์ ์์ฌ๋ ํด๋ณผ๊น์";
Console.WriteLine($"Input text: {text}");
var tokens = tokenizer.Encode(text);
Console.WriteLine($"Encoded: {string.Join(", ", tokens)}");
var decoded = tokenizer.Decode(tokens);
Console.WriteLine($"Decoded: {decoded}");
Console.WriteLine($"Version of Tokenizers.DotNet.runtime.win: {tokenizer.GetVersion()}");
Console.WriteLine("--------------------------------------------------");
// Use another tokenizer
//// Download openai-community/gpt2 from the hub
hubName = "openai-community/gpt2";
filePath = "tokenizer.json";
fileFullPath = await HuggingFace.GetFileFromHub(hubName, filePath, "deps");
// Create a tokenizer instance
var tokenizer2 = new Tokenizer(vocabPath: fileFullPath);
var text2 = "i was nervous before the exam, and i had a fever.";
Console.WriteLine($"Input text: {text2}");
var tokens2 = tokenizer2.Encode(text2);
Console.WriteLine($"Encoded: {string.Join(", ", tokens2)}");
var decoded2 = tokenizer2.Decode(tokens2);
Console.WriteLine($"Decoded: {decoded2}");
Console.WriteLine($"Version of Tokenizers.DotNet.runtime.win: {tokenizer2.GetVersion()}");
Console.ReadKey();
How to build
- Prepare following stuff:
- Rust build system (
cargo) - .NET build system (
dotnet 6.0, 7.0, 8.0, 9.0) - PowerShell (Recommend
7.4.2or above)
- Rust build system (
- Bump the version number in
NATIVE_LIB_VERSION.txt - Run
build_all_clean.ps1- To build
Tokenizers.DotNet.runtime.winonly, runbuild_rust.ps1 - To build
Tokenizers.DotNetonly, runbuild_dotnet.ps1
- To build
Each build artifacts will be in nuget directory.
Cross-platform build
You can use Docker to compile this library for Windows x64/arm64 and Linux x64/arm64
Run update_version.ps1 before running Docker to update the package version.
Windows:
PS > docker build -f Dockerfile -t ghcr.io/sappho192/tokenizers.dotnet:latest .
PS > docker run -v .\nuget:/out --rm ghcr.io/sappho192/tokenizers.dotnet:latest
Linux/MacOS:
$ docker build -f Dockerfile -t ghcr.io/sappho192/tokenizers.dotnet:latest .
$ docker run -v ./nuget:/out --rm ghcr.io/sappho192/tokenizers.dotnet:latest
Built packages will be in the nuget folder
Changes from Previous Version
No structural changes between v1.1.0 and v1.1.2.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Compatibility metadata lists 4 target frameworks; confirm the exact target used by your application.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.