Skip to main content

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

Project description

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.

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

vector_cache-1.1.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vector_cache-1.1.1-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file vector_cache-1.1.1.tar.gz.

File metadata

  • Download URL: vector_cache-1.1.1.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for vector_cache-1.1.1.tar.gz
Algorithm Hash digest
SHA256 c0375b9c1add51303e68139eb66f69fb5d39ed81e7a7c877eaa6299c70a5588e
MD5 edd3db88e54bf1fc8e433e483292f059
BLAKE2b-256 293c86366f384fde805916f1c089d28fae88c3f4ca766a86a8c8a517a122930b

See more details on using hashes here.

File details

Details for the file vector_cache-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: vector_cache-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for vector_cache-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 904b24dbd7af8748740b4e8a2535438606f99929983d7d0deed2ea7464173988
MD5 689049109344febd639d4cd9f1b116f0
BLAKE2b-256 d630b5496ce3488f17842566a1f758e3f2cede6514c9624fbf24719b754463e1

See more details on using hashes here.

Supported by

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