Skip to main content

Semantic caching wrapper for Redis — stop paying for duplicate LLM calls

Project description

semcache

Semantic caching for Redis. Stop paying for duplicate LLM calls.

The Problem

Traditional caching misses similar queries: "what is monthly revenue?" → cache miss ❌ "show me monthly revenue" → cache miss ❌ "monthly revenue figures?" → cache miss ❌

All three mean the same thing. You pay for 3 LLM calls instead of 1.

The Solution

semcache uses sentence embeddings to find semantically similar cached responses. Zero cost embeddings — runs locally.

Install

pip install semcache

Quick Start

from semcache import SemanticRedisCache

cache = SemanticRedisCache(threshold=0.85)

result = cache.get_or_set( query="what is monthly revenue?", func=your_llm_function, question="what is monthly revenue?" )

print(result.value) # LLM response print(result.hit) # True/False print(result.similarity) # 0.91 print(cache.metrics) # hit rate, counts

Results

→ 65-70% cache hit rate on real workloads → Zero cost embeddings (local model) → Works with any LLM → Drop-in Redis wrapper

Configuration

cache = SemanticRedisCache( host="localhost", # Redis host port=6379, # Redis port threshold=0.85, # similarity threshold ttl=86400, # cache TTL in seconds model_name="all-MiniLM-L6-v2", # embedding model namespace="semcache" # Redis key namespace )

How It Works

  1. Query arrives
  2. Convert to embedding (local, free)
  3. Search Redis for similar embeddings
  4. Similarity > threshold → return cached response
  5. Miss → call your LLM → store → return

Contributing

PRs welcome. See CONTRIBUTING.md.

License

MIT

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_redis_cache-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

semantic_redis_cache-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file semantic_redis_cache-0.1.0.tar.gz.

File metadata

  • Download URL: semantic_redis_cache-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for semantic_redis_cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 58e9688efed55dc6f88e122e873ee2a6164c22666e56be92d400f741b1a9ccaa
MD5 f003abaa6054a9cc8d4bc999099faa28
BLAKE2b-256 0960fa7503ebbb17791eb9897a4518031f79a88c47972a24d2f5c181c2e371c3

See more details on using hashes here.

File details

Details for the file semantic_redis_cache-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for semantic_redis_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bda3af794c1bb3e82e0c06999e47af37aaede3fa870c057cb661a854f2d3fd25
MD5 8e3fc47b523f49c535a4ca7419bca6a2
BLAKE2b-256 181b444a68f4b8a0dac3928f52f7bceea58181fadd34cab9181dc73c8cad87a6

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