Skip to main content

VectorCache: A Python Library for Efficient LLM Query Caching

A streamlined Python library that enhances LLM query performance through semantic caching, making responses faster and more cost-effective.

Pypi Build

What is VectorCache?

As AI applications gain traction, the costs and latency of using large language models (LLMs) can escalate. VectorCache addresses these issues by caching LLM responses based on semantic similarity, thereby reducing both costs and response times.

🤠 Overview

Semantic Vector Cache leverages the power of LLMs to provide two main advantages:

  1. Faster Responses: By caching, it significantly reduces latency, offering quicker feedback to user queries.
  2. Lower Costs: It minimizes the number of direct LLM requests, thereby saving on usage costs.

🤔 Benefits

VectorCache, akin to a more nuanced Redis, enables efficient caching by recognizing not just exact matches but also semantically similar queries. This efficiency is particularly useful in domains where queries within a specific topic or field are frequent.

😊 Getting Started

Prerequisites:

  • Ensure Python version is 3.9 or higher (python --version).
  • pip install vector-cache

NOTE: vector-cache comes with support for multiple vector stores and storage databases, which are optional installs. Read here for more information

  • Install from source

    • For library installation issues, upgrade pip: python -m pip install -r requirements.txt.
  • Initialise vector cache:

Initialization Parameters for Vector Cache

The VectorCache class can be initialized with the following parameters:

  • embedding_model (BaseEmbedding): The embedding model used to convert text or other data into vector embeddings. This must be an instance of a class that inherits from BaseEmbedding.

    • db (CacheStorageInterface): The storage interface for caching and retrieving vectors. This must be an instance of a class that inherits from CacheStorageInterface.

    • vector_store (VectorStoreInterface): The vector store interface for storing and searching vectors. This must be an instance of a class that inherits from VectorStoreInterface.

    • initial_similarity_threshold (float, optional, default=0.99): The initial similarity threshold used to determine if a cached vector is considered a hit.

    • target_hit_rate (float, optional, default=0.8): The target hit rate for cache queries. This is used if use_adjustable_threshold is set to True to dynamically adjust the similarity threshold.

    • min_threshold (float, optional, default=0.7): The minimum allowed similarity threshold for adaptive thresholding.

    • max_threshold (float, optional, default=1.99): The maximum allowed similarity threshold for adaptive thresholding.

    • adjustment_rate (float, optional, default=0.01): The rate at which the similarity threshold is adjusted to achieve the target hit rate.

    • verbose (bool, optional, default=False): If set to True, enables verbose logging for debugging purposes.

    • use_adjustable_threshold (bool, optional, default=False): If set to True, enables dynamic adjustment of the similarity threshold based on the target hit rate.

Metrics for Adaptive Thresholding

  • total_queries (int): The total number of queries made to the vector cache. This is used internally to track performance metrics.

    • cache_hits (int): The number of cache hits. This is used internally to track performance metrics and adjust the similarity threshold if use_adjustable_threshold is enabled.

    Example

    vector_cache = VectorCache(
        embedding_model=my_embedding_model,
        db=my_cache_storage,
        vector_store=my_vector_store,
        initial_similarity_threshold=0.95,
        target_hit_rate=0.85,
        min_threshold=0.75,
        max_threshold=1.0,
        adjustment_rate=0.02,
        verbose=True,
        use_adjustable_threshold=True
    )
    
  • Refer to the examples folder for sample usage.

VectorCache is designed to work with any LLM provider. It includes modules for:

  • Embedding Models: Facilitates similarity searches through various vector_cache.embedding APIs.
  • Cache Storage: Stores LLM responses for future retrieval based on semantic matches.
  • Vector Store: Identifies similar requests using the input request's embeddings.
  • Cache Manager: Manages cache storage and vector store operations, including eviction policies.
  • Similarity Evaluator: Determines the similarity between requests to ascertain cache matches.

** Features: **

  • Adaptive Threshold (Alpha) : The vector cache can now incrementally adjust the similarity threshold till a desired hit rate is met. This should be used with caution as the high threshold can cause non-relevant matches to be returned.

🔍 Vector Stores

VectorCache supports multiple vector store options for efficient similarity search. For detailed information on how to use different vector stores, please refer to our Vector Stores README.

Supported vector stores include:

  • Redis
  • Qdrant
  • Deeplake
  • ChromaDB
  • pgvector
  • Pinecone
  • Milvus

We're continuously working on expanding our support for other popular vector stores. If you don't see your preferred vector store listed, check our documentation for the most up-to-date information or consider contributing to add support for it!

😆 Contributing

Interested in contributing to VectorCache? Check our contribution guidelines.

Metadata

Release files for vector-cache 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vector-cache 1.1.1
File Size Uploaded Provenance
vector_cache-1.1.1.tar.gz 16.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for vector-cache 1.1.1
File Interpreter ABI Platform
vector_cache-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.0 kB

Release files / vector_cache-1.1.1.tar.gz

Download URL vector_cache-1.1.1.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c0375b9c1add51303e68139eb66f69fb5d39ed81e7a7c877eaa6299c70a5588e
BLAKE2b-256 checksum
How to use checksums
293c86366f384fde805916f1c089d28fae88c3f4ca766a86a8c8a517a122930b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / vector_cache-1.1.1-py3-none-any.whl

Download URL vector_cache-1.1.1-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
904b24dbd7af8748740b4e8a2535438606f99929983d7d0deed2ea7464173988
BLAKE2b-256 checksum
How to use checksums
d630b5496ce3488f17842566a1f758e3f2cede6514c9624fbf24719b754463e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Security for vector-cache 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page