๐ Add memory to the RAG application
In many Q&A applications we want to allow the user to have a back-and-forth conversation with the LLM, meaning the application needs some sort of "memory" of past questions and answers, and some logic for incorporating those into its current thinking. In this section, you will retrieve chat message history from MongoDB and incorporate it in your RAG application.
Fill in any <CODE_BLOCK_N> placeholders and run the cells under the Step 8: Add memory to the RAG application section in the notebook to add memory to the chatbot.
The answers for code blocks in this section are as follows:
CODE_BLOCK_19
Answer
history_collection.create_index("session_id")
CODE_BLOCK_20
Answer
history_collection.insert_one(message)
CODE_BLOCK_21
Answer
history_collection.find({"session_id": session_id}).sort("timestamp", 1)
CODE_BLOCK_22
Answer
retrieve_session_history(session_id)
CODE_BLOCK_23
Answer
{"role": "user", "content": user_query}
CODE_BLOCK_24
Answer
store_chat_message(session_id, "user", user_query)
store_chat_message(session_id, "assistant", answer)