Complete the code to define a state schema with LangChain's BaseMemory.
from langchain.memory import BaseMemory class MyMemory(BaseMemory): def __init__(self): self.state = [1]
The state should be initialized as an empty dictionary {} to store key-value pairs.
Complete the method to get a value from the state dictionary safely.
def get_value(self, key): return self.state.[1](key, None)
pop which removes the key from the dictionary.The get method returns the value for the given key or None if the key is missing.
Fix the error in the method that updates the state with a new key-value pair.
def update_state(self, key, value): self.state[1] = value
To update a dictionary entry, use square brackets with the key: self.state[key] = value.
Fill both blanks to define a method that clears the state dictionary.
def clear_state(self): self.state.[1]() self.state = [2]
reset() which is not a dictionary method.The clear() method empties the dictionary, and reassigning {} resets it to empty.
Fill all three blanks to define a method that returns a copy of the current state dictionary.
def get_state_copy(self): return self.state.[1]() # Usage: copy = memory.[2]() print(copy.[3])
clear() which empties the dictionary instead of copying.The copy() method returns a shallow copy of the dictionary.
The method is called get_state_copy().
Using keys() on the copy returns all keys.