0
0
LangChainframework~20 mins

Memory-augmented retrieval in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Mastery in LangChain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this LangChain memory retrieval code?

Consider this LangChain snippet using a simple memory retrieval:

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat_history")
memory.chat_memory.add_user_message("Hello")
memory.chat_memory.add_ai_message("Hi there!")
print(memory.load_memory_variables({}))

What will be printed?

LangChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat_history")
memory.chat_memory.add_user_message("Hello")
memory.chat_memory.add_ai_message("Hi there!")
print(memory.load_memory_variables({}))
A{"chat_history": "Human: Hello\nAI: Hi there!"}
B{"chat_history": "Hello\nHi there!"}
C{"chat_history": "User: Hello\nAssistant: Hi there!"}
D{"chat_history": "Hello"}
Attempts:
2 left
💡 Hint

Look at how ConversationBufferMemory formats messages with 'Human:' and 'AI:' prefixes.

state_output
intermediate
2:00remaining
What is the state of memory after adding messages?

Given this LangChain code snippet:

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="history")
memory.chat_memory.add_user_message("How are you?")
memory.chat_memory.add_ai_message("I'm fine, thanks!")
memory.chat_memory.add_user_message("What's the weather?")
state = memory.load_memory_variables({})
print(state)

What is the value of state?

LangChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="history")
memory.chat_memory.add_user_message("How are you?")
memory.chat_memory.add_ai_message("I'm fine, thanks!")
memory.chat_memory.add_user_message("What's the weather?")
state = memory.load_memory_variables({})
print(state)
A{"history": "User: How are you?\nAssistant: I'm fine, thanks!\nUser: What's the weather?"}
B{"history": "How are you?\nI'm fine, thanks!\nWhat's the weather?"}
C{"history": "Human: How are you?\nAI: I'm fine, thanks!\nHuman: What's the weather?"}
D{"history": "How are you?\nI'm fine, thanks!"}
Attempts:
2 left
💡 Hint

Remember how ConversationBufferMemory prefixes messages.

📝 Syntax
advanced
1:30remaining
Which option correctly initializes a memory with a custom key?

Which of the following code snippets correctly creates a ConversationBufferMemory with the memory key set to "session_memory"?

Amemory = ConversationBufferMemory(memory_key=session_memory)
Bmemory = ConversationBufferMemory(key="session_memory")
Cmemory = ConversationBufferMemory(memoryKey="session_memory")
Dmemory = ConversationBufferMemory(memory_key="session_memory")
Attempts:
2 left
💡 Hint

Check the exact parameter name and type for memory key.

🔧 Debug
advanced
2:00remaining
Why does this LangChain memory retrieval code raise an AttributeError?

Given this code:

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat")
memory.add_user_message("Hello")
print(memory.load_memory_variables({}))

Why does it raise AttributeError: 'ConversationBufferMemory' object has no attribute 'add_user_message'?

LangChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat")
memory.add_user_message("Hello")
print(memory.load_memory_variables({}))
ABecause add_user_message requires an argument named 'message'.
BBecause add_user_message is a method of chat_memory, not ConversationBufferMemory directly.
CBecause ConversationBufferMemory does not support adding messages manually.
DBecause memory_key must be 'chat_history' to use add_user_message.
Attempts:
2 left
💡 Hint

Check which object has the method add_user_message.

🧠 Conceptual
expert
2:30remaining
How does memory-augmented retrieval improve LangChain's response relevance?

In LangChain, what is the main benefit of using memory-augmented retrieval in conversational AI?

AIt allows the AI to recall past conversation context to provide more relevant and coherent responses.
BIt speeds up the response time by caching all previous API calls.
CIt automatically corrects grammar mistakes in user inputs before processing.
DIt encrypts conversation data to enhance security during retrieval.
Attempts:
2 left
💡 Hint

Think about how memory helps AI remember previous messages.