Como armazenar em cache chamadas do LLM

Entenda como armazenar em cache chamadas do LLM em diferentes cenários.

Ricardo Reis
8 min readMay 20, 2023

--

A seguir vamos abordar como armazenar em cache os resultados de chamadas LLM individuais.

from langchain.llms import OpenAI

In Memory Cache

Ao usar InMemoryCache as chamadas do LLM são armazenadas em um cache em memória. O código inicializa o cache em memória e, em seguida, usa o modelo LLM para gerar uma resposta a uma frase específica (“Tell me a joke”). A primeira vez que a frase é usada, o tempo de execução é maior, pois o modelo precisa calcular a resposta. A segunda vez que a mesma frase é usada, o tempo de execução é muito menor, pois a resposta é recuperada do cache.

from langchain.llms import OpenAI
import langchain
from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()

# To make the caching really obvious, lets use a slower model.
llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)

%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")

Observe que %%time é uma função mágica (também conhecida como “comando mágico”) usada no Jupyter Notebook para medir o tempo de execução de uma célula específica do notebook. Você pode usar a função %%time no Visual Studio Code (VS Code) quando estiver usando a extensão Jupyter para VS Code. Isso permitirá que você crie e execute células de código em um notebook Jupyter diretamente no VS Code.

Resultado:

CPU times: user 26.1 ms, sys: 21.5 ms, total: 47.6 ms
Wall time: 1.68 s
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

Ao executar uma segunda chamada:

%%time
# The second time it is, so it goes faster
llm("Tell me a joke")

Resultado:

CPU times: total: 0 µs
Wall time: 0 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

SQLite Cache

Similar ao exemplo anterior, mas as respostas são armazenadas em um banco de dados SQLite local. Ele remove qualquer banco de dados existente antes de começar para começar do zero.

!rm .langchain.db
# We can do the same thing with a SQLite cache
from langchain.cache import SQLiteCache
langchain.llm_cache = SQLiteCache(database_path=".langchain.db")
%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")
CPU times: user 17 ms, sys: 9.76 ms, total: 26.7 ms
Wall time: 825 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
%%time
# The second time it is, so it goes faster
llm("Tell me a joke")
CPU times: user 2.46 ms, sys: 1.23 ms, total: 3.7 ms
Wall time: 2.67 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

Redis Cache

Standard Cache

Neste exemplo, Redis é usado para armazenar o cache. O Redis é um banco de dados em memória que pode ser muito rápido para ler e escrever.

Use o Redis para armazenar prompts e respostas em cache.

# We can do the same thing with a Redis cache
# (make sure your local Redis instance is running first before running this example)
from redis import Redis
from langchain.cache import RedisCache

langchain.llm_cache = RedisCache(redis_=Redis())
%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")
CPU times: user 6.88 ms, sys: 8.75 ms, total: 15.6 ms
Wall time: 1.04 s
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'
%%time
# The second time it is, so it goes faster
llm("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 o Redis para armazenar prompts e respostas e avaliar ocorrências com base na semelhança semântica.

O RedisSemanticCache é um exemplo avançado onde o cache é baseado na semelhança semântica. Ou seja, mesmo que a pergunta não seja exatamente igual à pergunta anterior, se for semântica semelhante, a resposta em cache será usada.

from langchain.embeddings import OpenAIEmbeddings
from langchain.cache import RedisSemanticCache


langchain.llm_cache = RedisSemanticCache(
redis_url="redis://localhost:6379",
embedding=OpenAIEmbeddings()
)
%%time
# The first time, it is not yet in cache, so it should take longer
llm("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."
%%time
# 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

Aqui, o GPTCache é usado para cache de correspondência exata ou para armazenar resultados com base na semelhança semântica. Ou seja, podemos usar GPTCache para cache de correspondência exata OU para armazenar em cache os resultados com base na semelhança semântica

Vamos começar com um exemplo de correspondência exata:

from gptcache import Cache
from gptcache.manager.factory import manager_factory
from gptcache.processor.pre import get_prompt
from langchain.cache import GPTCache

# Avoid multiple caches using the same file, causing different llm model caches to affect each other

def init_gptcache(cache_obj: Cache, llm str):
cache_obj.init(
pre_embedding_func=get_prompt,
data_manager=manager_factory(manager="map", data_dir=f"map_cache_{llm}"),
)

langchain.llm_cache = GPTCache(init_gptcache)
%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")
CPU times: user 8.6 ms, sys: 3.82 ms, total: 12.4 ms
Wall time: 881 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
%%time
# The second time it is, so it goes faster
llm("Tell me a joke")
CPU times: user 286 µs, sys: 21 µs, total: 307 µs
Wall time: 316 µs
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

Vamos agora mostrar um exemplo de cache de similaridade:

from gptcache import Cache
from gptcache.adapter.api import init_similar_cache
from langchain.cache import GPTCache

# Avoid multiple caches using the same file, causing different llm model caches to affect each other

def init_gptcache(cache_obj: Cache, llm str):
init_similar_cache(cache_obj=cache_obj, data_dir=f"similar_cache_{llm}")

langchain.llm_cache = GPTCache(init_gptcache)
%%time
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")
CPU times: user 1.01 s, sys: 153 ms, total: 1.16 s
Wall time: 2.49 s
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
%%time
# This is an exact match, so it finds it in the cache
llm("Tell me a joke")
CPU times: user 745 ms, sys: 13.2 ms, total: 758 ms
Wall time: 136 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
%%time
# This is not an exact match, but semantically within distance so it hits!
llm("Tell me joke")
CPU times: user 737 ms, sys: 7.79 ms, total: 745 ms
Wall time: 135 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

SQLAlchemy Cache

Este exemplo mostra como usar o SQLAlchemy para criar um cache em qualquer banco de dados SQL suportado por SQLAlchemy. No entanto, este exemplo está comentado e não foi executado no notebook.

# You can use SQLAlchemyCache to cache with any SQL database supported by SQLAlchemy.

# from langchain.cache import SQLAlchemyCache
# from sqlalchemy import create_engine

# engine = create_engine("postgresql://postgres:postgres@localhost:5432/postgres")
# langchain.llm_cache = SQLAlchemyCache(engine)

Custom SQLAlchemy Schemas

# You can define your own declarative SQLAlchemyCache child class to customize the schema used for caching. For example, to support high-speed fulltext prompt indexing with Postgres, use:

from sqlalchemy import Column, Integer, String, Computed, Index, Sequence
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import TSVectorType
from langchain.cache import SQLAlchemyCache

Base = declarative_base()


class FulltextLLMCache(Base): # type: ignore
"""Postgres table for fulltext-indexed LLM Cache"""

__tablename__ = "llm_cache_fulltext"
id = Column(Integer, Sequence('cache_id'), primary_key=True)
prompt = Column(String, nullable=False)
llm = Column(String, nullable=False)
idx = Column(Integer)
response = Column(String)
prompt_tsv = Column(TSVectorType(), Computed("to_tsvector('english', llm || ' ' || prompt)", persisted=True))
__table_args__ = (
Index("idx_fulltext_prompt_tsv", prompt_tsv, postgresql_using="gin"),
)

engine = create_engine("postgresql://postgres:postgres@localhost:5432/postgres")
langchain.llm_cache = SQLAlchemyCache(engine, FulltextLLMCache)

Optional Caching

Neste exemplo, eles mostram como você pode desativar o cache para LLMs específicos

llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2, cache=False)
%%time
llm("Tell me a joke")
CPU times: user 5.8 ms, sys: 2.71 ms, total: 8.51 ms
Wall time: 745 ms
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'
%%time
llm("Tell me a joke")
CPU times: user 4.91 ms, sys: 2.64 ms, total: 7.55 ms
Wall time: 623 ms
'\n\nTwo guys stole a calendar. They got six months each.'

Optional Caching in Chains

Você também pode desativar o armazenamento em cache para determinados nós em cadeias. Observe que, devido a certas interfaces, geralmente é mais fácil construir a cadeia primeiro e editar o LLM posteriormente.

Como exemplo, carregaremos uma cadeia map-reduce do sumarizador. Armazenaremos os resultados em cache para a etapa do mapa, mas não os congelaremos para a etapa de combinação.

Ou seja, este exemplo é um pouco mais complexo. Ele mostra como desativar o cache para certos nós em cadeias. Eles usam um exemplo de uma cadeia de resumo do tipo map-reduce.

llm = OpenAI(model_name="text-davinci-002")
no_cache_llm = OpenAI(model_name="text-davinci-002", cache=False)
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.mapreduce import MapReduceChain

text_splitter = CharacterTextSplitter()
with open('../../../state_of_the_union.txt') as f:
state_of_the_union = f.read()
texts = text_splitter.split_text(state_of_the_union)
from langchain.docstore.document import Document
docs = [Document(page_content=t) for t in texts[:3]]
from langchain.chains.summarize import load_summarize_chain
chain = load_summarize_chain(llm, chain_type="map_reduce", reduce_llm=no_cache_llm)
%%time
chain.run(docs)
CPU times: user 452 ms, sys: 60.3 ms, total: 512 ms
Wall time: 5.09 s
'\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure. In response to Russian aggression in Ukraine, the United States is joining with European allies to impose sanctions and isolate Russia. American forces are being mobilized to protect NATO countries in the event that Putin decides to keep moving west. The Ukrainians are bravely fighting back, but the next few weeks will be hard for them. Putin will pay a high price for his actions in the long run. Americans should not be alarmed, as the United States is taking action to protect its interests and allies.'

Quando o executamos novamente, vemos que ele é substancialmente mais rápido, mas a resposta final é diferente. Isso ocorre devido ao armazenamento em cache nas etapas do mapa, mas não na etapa de redução.

%%time
chain.run(docs)
CPU times: user 11.5 ms, sys: 4.33 ms, total: 15.8 ms
Wall time: 1.04 s
'\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure.'
!rm .langchain.db sqlite.db

LangChain — Índice

Clique no link abaixo para acessar a página que serve como um índice abrangente, reunindo todos os links para os nossos tutoriais sobre LangChain.

--

--