Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
We use uuid4() to generate a unique session ID for each user session.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConversationSummaryMemory which summarizes instead of storing full history.
Using ChatMessageHistory which is a message container, not memory.
✗ Incorrect
ConversationBufferMemory stores the full conversation history in memory for the session.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We must use the variable user_id as the key to get the session memory, not the string 'user_id'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing embedding_function string instead of an embeddings object.
Using ChatMessageHistory instead of ConversationBufferMemory.
✗ Incorrect
FAISS.load_local requires the embedding function like OpenAIEmbeddings(), and memory should be ConversationBufferMemory.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChatMessageHistory instead of ConversationBufferMemory for session memory.
Not storing retriever correctly in the class.
✗ Incorrect
The retriever is passed in init, memory is ConversationBufferMemory for sessions, and ConversationSummaryMemory is a distractor here.