LangChain provides optional caching layer for LLMs.
This is useful for two reasons:
When requesting the same completions multiple times, it can reduce the number of API calls to the LLM provider and thus save costs.
By reduing the number of API calls to the LLM provider, it can improve the running time of the application.
In this tutorial, we will use gpt-4o-mini OpenAI API and utilize two kinds of cache, InMemoryCache and SQLiteCache.
At end of each section we will compare wall times between before and after caching.
South Korea, located on the Korean Peninsula, is known for its rich culture, advanced technology, and vibrant economy. It features bustling cities like Seoul, renowned cuisine, and historic landmarks.
CPU times: total: 93.8 ms
Wall time: 1.54 s
InMemoryCache
First, cache the answer to the same question using InMemoryCache.
from langchain_core.globals import set_llm_cache
from langchain_core.caches import InMemoryCache
# Set InMemoryCache
set_llm_cache(InMemoryCache())
South Korea is a technologically advanced country known for its fast-paced lifestyle, vibrant culture, and delicious cuisine. It is a leader in industries such as electronics, automotive, and entertainment. The country also has a rich history and beautiful landscapes, making it a popular destination for tourists.
CPU times: total: 0 ns
Wall time: 996 ms
South Korea is a technologically advanced country known for its fast-paced lifestyle, vibrant culture, and delicious cuisine. It is a leader in industries such as electronics, automotive, and entertainment. The country also has a rich history and beautiful landscapes, making it a popular destination for tourists.
CPU times: total: 0 ns
Wall time: 3 ms
Note that if we set InMemoryCache again, the cache will be lost and the wall time will increase.
South Korea is a tech-savvy, modern country known for its vibrant culture, delicious cuisine, and booming economy. It is a highly developed nation with advanced infrastructure, high standards of living, and a strong emphasis on education. The country also has a rich history and is famous for its K-pop music and entertainment industry.
CPU times: total: 0 ns
Wall time: 972 ms
SQLiteCache
Now, we cache the answer to the same question by using SQLiteCache.
from langchain_community.cache import SQLiteCache
from langchain_core.globals import set_llm_cache
import os
# Create cache directory
if not os.path.exists("cache"):
os.makedirs("cache")
# Set SQLiteCache
set_llm_cache(SQLiteCache(database_path="cache/llm_cache.db"))
South Korea is a technologically advanced country in East Asia, known for its booming economy, vibrant pop culture, and rich history. It is home to K-pop, Samsung, and delicious cuisine like kimchi. The country also faces tensions with North Korea and strives for reunification.
CPU times: total: 31.2 ms
Wall time: 953 ms
South Korea is a technologically advanced country in East Asia, known for its booming economy, vibrant pop culture, and rich history. It is home to K-pop, Samsung, and delicious cuisine like kimchi. The country also faces tensions with North Korea and strives for reunification.
CPU times: total: 375 ms
Wall time: 375 ms
Note that if we use SQLiteCache, setting caching again does not delete stored cache.
South Korea is a technologically advanced country in East Asia, known for its booming economy, vibrant pop culture, and rich history. It is home to K-pop, Samsung, and delicious cuisine like kimchi. The country also faces tensions with North Korea and strives for reunification.
CPU times: total: 0 ns
Wall time: 4.01 ms