0
0
LangChainframework~10 mins

State schema definition in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a state schema with LangChain's BaseMemory.

LangChain
from langchain.memory import BaseMemory

class MyMemory(BaseMemory):
    def __init__(self):
        self.state = [1]
Drag options to blanks, or click blank then click option'
A''
B[]
C{}
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list or None instead of a dictionary for state.
2fill in blank
medium

Complete the method to get a value from the state dictionary safely.

LangChain
def get_value(self, key):
    return self.state.[1](key, None)
Drag options to blanks, or click blank then click option'
Afetch
Bpop
Cfind
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes the key from the dictionary.
3fill in blank
hard

Fix the error in the method that updates the state with a new key-value pair.

LangChain
def update_state(self, key, value):
    self.state[1] = value
Drag options to blanks, or click blank then click option'
A[key]
B(key)
C{key}
D.key
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces which cause syntax errors.
4fill in blank
hard

Fill both blanks to define a method that clears the state dictionary.

LangChain
def clear_state(self):
    self.state.[1]()
    self.state = [2]
Drag options to blanks, or click blank then click option'
Aclear
Breset
C{}
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using reset() which is not a dictionary method.
5fill in blank
hard

Fill all three blanks to define a method that returns a copy of the current state dictionary.

LangChain
def get_state_copy(self):
    return self.state.[1]()

# Usage:
copy = memory.[2]()
print(copy.[3])
Drag options to blanks, or click blank then click option'
Acopy
Bget_state_copy
Ckeys()
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear() which empties the dictionary instead of copying.