0
0
LangChainframework~10 mins

Caching strategies for cost reduction 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 enable simple in-memory caching in LangChain.

LangChain
from langchain.cache import [1]

cache = [1]()
Drag options to blanks, or click blank then click option'
ADiskCache
BInMemoryCache
CRedisCache
DNoCache
Attempts:
3 left
💡 Hint
Common Mistakes
Using DiskCache or RedisCache without setup causes errors.
Using NoCache disables caching.
2fill in blank
medium

Complete the code to set the cache for LangChain's global cache system.

LangChain
from langchain.cache import cache

cache.[1] = InMemoryCache()
Drag options to blanks, or click blank then click option'
Acache
Bset_cache
Cenabled
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method like set_cache instead of assigning.
Using clear which removes cache data.
3fill in blank
hard

Fix the error in the code to use RedisCache properly for caching.

LangChain
from langchain.cache import RedisCache

cache = RedisCache(redis_url='[1]')
Drag options to blanks, or click blank then click option'
Aredis://localhost:6379
Bhttp://localhost:6379
Clocalhost:6379
Dredis://127.0.0.1
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'redis://' prefix causes connection errors.
Using 'http://' instead of 'redis://'.
4fill in blank
hard

Fill both blanks to create a cache key function that uses the input text length and a prefix.

LangChain
def cache_key_function(input_text):
    return f"[1]_[2]"
Drag options to blanks, or click blank then click option'
A"prefix"
Blen(input_text)
Cinput_text
D"cachekey"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole input text as a key can cause very long keys.
Omitting the prefix reduces key uniqueness.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension caching only results with cost below a threshold.

LangChain
costs = {'a': 10, 'b': 5, 'c': 20}
cached = {k: v for k, v in costs.items() if v [1] [2] and k != [3]
Drag options to blanks, or click blank then click option'
A<
B15
C'c'
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' changes the filter logic.
Not excluding the key 'c' causes unwanted caching.