Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

llm-tokenizer

KevinKWZheng26Apache-2.01.0.2TypeScript support: included

A lightweight tokenizer for OpenAI's GPT model series. Uses OpenAI's tiktoken python package

OpenAI, GPT-3, GPT-4, GPT-3.5, ChatGPT, LLMs, GPT, tokenizer, BPE, encoder, decoder, Machine Learning

readme

LLM-Tokenizer

A simple and minimalized TS port for using OpenAI's Python pack tiktoken. It is essentially a script runner for OpenAI's tiktoken package with TS. Hence, running this package requires you to have a Python runtime environment and tiktoken package installed.

You will need a Python runtime envrionment and tiktoken package installed in order to make this package work.

To install this package, run npm i llm-tokenizer.

Usage

import Tokenizer from "llm-tokenizer";

const tokenizer=new Tokenizer();

const result1=await tokenizer.countToken(`tiktoken is great!`);
// Output: 6

const result2=await tokenizer.encode(`tiktoken is great!`);
// Output: [83, 1609, 5963, 374, 2294, 0]

const result3=await tokenizer.decode([83, 1609, 5963, 374, 2294, 0]);
// Output: `tiktoken is great!`

Additionally, you could specify which encoding to defaultly use or for temporary usage by setting the encoding option in the constructor or setEncoding method.