SharpToken 1.0.23

SharpToken

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.1, making it compatible with a wide range of frameworks.

Installation

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.

Usage

To use SharpToken in your project, first import the library:

using SharpToken;

Next, create an instance of GptEncoding by specifying the desired encoding:

var encoding = GptEncoding.GetEncoding("cl100k_base");

You can then use the Encode method to encode a string:

var encoded = encoding.Encode("Hello, world!");

And use the Decode method to decode the encoded tokens:

var decoded = encoding.Decode(encoded);

Supported Models

SharpToken currently supports the following models:

  • r50k_base
  • p50k_base
  • p50k_edit
  • cl100k_base

You 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");

Understanding Encoded Values

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.

Contributions and Feedback

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.

No packages depend on SharpToken.

.NET 6.0

  • No dependencies.

.NET Standard 2.1

  • No dependencies.

Version Downloads Last updated
2.0.3 7 10/07/2024
2.0.2 1 06/10/2025
2.0.1 1 06/10/2025
1.2.33 1 06/10/2025
1.2.17 1 06/10/2025
1.2.16 1 06/10/2025
1.2.15 1 06/10/2025
1.2.14 1 06/10/2025
1.2.13 1 06/10/2025
1.2.12 1 06/10/2025
1.2.10 1 06/10/2025
1.2.8 1 06/10/2025
1.2.7 1 06/10/2025
1.2.6 1 06/10/2025
1.2.5 1 06/10/2025
1.2.2 1 06/10/2025
1.2.1 1 06/10/2025
1.1.3 1 06/10/2025
1.0.30 1 06/10/2025
1.0.29 1 06/10/2025
1.0.28 1 06/10/2025
1.0.27 1 06/10/2025
1.0.26 1 06/10/2025
1.0.25 1 06/10/2025
1.0.24 1 06/10/2025
1.0.23 1 06/10/2025
1.0.22 1 06/10/2025
1.0.21 1 06/10/2025
1.0.20 2 06/10/2025
1.0.19 1 06/10/2025
1.0.18 1 06/10/2025
1.0.17 1 06/10/2025
1.0.16 1 06/10/2025
1.0.12 1 06/10/2025
1.0.11 1 06/10/2025
1.0.2 1 06/10/2025