diff --git a/404.html b/404.html index 3cc08db..41aab20 100644 --- a/404.html +++ b/404.html @@ -7,7 +7,7 @@ - +
diff --git a/assets/js/400dfa73.066bb592.js b/assets/js/400dfa73.c1d11f2d.js similarity index 70% rename from assets/js/400dfa73.066bb592.js rename to assets/js/400dfa73.c1d11f2d.js index 850ba81..21fbf6b 100644 --- a/assets/js/400dfa73.066bb592.js +++ b/assets/js/400dfa73.c1d11f2d.js @@ -1 +1 @@ -"use strict";(self.webpackChunkai_rag_lab=self.webpackChunkai_rag_lab||[]).push([[254],{6223:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>c,contentTitle:()=>t,default:()=>h,frontMatter:()=>i,metadata:()=>a,toc:()=>d});var r=s(4848),o=s(8453);const i={},t="\ud83d\udc50 Add memory to the RAG application",a={id:"add-memory/add-memory",title:"\ud83d\udc50 Add memory to the RAG application",description:'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.',source:"@site/docs/80-add-memory/2-add-memory.mdx",sourceDirName:"80-add-memory",slug:"/add-memory/add-memory",permalink:"/ai-rag-lab/docs/add-memory/add-memory",draft:!1,unlisted:!1,editUrl:"https://github.com/mongodb-developer/ai-rag-lab/blob/main/docs/80-add-memory/2-add-memory.mdx",tags:[],version:"current",sidebarPosition:2,frontMatter:{},sidebar:"tutorialSidebar",previous:{title:"\ud83d\udcd8 Tools, libraries, and concepts",permalink:"/ai-rag-lab/docs/add-memory/concepts"},next:{title:"\ud83c\udfaf Summary",permalink:"/ai-rag-lab/docs/summary"}},c={},d=[];function l(e){const n={code:"code",h1:"h1",p:"p",pre:"pre",strong:"strong",...(0,o.R)(),...e.components},{Details:s}=n;return s||function(e,n){throw new Error("Expected "+(n?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}("Details",!0),(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.h1,{id:"-add-memory-to-the-rag-application",children:"\ud83d\udc50 Add memory to the RAG application"}),"\n",(0,r.jsx)(n.p,{children:'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.'}),"\n",(0,r.jsxs)(n.p,{children:["Fill in any ",(0,r.jsx)(n.code,{children:"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 10: Add memory to the RAG application section in the notebook to add memory to the RAG application.
The answers for code blocks in this section are as follows:
-CODE_BLOCK_25
-history_collection.create_index("session_id")
CODE_BLOCK_26
-{
"session_id": session_id,
"role": role,
"content": content,
"timestamp": datetime.now(),
}
CODE_BLOCK_27
-history_collection.insert_one(message)
history_collection.create_index("session_id")
CODE_BLOCK_28
-history_collection.find({"session_id": session_id}).sort("timestamp", 1)
{
"session_id": session_id,
"role": role,
"content": content,
"timestamp": datetime.now(),
}
CODE_BLOCK_29
-{"role": msg["role"], "content": msg["content"]} for msg in cursor
history_collection.insert_one(message)
CODE_BLOCK_30
-message_history = retrieve_session_history(session_id)
messages += message_history
history_collection.find({"session_id": session_id}).sort("timestamp", 1)
CODE_BLOCK_31
-user_message = {"role": "user", "content": user_query}
messages.append(user_message)
{"role": msg["role"], "content": msg["content"]} for msg in cursor
CODE_BLOCK_32
+message_history = retrieve_session_history(session_id)
messages.extend(message_history)
CODE_BLOCK_33
+user_message = {"role": "user", "content": user_query}
messages.append(user_message)
CODE_BLOCK_34
store_chat_message(session_id, "user", user_query)
store_chat_message(session_id, "assistant", answer)