Skip to main content

gpt-trim

This is a (slightly) faster version of KillianLucas/tokentrim for longer message arrays.

In average, gpt-trim is ~80% faster than tokentrim, and that tokentrim is around 5x~7x slower.

Although gpt-trim is fast, I still need to finish my LeetCode problems that I left years ago, just so that I can make it 20x faster than 95% of people.

Usage

The usage is quite similiar to tokentrim.

import gpt_trim

trimmed = gpt_trim.trim(
    messages, 
    model="gpt-3.5-turbo"
)
print(trimmed)

Alternatively, you can assign the token limit manually:

gpt_trim.trim(
    messages,
    max_tokens=100
)

You can also add system messages with ease:

import gpt_trim

messages = [
    ..., # long, long content
    {
        "role": "user",
        "content": "It's about drive, it's about power"
    }
]
trimmed = gpt_trim.advanced_trim(
    messages,
    system_messages=[
        {
            "role": "system",
            "content": "You'll act like the celebrity: The Rock."
        }
    ],
    model="gpt-3.5-turbo",
)
print(trimmed)

The catch? It's slower. With great power comes great... patience.

Comparison

You can compare this project to KillianLucas/tokentrim like so:

import time

import gpt_trim
import tiktoken
import tokentrim

pattern = "d!3h.l7$fj" # 10 tokens
messages = [
    {
        "role": "user",
        "content": pattern * 5000 # 50000 tokens
    }
]

# cache first
enc = tiktoken.get_encoding("cl100k_base")
gpt_trim.num_tokens_from_messages(
    messages,
    enc
)

def test(provider):
    print("Testing", provider.__name__)

    s = time.time()
    result = provider.trim(
        messages,
        model="gpt-3.5-turbo",
    )

    print(f"took {(time.time() - s):.4f}s\n")

# Swap the following for every test and see tokentrim 
# struggles when dealing with longer context.
test(gpt_trim)
test(tokentrim)

Right. I was bored.

Metadata

Release files for gpt-trim 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gpt-trim 0.1
File Size Uploaded Provenance
gpt-trim-0.1.tar.gz 4.8 kB Without provenance Details

Release files / gpt-trim-0.1.tar.gz

Download URL gpt-trim-0.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
edda599ed622283d7b002ef992976a89f216c10655927f84fac68aae2e4a5526
BLAKE2b-256 checksum
How to use checksums
b5768689552465e3a4678982170f2a2509edccbc1dae4656518b97d0d52a4801
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Security for gpt-trim 0.1

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.

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page