import MomentoCachecache_name = "langchain"ttl = timedelta(days=1)set_llm_cache(MomentoCache.from_client_params(cache_name, ttl))# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 40.7 ms, sys: 16.5 ms, total: 57.2 ms    Wall time: 1.73 s    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'# The second time it is, so it goes faster# When run in the same region as the cache, latencies are single digit msllm("Tell me a joke")    CPU times: user 3.16 ms, sys: 2.98 ms, total: 6.14 ms    Wall time: 57.9 ms    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'SQLAlchemy Cache‚Äã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")# set_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, Sequencefrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy_utils import TSVectorTypefrom langchain.cache import SQLAlchemyCacheBase = 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",
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. ->: import MomentoCachecache_name = "langchain"ttl = timedelta(days=1)set_llm_cache(MomentoCache.from_client_params(cache_name, ttl))# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 40.7 ms, sys: 16.5 ms, total: 57.2 ms    Wall time: 1.73 s    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'# The second time it is, so it goes faster# When run in the same region as the cache, latencies are single digit msllm("Tell me a joke")    CPU times: user 3.16 ms, sys: 2.98 ms, total: 6.14 ms    Wall time: 57.9 ms    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'SQLAlchemy Cache‚Äã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")# set_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, Sequencefrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy_utils import TSVectorTypefrom langchain.cache import SQLAlchemyCacheBase = 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",