![]() |
VOOZH | about |
dotnet add package SharpToken --version 1.2.1
NuGet\Install-Package SharpToken -Version 1.2.1
<PackageReference Include="SharpToken" Version="1.2.1" />
<PackageVersion Include="SharpToken" Version="1.2.1" />Directory.Packages.props
<PackageReference Include="SharpToken" />Project file
paket add SharpToken --version 1.2.1
#r "nuget: SharpToken, 1.2.1"
#:package SharpToken@1.2.1
#addin nuget:?package=SharpToken&version=1.2.1Install as a Cake Addin
#tool nuget:?package=SharpToken&version=1.2.1Install as a Cake Tool
👁 NuGet
👁 dotnet
👁 Last Commit
👁 GitHub Issues
👁 Used by
👁 Contributors
SharpToken is a C# library that serves as a port of the Python tiktoken library. It provides functionality for encoding and decoding tokens using GPT-based encodings. This library is built for .NET 6 and .NET Standard 2.0, making it compatible with a wide range of frameworks.
To install SharpToken, use the NuGet package manager:
Install-Package SharpToken
Or, if you prefer using the .NET CLI:
dotnet add package SharpToken
For more information, visit the NuGet package page.
To use SharpToken in your project, first import the library:
using SharpToken;
Next, create an instance of GptEncoding by specifying the desired encoding or model:
// Get encoding by encoding name
var encoding = GptEncoding.GetEncoding("cl100k_base");
// Get encoding by model name
var encoding = GptEncoding.GetEncodingForModel("gpt-4");
You can then use the Encode method to encode a string:
var encoded = encoding.Encode("Hello, world!"); // Output: [9906, 11, 1917, 0]
And use the Decode method to decode the encoded tokens:
var decoded = encoding.Decode(encoded); // Output: "Hello, world!"
SharpToken currently supports the following models:
r50k_basep50k_basep50k_editcl100k_baseYou can use any of these models when creating an instance of GptEncoding:
var r50kBaseEncoding = GptEncoding.GetEncoding("r50k_base");
var p50kBaseEncoding = GptEncoding.GetEncoding("p50k_base");
var p50kEditEncoding = GptEncoding.GetEncoding("p50k_edit");
var cl100kBaseEncoding = GptEncoding.GetEncoding("cl100k_base");
When you encode a string using the Encode method, the returned value is a list of integers that represent tokens in the specified encoding. These tokens are a compact way of representing the input text and can be processed more efficiently by various algorithms.
For example, encoding the text "Hello world!" using the cl100k_base encoding might produce the following list of integers:
var encoded = cl100kBaseEncoding.Encode("Hello world!"); // Output: [9906, 1917, 0]
You can then use the Decode method to convert these tokenized integer values back into the original text:
var decoded = cl100kBaseEncoding.Decode(encoded); // Output: "Hello world!"
With SharpToken, you can seamlessly switch between different encodings to find the one that best suits your needs. Just
remember to use the same encoding for both the Encode and Decode methods to ensure accurate results.
SharpToken allows you to specify custom sets of allowed special tokens when encoding text. To do this, pass a HashSet<string> containing the allowed special tokens as a parameter to the Encode method:
const string encodingName = "cl100k_base";
const string inputText = "Some Text <|endofprompt|>";
var allowedSpecialTokens = new HashSet<string> { "<|endofprompt|>" };
var encoding = GptEncoding.GetEncoding(encodingName);
var encoded = encoding.Encode(inputText, allowedSpecialTokens);
var expectedEncoded = new List<int> { 8538, 2991, 220, 100276 };
Assert.Equal(expectedEncoded, encoded);
Similarly, you can specify custom sets of disallowed special tokens when encoding text. Pass a HashSet<string>
containing the disallowed special tokens as a parameter to the Encode method:
const string encodingName = "cl100k_base";
const string inputText = "Some Text";
var encoding = GptEncoding.GetEncoding(encodingName);
void TestAction()
{
encoding.Encode(inputText, disallowedSpecial: new HashSet<string> { "Some" });
}
Assert.Throws<ArgumentException>(TestAction);
In this example, an ArgumentException is thrown because the input text contains a disallowed special token
SharpToken includes a set of test cases in the file to ensure its compatibility with the Python tiktoken library. These test cases validate the functionality and behavior of SharpToken, providing a reliable reference for developers. Running the unit tests and verifying the test cases helps maintain consistency between the C# SharpToken library and the original Python implementation.
If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the project's repository.
Hope you find SharpToken useful for your projects and welcome any feedback you may have.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on SharpToken:
| Package | Downloads |
|---|---|
|
EachShow.AI
OpenAI, ChatGPT, Ollama, Vllm |
|
|
AICentral
Package Description |
|
|
Encamina.Enmarcha.SemanticKernel.Abstractions
Package Description |
|
|
Parhelia.10.4
Parhelia is an advanced AI-powered editor component for Sitecore, providing intelligent content editing capabilities and seamless integration with Sitecore content management. |
|
|
OBotService
OBase Framework |
Showing the top 7 popular GitHub repositories that depend on SharpToken:
| Repository | Stars |
|---|---|
|
PowerShell/AIShell
An interactive shell to work with AI-powered assistance providers
|
|
|
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
|
|
|
AIDotNet/Thor
Thor is a powerful artificial intelligence model management tool, whose main purpose is to achieve unified management and use of multiple AI models. Through Thor, users can easily manage and utilize numerous AI models
|
|
|
tree-sitter/csharp-tree-sitter
C# bindings to the Tree-sitter parsing library
|
|
|
aiqinxuancai/TiktokenSharp
Token calculation for OpenAI models, using `o200k_base` `cl100k_base` `p50k_base` encoding.
|
|
|
telerik/kendo-ui-demos-service
Back-end service used for the Kendo UI Online Demos
|
|
|
Azure/Vector-Search-AI-Assistant-MongoDBvCore
Microsoft Official Build & Modernize AI Apps reference solution and content for implementing RAG Pattern solutions using vector search with Azure Cosmos DB MongoDB vCore with an AI assistant user experience.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.6 | 147,833 | 3/25/2026 |
| 2.0.5 | 1,315 | 3/25/2026 |
| 2.0.4 | 468,974 | 8/24/2025 |
| 2.0.3 | 1,302,915 | 5/17/2024 |
| 2.0.2 | 89,170 | 4/8/2024 |
| 2.0.1 | 50,954 | 3/26/2024 |
| 1.2.33 | 10,613 | 3/25/2024 |
| 1.2.17 | 134,380 | 2/19/2024 |
| 1.2.16 | 10,544 | 2/15/2024 |
| 1.2.15 | 40,654 | 2/5/2024 |
| 1.2.14 | 369,193 | 12/10/2023 |
| 1.2.13 | 448 | 12/10/2023 |
| 1.2.12 | 418,278 | 9/12/2023 |
| 1.2.10 | 7,352 | 9/7/2023 |
| 1.2.8 | 45,266 | 8/28/2023 |
| 1.2.7 | 4,586 | 8/23/2023 |
| 1.2.6 | 127,682 | 8/2/2023 |
| 1.2.5 | 2,307 | 8/1/2023 |
| 1.2.2 | 29,522 | 7/1/2023 |
| 1.2.1 | 6,330 | 7/1/2023 |