Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7765: Fix exceed context length error #8530

Merged
merged 5 commits into from
Nov 1, 2023

Conversation

rchan26
Copy link
Contributor

@rchan26 rchan26 commented Oct 27, 2023

Description

I was getting errors of exceeding my context window (see #7765) after a few messages and after some discussion, it seemed like the ChatMemoryBuffer is meant to guard against this and it does actually start to forget some history after a while. But a problem will occur if the system prompt (in the prefix-messages) is quite long and in addition to the chat history, we can actually get an error regarding exceeding the context window.

I should also add that the messages_to_prompts always expect that we have alternating user and assistant interactions and hence I wrote in a line so that when we are forgetting chat history, we delete pairs of messages so we still have these alternating messages. It's also possible to get errors if we forget chat history but we have the chat history starting with assistant messages in the chat history.

In this PR, I made some changes to the ChatMemoryBuffer.get() method by allowing you to pass in an initial_token_count argument which represents a kind of amount of context window to keep available and this gets added to each time we assess the token count of the chat history.

I have made changes to the context and simple chat engines where we first compute the number of tokens in the prefix-messages and pass that into the ChatMemoryBuffer.get() method and this ensures that we do not have a resulting all_messages which exceeds the length.

Happy to add some tests if feel necessary.

Fixes #7765 (this is currently closed, but I think it's still an open issue actually - see #7765 (comment))

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • I stared at the code and made sure it makes sense

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran make format; make lint to appease the lint gods

Copy link
Collaborator

@Disiok Disiok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into this and finding the true cause of the bug! 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Context length errors after a couple of chats using chat engine
2 participants