0
0
LangChainframework~10 mins

Session management for multi-user RAG in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new session ID for each user.

LangChain
from uuid import [1]

session_id = [1]()
Drag options to blanks, or click blank then click option'
Agetuuid
Brandom
Cuuid4
Duuid1
Attempts:
3 left
💡 Hint
Common Mistakes
Using random instead of uuid4 for unique session IDs.
Using uuid1 which includes MAC address and timestamp, less privacy.
2fill in blank
medium

Complete the code to initialize a LangChain ConversationBufferMemory for a user session.

LangChain
from langchain.memory import [1]

memory = [1]()
Drag options to blanks, or click blank then click option'
AConversationBufferMemory
BConversationSummaryMemory
CChatMessageHistory
DConversationBufferWindowMemory
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConversationSummaryMemory which summarizes instead of storing full history.
Using ChatMessageHistory which is a message container, not memory.
3fill in blank
hard

Fix the error in the code to retrieve the correct session memory from a sessions dictionary.

LangChain
sessions = {}

user_id = 'user123'
memory = sessions.get([1], None)
if memory is None:
    memory = ConversationBufferMemory()
    sessions[user_id] = memory
Drag options to blanks, or click blank then click option'
Asessions
Buser_id
C'user_id'
Dmemory
Attempts:
3 left
💡 Hint
Common Mistakes
Using the string 'user_id' instead of the variable user_id.
Trying to get memory with an undefined variable.
4fill in blank
hard

Fill both blanks to update the retriever and memory for a new user session.

LangChain
from langchain.vectorstores import FAISS

retriever = FAISS.load_local('index_dir', [1])
memory = [2]()
Drag options to blanks, or click blank then click option'
Aembedding_function
BConversationBufferMemory
COpenAIEmbeddings()
DChatMessageHistory
Attempts:
3 left
💡 Hint
Common Mistakes
Passing embedding_function string instead of an embeddings object.
Using ChatMessageHistory instead of ConversationBufferMemory.
5fill in blank
hard

Fill the blanks to create a multi-user session manager class with memory and retriever.

LangChain
class SessionManager:
    def __init__(self, retriever):
        self.sessions = {}
        self.retriever = [1]

    def get_session(self, user_id):
        if user_id not in self.sessions:
            self.sessions[user_id] = [2]()
        return self.sessions[user_id]

    def query(self, user_id, question):
        memory = self.get_session(user_id)
        # Use retriever and memory to answer question
        results = self.retriever.similarity_search(question)
        # Imagine combining results with memory here
        return results
Drag options to blanks, or click blank then click option'
Aretriever
BConversationBufferMemory
CConversationSummaryMemory
DChatMessageHistory
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChatMessageHistory instead of ConversationBufferMemory for session memory.
Not storing retriever correctly in the class.