Learn to use the LangChain ConversationSummaryMemory class.
Checkout final bot on HuggingFace space : acloudfan/chatbot-langchain-memory
def get_summarization_llm():
model = 'gpt-3.5-turbo-0125'
return ChatOpenAI(model=model, openai_api_key=openai_api_key)
if 'MEMORY' not in st.session_state:
memory = ConversationSummaryMemory(
llm = get_summarization_llm(),
human_prefix='user',
ai_prefix = 'assistant',
return_messages=True
)
# add to the session
st.session_state['MEMORY'] = memory
def get_llm_chain():
memory = st.session_state['MEMORY']
conversation = ConversationChain(
llm=get_llm(),
memory=memory
)
return conversation
st.divider()
st.subheader('Context/Summary:')
# Print the state of the buffer
st.session_state['MEMORY'].buffer
Solution uses the OpenAI gpt3.5-turbo model for both the chatbot responses and for context summarization. You may replace them with any model of your choice. The solution script is available in the project repository.