OpenAI Token Counter
Token counter for OpenAI messages with support for function token calculation. This project was ported to python based on the following repository: https://github.com/hmarr/openai-chat-tokens
As stated in hmarr project:
Estimating token usage for chat completions isn't quite as easy as it sounds. For regular chat messages, you need to consider how the messages are formatted by OpenAI when they're provided to the model, as they don't simply dump the JSON messages they receive via the API into the model. For function calling, things are even more complex, as the OpenAPI-style function definitions get rewritten into TypeScript type definitions. This library handles both of those cases, as well as a minor adjustment needed for handling the results of function calling. tiktoken is used to do the tokenization.
This library is tested nightly againts the openai API to detect for potential breaks if any internal change is made by openai, because as stated above we implement token calculation based on internal OpenAI techniques that are not exposed and can potentially change without notice.
Installation
You can install OpenAI Token Counter via [pip] from [PyPI]:
$ pip install openai-token-counter
Usage
from openai_token_counter import openai_token_counter
messages = [{"role": "user", "content": "hello"}]
functions = [
{
"name": "bing_bong",
"description": "Do a bing bong",
"parameters": {
"type": "object",
"properties": {
"foo": {"type": "string"},
"bar": {"type": "number", "description": "A number"},
}
}
}
]
result = openai_token_counter(
messages=messages,
model="gpt-3.5-turbo", # Optional, deafults to cl100k_base encoding which is used by GPT models
functions=functions, # Optional
function_call="auto" # Optional
)
print(result) # Output: '57'
Contributing
Contributions are very welcome.
- Install poetry
- Install the project dependencies
poetry install
- Make the changes
- Test locally using nox (no need to test all python versions, select only 3.10):
nox --python=3.10
- Create a PR in GitHub.
License
Distributed under the terms of the MIT, OpenAI Token Counter is free and open source software.
Issues
If you encounter any problems, please [file an issue] along with a detailed description.
Metadata
Release files for openai-token-counter 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openai_token_counter-1.0.2.tar.gz | 6.4 kB | Without provenance | Details for openai_token_counter-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openai_token_counter-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for openai_token_counter-1.0.2-py3-none-any.whl |
Total release size: 14.0 kB
Release files / openai_token_counter-1.0.2.tar.gz
| Download URL | openai_token_counter-1.0.2.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2e1beb80c6084d6939f886b7608bcfcad4b1ad1df3874a885461f4f0561d9c7
|
|
BLAKE2b-256 checksum How to use checksums |
9d1e280d6c396a267a2f744edb9ec71f31c99a203a7d3e3009abe1376f828b74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / openai_token_counter-1.0.2-py3-none-any.whl
| Download URL | openai_token_counter-1.0.2-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e77f32323a51428db9e45901a2070baf602157aaab9d616c590b7f34eb2c19c9
|
|
BLAKE2b-256 checksum How to use checksums |
7762931a4be6842b825fe469c44f039055cc5b5022d3eb57b088781c852f2d9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for openai-token-counter 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.