SharpToken 1.0.23

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpToken --version 1.0.23                
NuGet\Install-Package SharpToken -Version 1.0.23                
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="SharpToken" Version="1.0.23" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpToken --version 1.0.23                
#r "nuget: SharpToken, 1.0.23"                
#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 SharpToken as a Cake Addin
#addin nuget:?package=SharpToken&version=1.0.23

// Install SharpToken as a Cake Tool
#tool nuget:?package=SharpToken&version=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.

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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (17)

Showing the top 5 NuGet packages that depend on SharpToken:

Package Downloads
EachShow.AI

OpenAI, ChatGPT

Encamina.Enmarcha.SemanticKernel.Abstractions

Package Description

AICentral

Package Description

MyIA.SemanticKernel.Connectors.AI.MultiConnector

Extend your Semantic Kernel-powered apps with a fleet of specialized connectors, managed by a superior LLM as your fleet captain.

Musoq.DataSources.OpenAI

Package Description

GitHub repositories (6)

Showing the top 5 popular GitHub repositories that depend on SharpToken:

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
PowerShell/AIShell
An interactive shell to work with AI-powered assistance providers
AIDotNet/Thor
Thor(雷神托尔) 是一款强大的人工智能模型管理工具,其主要目的是为了实现多种AI模型的统一管理和使用。通过Thor(雷神托尔),用户可以轻松地管理和使用众多AI模型,而且Thor(雷神托尔)兼容OpenAI的接口格式,使得使用更加方便。
aiqinxuancai/TiktokenSharp
Token calculation for OpenAI models, using `o200k_base` `cl100k_base` `p50k_base` encoding.
Version Downloads Last updated
2.0.3 235,650 5/17/2024
2.0.2 54,340 4/8/2024
2.0.1 34,048 3/26/2024
1.2.33 2,727 3/25/2024
1.2.17 108,942 2/19/2024
1.2.16 9,306 2/15/2024
1.2.15 29,418 2/5/2024
1.2.14 207,903 12/10/2023
1.2.13 190 12/10/2023
1.2.12 281,914 9/12/2023
1.2.10 6,926 9/7/2023
1.2.8 31,532 8/28/2023
1.2.7 4,234 8/23/2023
1.2.6 37,031 8/2/2023
1.2.5 2,056 8/1/2023
1.2.2 26,538 7/1/2023
1.2.1 203 7/1/2023
1.1.3 219 7/1/2023
1.0.30 3,987 6/26/2023
1.0.29 814 6/25/2023
1.0.28 74,270 4/25/2023
1.0.27 2,286 4/20/2023
1.0.26 276 4/18/2023
1.0.25 16,492 3/28/2023
1.0.24 275 3/28/2023
1.0.23 273 3/28/2023
1.0.22 279 3/28/2023
1.0.21 275 3/28/2023
1.0.20 272 3/28/2023
1.0.19 308 3/28/2023
1.0.18 309 3/28/2023
1.0.17 299 3/28/2023
1.0.16 285 3/28/2023
1.0.12 287 3/28/2023
1.0.11 285 3/28/2023
1.0.2 5,669 7/1/2023