Skip to content

Commit

Permalink
readme impr
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Jul 23, 2023
1 parent bbb12db commit d18e622
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
81 changes: 61 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![npm](https://img.shields.io/npm/dt/openai-gpt-token-counter)

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, making it a versatile tool for your natural language processing tasks.
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:
Install the package using npm:

```bash
npm install openai-gpt-token-counter
Expand All @@ -16,13 +16,21 @@ npm install openai-gpt-token-counter

### Importing the Module

For CommonJS:

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

For ES6 Imports:

```javascript
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 (ex: text-davinci-003), use the `text` method:
To count the number of tokens in a text for a specific OpenAI text model (e.g. text-davinci-003), use the `text` method:

```javascript
const text = "This is a test sentence.";
Expand All @@ -47,44 +55,77 @@ const model = "gpt-3.5-turbo"; // 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:

```javascript
const messages = [
{ role: "user", content: "Message content from the user" },
{ role: "system", content: "System 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 a range of OpenAI models, including both text and chat models. Here are some examples of 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)

For text models, no need to pass the model name as a string. Just use the text function, they should all use the same tokens calculation.

### 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"`

Please ensure you provide the correct model name when using the package.

## Example Messages Array for Chat Models
## Accuracy

For chat models, you should provide an array of messages, where each message is an object with the following structure:
This module has been tested and verified for 100% accuracy against the OpenAI API's token count. Here is an example test code:

```javascript
const messages = [
{ role: "user", content: "Message content from the user" },
{ role: "system", content: "System response to the user's message" },
// Add more messages as needed
];
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");
}
})();
```

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

## Important Note on Embeddings
## 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.
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](https://github.com/codergautam/openai-gpt-token-counter). Contributions through pull requests are also welcome!
If you encounter any issues or have suggestions for improvements, please feel free to open an issue on the [GitHub repository](https://github.com/codergautam/openai-gpt-token-counter). Contributions through pull requests are also welcome!
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openai-gpt-token-counter",
"version": "1.0.8",
"version": "1.0.9",
"description": "Count the number of OpenAI GPT tokens in a string",
"main": "src/index.js",
"scripts": {
Expand Down

0 comments on commit d18e622

Please sign in to comment.