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?
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({}))
Look at how ConversationBufferMemory formats messages with 'Human:' and 'AI:' prefixes.
The ConversationBufferMemory stores messages with 'Human:' prefix for user and 'AI:' prefix for assistant. The load_memory_variables method returns a dict with the key 'chat_history' and the conversation as a string.
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?
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)
Remember how ConversationBufferMemory prefixes messages.
The memory stores messages with 'Human:' and 'AI:' prefixes in the order they were added. The last user message is included.
Which of the following code snippets correctly creates a ConversationBufferMemory with the memory key set to "session_memory"?
Check the exact parameter name and type for memory key.
The correct parameter name is memory_key and it expects a string. Option D uses the correct syntax.
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'?
from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory(memory_key="chat") memory.add_user_message("Hello") print(memory.load_memory_variables({}))
Check which object has the method add_user_message.
The add_user_message method belongs to the chat_memory attribute inside ConversationBufferMemory, not the memory object itself.
In LangChain, what is the main benefit of using memory-augmented retrieval in conversational AI?
Think about how memory helps AI remember previous messages.
Memory-augmented retrieval enables the AI to access past conversation history, making responses more relevant and context-aware.