Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions ai-integrations/langchain-memory-semantic-cache.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"outputs": [],
"source": [
"pip install --quiet --upgrade langchain langchain-community langchain-core langchain-mongodb langchain-voyageai langchain-openai langchain-text-splitters pypdf"
"pip install --quiet --upgrade langchain langchain-community langchain-core langchain-mongodb langchain-voyageai langchain-openai langchain-text-splitters langgraph langgraph-checkpoint-mongodb pypdf"
]
},
{
Expand Down Expand Up @@ -141,18 +141,15 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory\n",
"from langchain_core.runnables.history import RunnableWithMessageHistory\n",
"from langgraph.checkpoint.mongodb import MongoDBSaver\n",
"from langchain_core.prompts import MessagesPlaceholder\n",
" \n",
"# Define a function that gets the chat message history \n",
"def get_session_history(session_id: str) -> MongoDBChatMessageHistory:\n",
" return MongoDBChatMessageHistory(\n",
" connection_string=MONGODB_URI,\n",
" session_id=session_id,\n",
" database_name=\"langchain_db\",\n",
" collection_name=\"rag_with_memory\"\n",
" )"
"from pymongo import MongoClient\n",
"\n",
"# Connect to your MongoDB cluster\n",
"mongo_client = MongoClient(MONGODB_URI)\n",
"\n",
"# Create the checkpointer to persist conversation state\n",
"checkpointer = MongoDBSaver(mongo_client, db_name=\"langchain_db\")"
]
},
{
Expand Down Expand Up @@ -229,6 +226,9 @@
"metadata": {},
"outputs": [],
"source": [
"from langgraph.graph import StateGraph, START, END, MessagesState\n",
"from langchain_core.messages import AIMessage\n",
"\n",
"# Build the RAG chain\n",
"rag_chain = (\n",
" retriever_chain\n",
Expand All @@ -237,13 +237,23 @@
" | parse_output\n",
")\n",
"\n",
"# Wrap the chain with message history\n",
"rag_with_memory = RunnableWithMessageHistory(\n",
" rag_chain,\n",
" get_session_history,\n",
" input_messages_key=\"question\",\n",
" history_messages_key=\"history\",\n",
")"
"# Define the node that runs the RAG chain\n",
"def call_rag_chain(state: MessagesState):\n",
" question = state[\"messages\"][-1].content\n",
" history = state[\"messages\"][:-1]\n",
" answer = rag_chain.invoke(\n",
" {\"question\": question, \"history\": history}\n",
" )\n",
" return {\"messages\": [AIMessage(content=answer)]}\n",
"\n",
"# Build the graph\n",
"workflow = StateGraph(MessagesState)\n",
"workflow.add_node(\"rag\", call_rag_chain)\n",
"workflow.add_edge(START, \"rag\")\n",
"workflow.add_edge(\"rag\", END)\n",
"\n",
"# Compile the graph with the checkpointer\n",
"rag_with_memory = workflow.compile(checkpointer=checkpointer)"
]
},
{
Expand All @@ -255,10 +265,10 @@
"source": [
"# First question\n",
"response_1 = rag_with_memory.invoke(\n",
" {\"question\": \"What was MongoDB's latest acquisition?\"},\n",
" {\"configurable\": {\"session_id\": \"user_1\"}}\n",
" {\"messages\": [(\"human\", \"What was MongoDB's latest acquisition?\")]},\n",
" {\"configurable\": {\"thread_id\": \"user_1\"}}\n",
")\n",
"print(response_1)"
"print(response_1[\"messages\"][-1].content)"
]
},
{
Expand All @@ -270,10 +280,10 @@
"source": [
"# Follow-up question that references the previous question\n",
"response_2 = rag_with_memory.invoke(\n",
" {\"question\": \"Why did they do it?\"},\n",
" {\"configurable\": {\"session_id\": \"user_1\"}}\n",
" {\"messages\": [(\"human\", \"Why did they do it?\")]},\n",
" {\"configurable\": {\"thread_id\": \"user_1\"}}\n",
")\n",
"print(response_2)"
"print(response_2[\"messages\"][-1].content)"
]
},
{
Expand Down Expand Up @@ -320,8 +330,8 @@
"\n",
"# First query (not cached)\n",
"rag_with_memory.invoke(\n",
" {\"question\": \"What was MongoDB's latest acquisition?\"},\n",
" {\"configurable\": {\"session_id\": \"user_2\"}}\n",
" {\"messages\": [(\"human\", \"What was MongoDB's latest acquisition?\")]},\n",
" {\"configurable\": {\"thread_id\": \"user_2\"}}\n",
")"
]
},
Expand All @@ -336,8 +346,8 @@
"\n",
"# Second query (cached)\n",
"rag_with_memory.invoke(\n",
" {\"question\": \"What company did MongoDB acquire recently?\"},\n",
" {\"configurable\": {\"session_id\": \"user_2\"}}\n",
" {\"messages\": [(\"human\", \"What company did MongoDB acquire recently?\")]},\n",
" {\"configurable\": {\"thread_id\": \"user_2\"}}\n",
")"
]
}
Expand Down
Loading