fix: Set the max history messages to 9 #438
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Set the default context to send only the latest 9 historical messages to avoid exceeding the tokens limit error after chatting for a period of time.
Change the maximum number of history messages by setting the environment variable
PUBLIC_MAX_HISTORY_MESSAGES
if needed.Linked Issues
#309
Additional context
Anse
is a very useful application, butChatGPT-Demo
is more suitable for deployment to relatives and friends who don’t know much about software.However, during the use of
ChatGPT-Demo
, when the chat goes on for a long time, thecontext_length_exceeded
error will easily occur.After studying the file code of
ChatGPT-Demo
, I found that when it sends a message to the OpenAI API, it will send all historical messages together. No wonder it exceeds the token limit after chatting for a period of time.This will also cause excessive consumption of tokens.
Therefore, I think it might be appropriate to set a certain length limit on the context messages sent to the OpenAI API. This can ensure that the chat continues in most cases.
Even though the AI might become "forgetful," it would still provide a better experience than clearing all chat history and starting over.
I set the default context message count to 9, which allows the OpenAI API to receive the latest 4 complete question and answer exchanges, as well as the most recent question.