Tiktoken 0.9.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tiktoken --version 0.9.4
NuGet\Install-Package Tiktoken -Version 0.9.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tiktoken" Version="0.9.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tiktoken --version 0.9.4
#r "nuget: Tiktoken, 0.9.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tiktoken as a Cake Addin
#addin nuget:?package=Tiktoken&version=0.9.4

// Install Tiktoken as a Cake Tool
#tool nuget:?package=Tiktoken&version=0.9.4

Tiktoken

Nuget package dotnet License: MIT Discord

Due to the lack of a C# version of cl100k_base encoding (gpt-3.5-turbo), I have implemented a basic solution with encoding and decoding methods based on the official Rust implementation.

Currently, cl100k_base p50k_base has been implemented. Other encodings will be added in future submissions. If you encounter any issues or have questions, please feel free to submit them on the lssues."

Usage

using Tiktoken;

var encoding = Encoding.ForModel("gpt-3.5-turbo");
var tokens = encoding.Encode("hello world"); // [15339, 1917]
var text = encoding.Decode(tokens); // hello world

var encoding = Encoding.Get("cl100k_base");
var tokens = encoding.Encode("hello world"); // [15339, 1917]
var text = encoding.Decode(tokens); // hello world

When using a new encoder for the first time, the required tiktoken files for the encoder will be downloaded from the internet. This may take some time. Once the download is successful, subsequent uses will not require downloading again. You can set TikToken.PBEFileDirectory before using the encoder to modify the storage path of the downloaded tiktoken files, or you can pre-download the files to avoid network issues causing download failures.

Why are the tiktoken files not integrated into the package? On one hand, this would make the package size larger. On the other hand, I want to stay as consistent as possible with OpenAI's official Python code.

Below are the file download links: p50k_base.tiktoken cl100k_base.tiktoken

Benchmarks

You can view the reports for each version here


BenchmarkDotNet=v0.13.5, OS=macOS Ventura 13.3.1 (a) (22E772610a) [Darwin 22.4.0]
Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
.NET SDK=7.0.203
  [Host]     : .NET 7.0.5 (7.0.523.17405), Arm64 RyuJIT AdvSIMD
  Job-ZISPEC : .NET 7.0.5 (7.0.523.17405), Arm64 RyuJIT AdvSIMD DEBUG

BuildConfiguration=Debug  

Method Data Mean Error StdDev Median Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
SharpTokenV1_0_28 Hello, World! 3.293 μs 0.0422 μs 0.0394 μs 3.325 μs 1.00 0.00 0.6752 0.0038 4.14 KB 1.00
TiktokenSharpV1_0_5 Hello, World! 6.652 μs 0.0300 μs 0.0281 μs 6.643 μs 2.02 0.03 2.1820 0.0458 13.41 KB 3.24
Tiktoken Hello, World! 6.360 μs 0.0137 μs 0.0129 μs 6.364 μs 1.93 0.02 2.1820 0.0458 13.41 KB 3.24
SharpTokenV1_0_28 King(...)edy. [275] 62.972 μs 0.3329 μs 0.2951 μs 62.912 μs 1.00 0.00 8.5449 0.4883 52.89 KB 1.00
TiktokenSharpV1_0_5 King(...)edy. [275] 21.692 μs 0.0669 μs 0.0593 μs 21.698 μs 0.34 0.00 5.0964 0.3052 31.34 KB 0.59
Tiktoken King(...)edy. [275] 20.063 μs 0.1374 μs 0.1148 μs 20.033 μs 0.32 0.00 5.7678 0.3357 35.38 KB 0.67
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on Tiktoken:

Package Downloads
tryAGI.OpenAI The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Generated C# SDK based on official OpenAI OpenAPI specification. Includes C# Source Generator which allows you to define functions natively through a C# interface, and also provides extensions that make it easier to call this interface later

HuggingFace

Generated C# SDK based on HuggingFace OpenAPI specification.

Anthropic

Generated C# SDK based on official Anthropic OpenAPI specification.

Aeolin.AwosFramework.OpenAIFunctionCalling

Package Description

Midjourney

Generated C# SDK based on Midjourney OpenAPI specification.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 53 5/15/2024
1.2.0 21,908 2/18/2024
1.1.3 87,311 10/11/2023
1.1.2 59,866 8/28/2023
1.1.1 55,185 7/12/2023
1.1.0 1,551 6/28/2023
1.0.2 238 6/21/2023
1.0.1 285 6/16/2023
1.0.0 523 6/12/2023
0.9.7 218 6/11/2023
0.9.6 287 6/9/2023
0.9.5 208 6/9/2023
0.9.4 142 6/9/2023
0.9.3 131 6/9/2023
0.9.2 162 6/9/2023