Giter Site home page Giter Site logo

chatgptnet.tokenizer's Introduction

ChatGPT Tokenizer for .NET

NuGet

A ChatGPT tokenizer implementation for .NET. You can use this library to estimate the cost (amount of tokens) of your request to ChatGPT.

Installation

The library is available on NuGet. Just search for ChatGptNet.Tokenizer in the Package Manager GUI or run the following command in the .NET CLI:

dotnet add package ChatGptNet.Tokenizer

About

This is a C# implementation of OpenAI's original python encoder/decoder which can be found here. This implementation was strongly inspired from the JavaScript version created by latitudegames, that can be found here.

Setup

The library can be used in any .NET application built with .NET 6.0 or later. Just create an instance of the GptTokenizer and you are ready to go.

You can either create the default tokenizer

var tokenizer = await GptTokenizer.CreateTokenizerAsync();

Or, optionally, you can create a tokenizer with a custom vocab.bpe and encodings.json source files with:

using var vocabStream = File.OpenRead(@"path/to/vocab.bpe");
using var encodingStream = File.OpenRead(@"path/to/encodings.json");

var tokenizer = await GptTokenizer.CreateTokenizerAsync(vocabStream, encodingsStream);

The default vocab.bpe and encodings.json are already included in this library.

Usage

Once you obtain an instance of the GptTokenizer, you can perform the tokenization of your text.

var tokens = tokenizer.GetTokens(@"Lorem ipsum dolor sit amet")

var tokensCount = tokens.Length;
var tokensAsText = tokenizer.GetTextFromTokens(tokens);

Integration with ChatGptNet

This package extends the ChatGptNet NuGet package (source: https://github.com/marcominerva/ChatGptNet) by providing some functionalities for estimating the token amount.

public async Task Estimate(IChatGptClient chatGptClient)
{
    var conversationId = Guid.NewGuid();

    chatGptClient.EstimateConversationTokenCountAsync("first message");
    chatGptClient.EstimateConversationTokenCountAsync(conversationId);
    chatGptClient.EstimateConversationTokenCountAsync(conversationId, "next message");
}

Contribute

Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.

chatgptnet.tokenizer's People

Contributors

nicolaparo avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

edwardy2418

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.