danneu / telegram-chatgpt-bot

a Telegram ChatGPT bot that supports text prompts and two-way voice memos
33 stars 8 forks source link

Optimize token count on message history #2

Open danneu opened 1 year ago

danneu commented 1 year ago

Using GPT-3.5 to summarize its own answers shrinks token count by ~80% while preserving context.

This naive prompt works well enough: Summarize this: ${answer}

It also does decent token compression: Summarize this and use the fewest and shortest words possible: ${answer}

This alone can 5x the amount of past conversation that gets included in the chat completion.

Idea:

This will double the amount of token I/O (aka cost) since the whole message needs to be sent back to ChatGPT, but it dramatically increases context.

Further thinking: It only makes sense to use summaries of messages that are N messages old. If we use summaries of the latest messages, then we can ruin the conversation. Consider this example:

  1. Me: Explain to me the human body in great detail.
  2. Bot: {A very long message}
  3. Me: Summarize that.
  4. Bot: {Summary 1}
  5. Me: Wait, summarize that in Spanish.
  6. Bot: {Summary 2}
  7. Me: Summarize that even further by using only simple words.
  8. Bot: {Summary 3}

In this example, using the summary of message 2 instead of the full message will ruin context for all subsequent user prompts. Something to think about.