From d18e622f2219b3a7fa7d51cb164ae0f78d00da5a Mon Sep 17 00:00:00 2001 From: Coder Gautam Date: Sun, 23 Jul 2023 17:26:07 -0500 Subject: [PATCH] readme impr --- README.md | 81 +++++++++++++++++++++++++++++++++++++++------------- package.json | 2 +- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 758ed1d..0effcd1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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."; @@ -47,17 +55,28 @@ 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"` @@ -65,26 +84,48 @@ For text models, no need to pass the model name as a string. Just use the text f - 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! \ No newline at end of file diff --git a/package.json b/package.json index 9287315..82cffdb 100644 --- a/package.json +++ b/package.json @@ -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": {