Skip to main content

Replaying cache for LangChain

Project description

Replaying cache for LangChain

This is a replay cache implementation for LangChain. It is useful when you want to make the same LLM queries, and you do not want to get a single answer from the cache each time.

replay_cache wraps any cache from LangChain and remembers not only prompt & LLM configuration but also adds a sequential number to the cached value. So when it is used for the first time, it will only fill the cache and query the model each time.

When replay_cache is used again over the same cache, then it will return the results from the cache in the order how it was obtained in the previous run (on the same prompts over the same models). Each pair prompt & LLM configuration remembers its own order of responses. If a query is called more times than in the previous run, then a model is queried again and the result is stored in cached.

Installation

$ pip install replay_cache

Example

Without replay cache

cache = InMemoryCache()
set_llm_cache(cache)

llm.invoke("How are you?")  # Answer A (model queried)
llm.invoke("How are you?")  # Answer A (taken from cache)
llm.invoke("How are you?")  # Answer A (taken from cache)

With replay cache

from replay_cache import replay_cache

cache = InMemoryCache()

with replay_cache(cache):
    llm.invoke("How are you?")  # Answer A (model queried)
    llm.invoke("How are you?")  # Answer B (model queried)
    llm.invoke("How are you?")  # Answer C (model queried)

with replay_cache(cache):
    llm.invoke("How are you?")  # Answer A (taken from cache)
    llm.invoke("How are you?")  # Answer B (taken from cache)
    llm.invoke("How are you?")  # Answer C (taken from cache)
    llm.invoke("How are you?")  # Answer D (model queried)

with replay_cache(cache):
    llm.invoke("How are you?")  # Answer A (taken from cache)
    llm.invoke("How are you?")  # Answer B (taken from cache)
from replay_cache import replay_cache

cache = InMemoryCache()

with replay_cache(cache):
    llm.invoke("How are you?")        # Answer A (model queried)    
    llm.invoke("How are you?")        # Answer B (model queried)
    llm.invoke("What is your name?")  # Answer C (model queried)
    llm.invoke("What is your name?")  # Answer D (model queried)

with replay_cache(cache):
    llm.invoke("How are you?")        # Answer A (taken from cache)    
    llm.invoke("What is your name?")  # Answer C (taken from cache)
    llm.invoke("How are you?")        # Answer B (taken from cache)
    llm.invoke("What is your name?")  # Answer D (taken from cache)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

replay_cache-0.2.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

replay_cache-0.2.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file replay_cache-0.2.0.tar.gz.

File metadata

  • Download URL: replay_cache-0.2.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.5.0-21-generic

File hashes

Hashes for replay_cache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 37b1708307f4375ba1d37cbf332d550da2625e69adeae9ede796806ae5341216
MD5 1958bda98db0170ea3c7d5dbb3e2e778
BLAKE2b-256 510936eafd358b918d9c2b0f75aa84f4fe7f39e28f50ce6693e857b55f53350a

See more details on using hashes here.

File details

Details for the file replay_cache-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: replay_cache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.5.0-21-generic

File hashes

Hashes for replay_cache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea0f29d6fad10faa8ba34873769d40be7b90cc4fd8d2588038bc776935cc55f1
MD5 1e3fd0c2bf87e91276e364e0a3b2c08f
BLAKE2b-256 12adca6f625950265f44fcb13bc600d88ce5d5bcdd4713e49a03dfb5d4ff6e8b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page