TokenTrim

raw JSON →
0.1.13 verified Mon Apr 27 auth: no python

A utility library for trimming 'messages' arrays to fit within a specified token limit, commonly used with GPT models. Current version is 0.1.13, released as a stable package with no recent changes.

pip install tokentrim
error ModuleNotFoundError: No module named 'tiktoken'
cause Missing dependency tiktoken.
fix
Install tiktoken: pip install tiktoken
error KeyError: 'model'
cause The model string provided is not recognized by tiktoken.
fix
Use a valid model like 'gpt-3.5-turbo', 'gpt-4', or 'text-davinci-003'.
gotcha If the last message is a user message and trimming removes it, the function may return an empty list or incomplete conversation.
fix Ensure that the last message is preserved or handle the case where no messages remain.
gotcha The 'model' parameter must match a model supported by tiktoken; otherwise, it may fall back to a default encoding or raise an error.
fix Use a known model string like 'gpt-3.5-turbo' or 'gpt-4'.
deprecated Future versions may change default trimming behavior; check documentation for updates.
fix Always specify max_tokens explicitly to avoid breaking changes.

Basic usage: trim a list of messages to a maximum token count for a given model.

import tokentrim

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me about Python."}
]

# Trim messages to fit within 100 tokens
trimmed_messages = tokentrim.trim_messages(messages, model="gpt-3.5-turbo", max_tokens=100)
print(trimmed_messages)