get to the other side!'# The second time it is, so it goes fasterllm("Tell me a joke")    CPU times: user 1.59 ms, sys: 610 ¬µs, total: 2.2 ms    Wall time: 5.58 ms    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'Semantic Cache‚ÄãUse Redis to cache prompts and responses and evaluate hits based on semantic similarity.from langchain.embeddings import OpenAIEmbeddingsfrom langchain.cache import RedisSemanticCacheset_llm_cache(    RedisSemanticCache(        redis_url="redis://localhost:6379", embedding=OpenAIEmbeddings()    ))# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 351 ms, sys: 156 ms, total: 507 ms    Wall time: 3.37 s    "\n\nWhy don't scientists trust atoms?\nBecause they make up everything."# The second time, while not a direct hit, the question is semantically similar to the original question,# so it uses the cached result!llm("Tell me one joke")    CPU times: user 6.25 ms, sys: 2.72 ms, total: 8.97 ms    Wall time: 262 ms    "\n\nWhy don't scientists trust atoms?\nBecause they make up everything."GPTCache‚ÄãWe can use GPTCache for exact match caching OR to cache results based on semantic similarityLet's first start with an example of exact matchfrom gptcache import Cachefrom gptcache.manager.factory import manager_factoryfrom gptcache.processor.pre import get_promptfrom langchain.cache import GPTCacheimport hashlibdef get_hashed_name(name):    return hashlib.sha256(name.encode()).hexdigest()def init_gptcache(cache_obj: Cache, llm: str):    hashed_llm = get_hashed_name(llm)    cache_obj.init(        pre_embedding_func=get_prompt,        data_manager=manager_factory(manager="map", data_dir=f"map_cache_{hashed_llm}"),    )set_llm_cache(GPTCache(init_gptcache))# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 21.5 ms, sys: 21.3 ms, total: 42.8 ms    Wall time: 6.2 s    '\n\nWhy did the chicken cross the road?\n\nTo get
This notebook covers how to cache results of individual LLM calls using different caches.
This notebook covers how to cache results of individual LLM calls using different caches. ->: get to the other side!'# The second time it is, so it goes fasterllm("Tell me a joke")    CPU times: user 1.59 ms, sys: 610 ¬µs, total: 2.2 ms    Wall time: 5.58 ms    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'Semantic Cache‚ÄãUse Redis to cache prompts and responses and evaluate hits based on semantic similarity.from langchain.embeddings import OpenAIEmbeddingsfrom langchain.cache import RedisSemanticCacheset_llm_cache(    RedisSemanticCache(        redis_url="redis://localhost:6379", embedding=OpenAIEmbeddings()    ))# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 351 ms, sys: 156 ms, total: 507 ms    Wall time: 3.37 s    "\n\nWhy don't scientists trust atoms?\nBecause they make up everything."# The second time, while not a direct hit, the question is semantically similar to the original question,# so it uses the cached result!llm("Tell me one joke")    CPU times: user 6.25 ms, sys: 2.72 ms, total: 8.97 ms    Wall time: 262 ms    "\n\nWhy don't scientists trust atoms?\nBecause they make up everything."GPTCache‚ÄãWe can use GPTCache for exact match caching OR to cache results based on semantic similarityLet's first start with an example of exact matchfrom gptcache import Cachefrom gptcache.manager.factory import manager_factoryfrom gptcache.processor.pre import get_promptfrom langchain.cache import GPTCacheimport hashlibdef get_hashed_name(name):    return hashlib.sha256(name.encode()).hexdigest()def init_gptcache(cache_obj: Cache, llm: str):    hashed_llm = get_hashed_name(llm)    cache_obj.init(        pre_embedding_func=get_prompt,        data_manager=manager_factory(manager="map", data_dir=f"map_cache_{hashed_llm}"),    )set_llm_cache(GPTCache(init_gptcache))# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 21.5 ms, sys: 21.3 ms, total: 42.8 ms    Wall time: 6.2 s    '\n\nWhy did the chicken cross the road?\n\nTo get