Skip to main content

Semantic Caching on python functions

Project description

semantic-cache: Semantic Caching for Python Functions

This project provides a decorator for Python functions that implements semantic caching. This means that function calls with semantically similar inputs will return the cached result, even if the exact input values are different. This can be useful for optimizing expensive function calls, especially when dealing with natural language processing or other tasks where inputs can vary slightly but still have the same meaning.

How it works

The semantic_cache decorator uses the Qdrant vector similarity search engine to store and retrieve function call information. When a function is decorated, the following happens:

  1. Input encoding: The function arguments and keyword arguments are converted into a string representation.
  2. Similarity search: This string representation is used to query Qdrant for similar previously cached function calls.
  3. Cache hit: If a similar call is found with a similarity score above a threshold (default 0.95), the cached result is returned.
  4. Cache miss: If no similar call is found, the function is executed, its output is stored in the cache along with the input representation, and the output is returned.

Installation

pip install semantic-cache

Usage

from semantic_cache import semantic_cache

@semantic_cache()
def say(message: str) -> str:
    return f"Hi {message}"

# First call, function is executed
result1 = say("John")

# Second call with semantically similar input, cached result is used
result2 = say("john")

assert result1 == result2

Features

  • Similarity threshold: You can adjust the similarity threshold for determining cache hits.
  • In-memory caching: By default, an in-memory Qdrant instance is used for caching. You can configure a persistent Qdrant server for production use.
  • Automatic input encoding: The decorator automatically handles the conversion of function arguments into a searchable representation.

Potential Applications

  • Natural language processing: Cache results for functions that process text inputs, even if the wording is slightly different.
  • Machine learning model inference: Optimize inference calls by caching results for similar input data points.
  • Expensive calculations: Cache the results of computationally intensive functions based on input parameters.

Contributing

Contributions are welcome! Please see the CONTRIBUTING.md file for details.

License

This project is licensed under the MIT License.

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

semantic_cache-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

semantic_cache-0.1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file semantic_cache-0.1.1.tar.gz.

File metadata

  • Download URL: semantic_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for semantic_cache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 28c609c65725a2511a634d0f979c7ccb0c9d32eeecd8ec683b15344b5c821489
MD5 cd081314943e287a37241b3974156f67
BLAKE2b-256 1ce62ba51e45ae55b4a95435856fdec130f0a1fcf96922a4081510f8c3b88bfb

See more details on using hashes here.

File details

Details for the file semantic_cache-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: semantic_cache-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for semantic_cache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 204583d8ba2a821378a12a1f07d6de80f7db581b24b7d73bdfbb03a2b449d520
MD5 12f57584941bc81fa22f53f61f5e8b58
BLAKE2b-256 cf0e6b6dfd4e42ff3a3d80640aa19da65766b11143299b5a62b9a10e2a680285

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