0
0
LangChainframework~20 mins

Chat history management in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Chat History Master
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 chat history code?
Consider this code snippet using LangChain's ConversationBufferMemory to store chat history. What will be the printed output?
LangChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({'input': 'Hello'}, {'output': 'Hi there!'})
memory.save_context({'input': 'How are you?'}, {'output': 'I am fine, thanks!'})
print(memory.load_memory_variables({})['history'])
AHuman: Hello\nHuman: How are you?\nAI: Hi there!\nAI: I am fine, thanks!
BHello\nHi there!\nHow are you?\nI am fine, thanks!
C['Hello', 'Hi there!', 'How are you?', 'I am fine, thanks!']
DHuman: Hello\nAI: Hi there!\nHuman: How are you?\nAI: I am fine, thanks!
Attempts:
2 left
💡 Hint
Look at how ConversationBufferMemory formats the history string with 'Human:' and 'AI:' prefixes.
state_output
intermediate
2:00remaining
What is the value of the chat history after adding a new message?
Using LangChain's ConversationBufferMemory, after saving these contexts, what is the value of memory.chat_memory.messages?
LangChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({'input': 'Hi'}, {'output': 'Hello!'})
memory.save_context({'input': 'What is your name?'}, {'output': 'I am LangChain.'})
messages = memory.chat_memory.messages
A[{'role': 'human', 'content': 'Hi'}, {'role': 'ai', 'content': 'Hello!'}, {'role': 'human', 'content': 'What is your name?'}, {'role': 'ai', 'content': 'I am LangChain.'}]
B['Hi', 'Hello!', 'What is your name?', 'I am LangChain.']
C[HumanMessage(content='Hi'), AIMessage(content='Hello!'), HumanMessage(content='What is your name?'), AIMessage(content='I am LangChain.')]
D['Human: Hi', 'AI: Hello!', 'Human: What is your name?', 'AI: I am LangChain.']
Attempts:
2 left
💡 Hint
Check the type of objects stored in chat_memory.messages.
📝 Syntax
advanced
2:00remaining
Which option correctly initializes a LangChain chat memory with a custom key?
You want to create a ConversationBufferMemory that stores history under the key 'chat_history'. Which code snippet is correct?
Amemory = ConversationBufferMemory(key='chat_history')
Bmemory = ConversationBufferMemory(memory_key='chat_history')
Cmemory = ConversationBufferMemory(history_key='chat_history')
Dmemory = ConversationBufferMemory(memoryKey='chat_history')
Attempts:
2 left
💡 Hint
Check the official parameter name for setting the memory key in ConversationBufferMemory.
🔧 Debug
advanced
2:00remaining
Why does this LangChain chat memory code raise an AttributeError?
Given this code, why does it raise AttributeError: 'ConversationBufferMemory' object has no attribute 'messages'?
LangChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
print(memory.messages)
ABecause <code>ConversationBufferMemory</code> does not have a direct <code>messages</code> attribute; messages are inside <code>chat_memory</code>.
BBecause <code>messages</code> is a private attribute and cannot be accessed directly.
CBecause <code>ConversationBufferMemory</code> must be initialized with <code>messages=True</code> to have that attribute.
DBecause the import statement is incorrect and does not import <code>ConversationBufferMemory</code> properly.
Attempts:
2 left
💡 Hint
Check the structure of ConversationBufferMemory and where messages are stored.
🧠 Conceptual
expert
3:00remaining
Which LangChain memory type best suits a chat app needing to remember only the last 3 exchanges?
You are building a chat app that should keep only the last 3 user and AI messages in memory to save space. Which LangChain memory class is best to use?
AConversationBufferWindowMemory with k=3
BConversationBufferMemory with memory_key='last_3'
CConversationSummaryMemory with max_tokens=3
DConversationTokenBufferMemory with token_limit=3
Attempts:
2 left
💡 Hint
Think about which memory type limits the number of recent messages stored.