{"id":21569351,"url":"https://github.com/aweirddev/gpt-trim","last_synced_at":"2025-03-18T05:44:54.364Z","repository":{"id":195331393,"uuid":"692751848","full_name":"AWeirdDev/gpt-trim","owner":"AWeirdDev","description":"Trims the messages array for ChatGPT API","archived":false,"fork":false,"pushed_at":"2023-09-18T09:37:44.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-08T22:19:27.926Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AWeirdDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-17T13:30:29.000Z","updated_at":"2023-09-17T13:34:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"c5a8c732-de08-48aa-8277-bda2e9f07e36","html_url":"https://github.com/AWeirdDev/gpt-trim","commit_stats":null,"previous_names":["aweirdscratcher/gpt-trim","aweirddev/gpt-trim"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWeirdDev%2Fgpt-trim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWeirdDev%2Fgpt-trim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWeirdDev%2Fgpt-trim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AWeirdDev%2Fgpt-trim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AWeirdDev","download_url":"https://codeload.github.com/AWeirdDev/gpt-trim/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244166675,"owners_count":20409178,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T11:09:14.097Z","updated_at":"2025-03-18T05:44:54.345Z","avatar_url":"https://github.com/AWeirdDev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gpt-trim\n\nThis is a (slightly) faster version of [KillianLucas/tokentrim](https://pypi.org/project/tokentrim) for longer message arrays.\n\nIn average, gpt-trim is \\~80% faster than tokentrim, and that tokentrim is around 5x\\~7x slower.\n\nAlthough 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.\n\n## Usage\n\nThe usage is quite similiar to `tokentrim`.\n\n```python\nimport gpt_trim\n\ntrimmed = gpt_trim.trim(\n    messages, \n    model=\"gpt-3.5-turbo\"\n)\nprint(trimmed)\n```\n\nAlternatively, you can assign the token limit manually:\n\n```python\ngpt_trim.trim(\n    messages,\n    max_tokens=100\n)\n```\n\nYou can also add system messages with ease:\n\n```python\nimport gpt_trim\n\nmessages = [\n    ..., # long, long content\n    {\n        \"role\": \"user\",\n        \"content\": \"It's about drive, it's about power\"\n    }\n]\ntrimmed = gpt_trim.advanced_trim(\n    messages,\n    system_messages=[\n        {\n            \"role\": \"system\",\n            \"content\": \"You'll act like the celebrity: The Rock.\"\n        }\n    ],\n    model=\"gpt-3.5-turbo\",\n)\nprint(trimmed)\n```\n\nThe catch? It's slower. With great power comes great... patience.\n\n## Comparison\n\nYou can compare this project to [KillianLucas/tokentrim](https://pypi.org/project/tokentrim) like so:\n\n```python\nimport time\n\nimport gpt_trim\nimport tiktoken\nimport tokentrim\n\npattern = \"d!3h.l7$fj\" # 10 tokens\nmessages = [\n    {\n        \"role\": \"user\",\n        \"content\": pattern * 5000 # 50000 tokens\n    }\n]\n\n# cache first\nenc = tiktoken.get_encoding(\"cl100k_base\")\ngpt_trim.num_tokens_from_messages(\n    messages,\n    enc\n)\n\ndef test(provider):\n    print(\"Testing\", provider.__name__)\n\n    s = time.time()\n    result = provider.trim(\n        messages,\n        model=\"gpt-3.5-turbo\",\n    )\n\n    print(f\"took {(time.time() - s):.4f}s\\n\")\n\n# Swap the following for every test and see tokentrim \n# struggles when dealing with longer context.\ntest(gpt_trim)\ntest(tokentrim)\n```\n\n***\n\nRight. I was bored.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faweirddev%2Fgpt-trim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faweirddev%2Fgpt-trim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faweirddev%2Fgpt-trim/lists"}