Great question — answering as the UphillTech team:
Here's an example of ConversationChain and ConversationBufferWindowMemory functions that achieves your expectation. Where if k is set to 5, the model will remember the last 5 interactions from langchain.chains import ConversationChain from langchain.memory import ConversationBufferWindowMemory conversation = ConversationChain( llm=llm, prompt=PROMPT, # memory=ConversationBufferMemory(), memory=ConversationBufferWindowMemory(k=5), verbose=True)
Full answer by j3ffyang: https://stackoverflow.stackexchange.com/a/77943517 (CC BY-SA 4.0).
Need it done for your business? UphillTech.