TokenTrim
TokenTrim intelligently trims OpenAI messages to fit within a model's token limit (shortening a message by removing characters from the middle), making it easy to avoid exceeding the maximum token count.
It's best suited for use directly in OpenAI API calls:
import tokentrim as tt
model = "gpt-4"
response = openai.ChatCompletion.create(
model=model,
messages=tt.trim(messages, model) # Trims old messages to fit under model's max token count
)
TokenTrim's behavior is based on OpenAI's own best practices.
Installation
Use the package manager pip to install TokenTrim:
pip install tokentrim
Usage
The primary function in the TokenTrim library is trim(). This function receives a list of messages and a model name, and it returns a trimmed list of messages that should be within the model's token limit.
import tokentrim as tt
# Force a system_message to be prepended to your messages list. This will not be trimmed.
system_message = "You are a helpful assistant."
response = openai.ChatCompletion.create(
model=model,
messages=tt.trim(messages, model, system_message=system_message)
)
Parameters
messages: A list of message objects to be trimmed. Each message is a dictionary with 'role' and 'content'.model: The OpenAI model being used (e.g., 'gpt-4', 'gpt-4-32k'). This determines the token limit.system_message(optional): A system message to preserve at the start of the conversation.trim_ratio(optional): Target ratio of tokens to use after trimming. Default is 0.75, meaning it will trim messages so they use about 75% of the model's token limit.return_response_tokens(optional): If set to True, the function also returns the number of tokens left available for the response after trimming.
Return Value
By default, trim() returns the trimmed list of messages. If return_response_tokens is set to True, it returns a tuple where the first element is the trimmed list of messages, and the second element is the number of tokens left available for the response.
License
This project is licensed under the terms of the MIT license.
Metadata
Release files for tokentrim 0.1.13
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 | |
|---|---|---|---|---|
| tokentrim-0.1.13.tar.gz | 6.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tokentrim-0.1.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.4 kB
Release files / tokentrim-0.1.13.tar.gz
| Download URL | tokentrim-0.1.13.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
379e64bb0bd2d713d9e6a9142946d613249376a9234cfaad838b1b31c4d75d30
|
|
BLAKE2b-256 checksum How to use checksums |
1ddbf2c34f8ea2e216967ef3ea10ba276144fb3e0d8820de2517ff537d8e39da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.8 Linux/6.2.0-1018-gcp
|
Release files / tokentrim-0.1.13-py3-none-any.whl
| Download URL | tokentrim-0.1.13-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
83c1b8b7d9db391e521ef9a9e054fa1e9cff56398ace259e0b1342a4d3223953
|
|
BLAKE2b-256 checksum How to use checksums |
ecd9888803c9c1613809af7165657a54669af08395ba5df8f3ef40c2f045fb3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.8 Linux/6.2.0-1018-gcp
|
Security for tokentrim 0.1.13
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.