Giter Site home page Giter Site logo

rajaniraiyn / semantic_cache Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 12 KB

Semantic caching for Python Functions

License: MIT License

Python 100.00%
caching decorators-python embeddings in-memory-caching llm python semantic-search verctor-retrieval

semantic_cache's Introduction

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.

semantic_cache's People

Contributors

rajaniraiyn avatar

Stargazers

Jenin Immanuel avatar Philip Nuzhnyi avatar  avatar Andre Zayarni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.