I am currently developing a website and aiming to implement a chat section using ChatGPT.
The issue I have encountered is that I’m unsure about how to effectively recall previous conversations of a user for ChatGPT.
I used to follow a method where I stored user questions and ChatGPT responses in a database, and I would retrieve them using an approach similar to the code snippet below:
The problem you describe is the main issue facing chat systems, in the case of ChatGPT, older context past the token limit is simply not used for the current query, it’s actually surprising how well this works, but can lead to people thinking the system has infinite memory, when it does not.
You could build summarisations of past chat context and try using that or simply truncate older information, see which works best for your use case.
I appreciate your suggestion about building summaries of past chat context. Could you please provide more guidance on how and where to create these summaries within the conversation flow?
Additionally, how would I go about reminding the system of these summaries during a conversation?
You would calculate the size (in tokens) of the totality of the context.
Once that size hits some limit (say, 80% of the token limit of the model,) you would call out to the model, without the user prompting, and prompt something like: "summarize this conversation, to be used as a prompt for continuing the conversation later" (and each of the previous messages attached)
Now, you store this summary in the database, and wherever you would previously use the preceding conversation, you instead use the summary in the prompt. Once the size of summary plus the continued context reaches the limit, you re-summarize the summary plus the successive conversation.
So, assuming you have a “conversation_id” you’d need two tables:
You would then generate the prompt history something like:
sum_id = select max(to_statement_index) from summaries where conversation_id = {conversation_id}
if sum_id:
history = select summary_text from summaries where to_statement_index = {sum_id} and conversation_id = {conversation_id}
else:
sum_id = 0
history += select statement_text from statements where statement_index > {sum_id} and conversation_id = {conversation_id}
-- now create chat completion based on the history
Obviously this can in addition be tweaked, keeping a few of the last “real” statements even when summarizing, trying to extract main themes to focus on in the summary, and so on.
I wanted to extend my sincere appreciation for your invaluable advice. Following your method for managing previous conversations with ChatGPT has yielded impressive results. The approach of summarizing conversations to fit token limits worked seamlessly and significantly improved the efficiency of the chat section on my website.
Thank you once again for your expertise and assistance.