npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openai-gpt-token-counter

v1.1.1

Published

Count the number of OpenAI tokens in a string. Supports all OpenAI Text models (text-davinci-003, gpt-3.5-turbo, gpt-4)

Downloads

25,996

Readme

OpenAI GPT Token Counter

npm DT

This npm package is designed to count the number of OpenAI tokens in a given text or messages array. It supports various OpenAI text and chat models, and it has been verified for 100% accuracy.

Installation

You can install the package using npm:

npm install openai-gpt-token-counter

Usage

Importing the Module

For CommonJS:

const openaiTokenCounter = require('openai-gpt-token-counter');

For ES6 Imports:

import openaiTokenCounter from 'openai-gpt-token-counter';

Counting Tokens in Text

To count the number of tokens in a text for a specific OpenAI text model (e.g. text-davinci-003), use the text method:

const text = "This is a test sentence.";
const model = "text-davinci-003"; // Replace with your desired OpenAI model

const tokenCount = openaiTokenCounter.text(text, model);
console.log(`Token count: ${tokenCount}`);

Counting Tokens in Chat Messages

To count the number of tokens in chat messages for a specific OpenAI chat model, use the chat method:

const messages = [
  { role: "user", content: "Say this is a test!" },
  // Add more messages if needed
];

const model = "gpt-4"; // Replace with your desired OpenAI chat model

const tokenCount = openaiTokenCounter.chat(messages, model);
console.log(`Token count: ${tokenCount}`);

Example Messages Array for Chat Models

For chat models, provide an array of messages, where each message is an object with the following structure:

const messages = [
  { role: "system", content: "System prompt to guide the AI" },
  { role: "user", content: "Message content from the user" },
  { role: "assistant", content: "AI response to the user's message" },
  // Add more messages as needed
];

The role property can be one of "user", "system", or "assistant". The content property holds the actual text of the message.

Supported Models

This package supports all OpenAI chat/text models, but the official ones we tested on are:

Text Models

  • GPT3 (text-davinci-003, text-curie-001, text-babbage-001, text-ada-001)

Chat Models

  • GPT3.5 Turbo: "gpt-3.5-turbo"
  • GPT3.5 16K: "gpt-3.5-turbo-16k"
  • GPT4: "gpt-4"
  • GPT4 32K: "gpt-4-32k"

FineTuned Models

Use the base model system to calculate the token count for fine-tuned models. For example, if you have a fine-tuned model based on gpt-4, you can use the gpt-4 model to calculate the token count. Please report on the Github repository if you find any issues with fine-tuned models.

Accuracy

This module has been tested and verified for 100% accuracy against the OpenAI API's token count. Don't take my word for it, run this example test code to see the accuracy:

import openaiTokenCounter from 'openai-gpt-token-counter';
import { Configuration, OpenAIApi } from "openai";

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

(async () => {
  const model = "gpt-3.5-turbo";
  const texts = [
    "Hello world",
    "This is a slightly longer sentence with more words.",
    "And this is an even longer sentence that has an excessive number of words..."
  ];

  for (let text of texts) {
    console.log(`Testing text: "${text}"`);
    const messages = [{ role: "user", content: text }];

    const tokenCount = openaiTokenCounter.chat(messages, model);
    console.log(`openai-gpt-token-counter Token count: ${tokenCount}`);

    const chatCompletion = await openai.createChatCompletion({
      model: model,
      messages: messages,
    });
    console.log(`OpenAI API Token count: ${chatCompletion.data.usage.prompt_tokens}`);
    console.log("\n");
  }
})();

Note on Embeddings

Please note that this package does not support embeddings. It is specifically designed for counting the number of tokens in text or chat messages for OpenAI models. Though this is on our roadmap, we do not have an ETA for when this feature will be added.

Issues and Contributions

If you encounter any issues or have suggestions for improvements, please feel free to open an issue on the GitHub repository. Contributions through pull requests are also welcome!