Say More with 30% Fewer Words

3 min read

I've been experimenting with LLMs lately, and I stumbled upon an article that really caught my eye - "Say More with Less - Prompt Compression". The author suggests that the key to getting more out of your language model is to give it a more concise and well-crafted prompt. I was intrigued by this idea and decided to dig deeper.

So, what is prompt compression, exactly? In simple terms, it's the process of distilling a long, complex prompt into a shorter, more manageable one that still conveys the same information. This can be especially useful when working with LLMs, which can be prone to getting confused or producing irrelevant results if given too much information at once.

One of the main benefits of prompt compression is that it can help reduce the amount of data that needs to be processed by the LLM. According to a study on prompt compression, using a more concise prompt can lead to a 30% reduction in data processing time. This can result in faster response times and improved overall performance.

But how do you actually compress a prompt, you might ask? The article mentions a few different techniques for prompt compression, including using synonyms and reducing the number of words. For example, instead of saying "Write a short story about a character who is feeling sad and lonely," you could say "Write a short story about a character feeling down."

python
import nltk
from nltk.corpus import wordnet

def get_synonyms(word):
    synonyms = set()
    for syn in wordnet.synsets(word):
        for lemma in syn.lemmas():
            synonyms.add(lemma.name())
    return list(synonyms)

# Get synonyms for the word "sad"
sad_synonyms = get_synonyms("sad")
print(sad_synonyms)

This code uses the NLTK library to get a list of synonyms for the word "sad". You can then use these synonyms to replace the original word in your prompt.

Another technique mentioned in the article is using a technique called "prompt pruning," which involves removing unnecessary words or phrases from the prompt. For example, if you're asking the LLM to write a short story, you might remove the words "short story" from the prompt.

bash
echo "Write a story about a character feeling down" | sed 's/story//'

This code uses the sed command to remove the word "story" from the prompt.

Overall, prompt compression is an important technique for getting more out of your LLM. By distilling complex prompts into shorter, more manageable ones, you can improve the performance and accuracy of your model.

Try this

  • Use the NLTK library to get a list of synonyms for a word: pip install nltk and then run python -c "import nltk; from nltk.corpus import wordnet; print(wordnet.synsets('sad'))"
  • Use the sed command to remove a word from a prompt: echo "Write a story about a character feeling down" | sed 's/story//'

Sources