Good Idea! Let's make it better!
Main thing that comes to mind, a lot of tokens already come with spaces at the start of the word. Would removing them make things worse?
I put 'ButifIwrotewithoutspacesyouwouldprobablystillunderstandme' into openai's tokenizer.
It has 17 tokens.
I added the spaces back in to get 'But if I wrote without spaces you would probably still under stand me'.
It has 13.
Okay, so taking spaces out made it longer instead of shorter, but it's also a short bit of text and could be a fluke. What about your Tolkien example?
Your original Tolkien text has 187 tokens. Your compressed version without spaces has 160.
If you add spaces back in it only has 132 tokens (and 508 characters).
This is what I used:
"dont human also genuin origin idea? ”Come, read fantasi book. either Tolkien clone, Song Ice Fire. Tolkien professor Anglo-Saxon languag cultur; no secret got inspir. Song Ice Fire War Rose dragon. Lannist Stark Lancast York, map Westero Britain (minus Scotland) upsid down - ireland stuck bottom – wake, sheepl! Dullard blend Tolkien slurri shape anoth tolkien - clon. tolkien - level artist genius blend human experi, histori, artist corpus slurri form entir new genr. , differ fine blend spice add slurri."
Nice! Last weekend I expanded https://www.gptrim.com/ to allow the user to a) see savings in both characters and tokens; b) determine their own combination of word processing functions. Then I saw, like you said, that to save tokens you only want to remove stopwords. I will next add the option to remove punctuation. I also want to give users two general recipes: optimize for saving tokens vs. optimize for saving characters. Always happy to take more ideas.
I will probably write again on this, on my new personal Substack or other websites, reporting what I'v... (read more)
Based on this research, I have created a free web app for compressing GPT prompts at gptrim.com. gptrim is also a Python library. Contributions are welcome.
Readability and information
The other day I was trying to feed a large text to ChatGPT, and I grew frustrated with the limits of the chat message size. After some brainstorming with Bing, I had an insight.
Not all tokens in a text convey information. Some are there to make it easier for humans to parse text. Consider spaces: they are mostly used to tokenize words for the human visual cortex. ButifIwrotewithoutspacesyouwouldprobablystillunderstandme.
There are very few cases where a missing space would cause ambiguity. The nicest one ChatGPT could come up with was:
Even in such cases, the ambiguity would probably be solved by context.
What about punctuation? Again, I suspect there are very few cases where removing punctuation would create an ambiguity that cannot be solved by context. ChatGPT:
Can we go further? We could apply word stemming. This would significantly reduce our character count. How much information would we sacrifice? Try to read this snippet (full text below).
A machine learning researcher writes me in response to yesterday’s post, saying:
I resisted the urge to answer “Yeah, well, your mom is a brute-force statistical pattern matcher which blends up the internet and gives you back a slightly unappetizing slurry of it when asked.”
But I think it would have been true.
I find that "caveman Scott" is still very much readable, especially if I know the context.
Compressing prompts
Consider this snippet from Scott:
Let's remove spaces and punctuation and apply Snowball stemming. As an additional (and perhaps excessive) optimization, let's also remove stopwords.
We get something that looks like this:
Let's feed it into GPT-4 with the following prompt:
We get back this (I have bolded two sentences where I felt some meaning was lost):
Overall, I think it's a pretty good rendition. But much did we save? The original text has 187 tokens and 790 characters. The compressed text has 160 tokens and 428 characters. That's a 14% saving on tokens and a 46% saving on character count.
Overall, it seems this approach might be more useful for fighting ChatGPT and Bing's message character limit than reducing tokens. But even a 14% saving is significant if you are using GPT in production.
Conclusion
A few weeks ago Twitter discovered Shoggoth tongue, a technique for compressing GPT prompts by... asking GPT to do it. Shoggoth tongue may save a lot of tokens, but you also have to spend tokens to compress the text, so it only makes sense for crafting reusable prompts. I don't believe it applies to the majority of use cases, i.e. people solving mundane problems with ChatGPT.
Applying the standard NLP preprocessing routine to GPT prompts is a quick and free way to reduce character count and save some tokens while seemingly preserving most information.
I haven't had time to experiment extensively with prompt compression. I also did not try to quantify these results. I look forward to seeing new ideas in this space.