Skip to main content

An intelligent caching and LLM routing package using Mem0AI and zero-shot classification.

Project description

Smart Cache Package

Smart Cache Package is a pip-installable Python library that provides an intelligent caching and LLM routing solution. It efficiently reduces redundant LLM calls while ensuring that queries benefit from relevant past interactions.

This package leverages:

  • Mem0AI for storing and retrieving past interactions.
  • Hugging Face's zero-shot classification to automatically assign categories to queries.
  • Built-in LLM integrations for OpenAI (default), Anthropic (coming soon), and support for custom LLM callables.

🚀 Features

Smart Caching and Query Reuse

  • Caches previous query–answer pairs to avoid unnecessary LLM calls.
  • Uses Mem0AI to store user interactions for future reference.

🔍 Near-Duplicate Detection

  • Searches Mem0 for semantically similar queries.
  • If a near-duplicate query is found (above a configurable similarity threshold), it retrieves the stored response.

📄 Context Building for Richer Responses

  • If no exact or near-duplicate answer exists, it retrieves related stored interactions to provide context to the LLM.
  • Ensures the LLM receives additional context, improving coherence without exceeding token limits.

🔄 Built-in LLM Routing

  • Supports multiple LLM providers:
    • OpenAI (Default): Uses OPENAI_API_KEY from environment variables.
    • Custom LLM: Users can provide their own callable function.

🛠 Debugging and Transparency

  • get_answer() can return an extra debug flag indicating the source of the answer:
    • "Local Cache" → Retrieved from in-memory cache.
    • "Mem0 Near-Duplicate" → Retrieved from Mem0.
    • "LLM Call" → Fetched from an external LLM.

📥 Installation

pip install smart_cache_package

PREREQUISITES

  • Make sure you export OPENAI_API_KEY even if you use a custom LLM, it is required for storing the embeddings using the embedding model from openai.
  • Custom Embedding Model support coming soon.
export OPENAI_API_KEY = <your_openai_key>

🚀 Usage

1️⃣ Initialize SmartCache

The package automatically picks OpenAI as the default LLM if no custom callable is provided.

from smart_mem_cache import SmartCache

# Initialize the cache system (requires OPENAI_API_KEY)
smart_cache = SmartCache(
    similarity_threshold_reuse=0.75,
    similarity_threshold_context=0.4,
    max_context_tokens=256,
    ttl_seconds=60,  # 1-minute TTL
    debug=True,
    llm_name="openai"  # Use OpenAI by default
)

user_id = "alice"

Store Initial Interactions

Store a user’s conversation history for retrieval later.

smart_cache.store_interaction_auto_cat(
    user_id, 
    "Hi, I'm Alex. I like to play cricket on weekends.", 
    "Hello Alex! Great to know you enjoy cricket. I'll keep that in mind."
)

smart_cache.store_interaction_auto_cat(
    user_id, 
    "I also like to workout and cook on the weekends", 
    "That's wonderful to have hobbies."
)

print("Stored initial interactions.")

Ask a New Question

If an exact match or near-duplicate exists, it will reuse the previous answer. Otherwise, it calls the LLM.

new_question = "What does Alex do for fun on Saturday?"
answer, source = smart_cache.get_answer(user_id, new_question, return_debug=True)

print(f"Question: {new_question}")
print(f"Answer: {answer}")
print(f"Source: {source}")  # Could be "Local Cache", "Mem0 Near-Duplicate", or "LLM Call"

Ask an Exact Match Question

If a user repeats a previously asked query, it should return from cache.

exact_question = "Does Alex like to play cricket on weekends?"

# First call (expected LLM call)
answer1, source1 = smart_cache.get_answer(user_id, exact_question, return_debug=True)
print(f"First Call - Source: {source1}")

# Second call (should return from cache)
answer2, source2 = smart_cache.get_answer(user_id, exact_question, return_debug=True)
print(f"Second Call - Source: {source2}")  # Expected: "Local Cache"

Force Refresh to Bypass Cache

If you need to force a fresh answer from LLM, use force_refresh=True.

forced_question = "Any weekend hobbies for Alex?"
answer, source = smart_cache.get_answer(user_id, forced_question, force_refresh=True, return_debug=True)

print(f"Forced Refresh - Source: {source}")  # Expected: "LLM Call"

Provide Negative Feedback (Remove Cached Answer)

smart_cache.user_feedback(user_id, exact_question, helpful=False)
print("Negative feedback provided, answer removed from cache.")

# Asking again should now trigger a fresh LLM call.
answer, source = smart_cache.get_answer(user_id, exact_question, return_debug=True)
print(f"Re-asked after negative feedback - Source: {source}")  # Expected: "LLM Call"

Using a Custom LLM (Instead of OpenAI)

If you want to use a custom LLM (e.g., a self-hosted model), provide a callable function.

def custom_llm(prompt: str) -> str:
    return f"[Custom LLM response for: {prompt[:50]}...]"

# Initialize SmartCache with custom LLM
smart_cache = SmartCache(
    similarity_threshold_reuse=0.75,
    similarity_threshold_context=0.4,
    max_context_tokens=256,
    ttl_seconds=60,
    debug=True,
    llm_callable=custom_llm  # Pass custom LLM function
)

Environment Variables (Required for OpenAI & Anthropic)

If you use OpenAI or Anthropic, ensure the API keys are set as environment variables.

export OPENAI_API_KEY="your-openai-api-key"
export ANTHROPIC_API_KEY="your-anthropic-api-key"

🚀 Enjoy smart caching and optimized LLM interactions! 🚀

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

smart_cache_package-0.1.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

smart_cache_package-0.1.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file smart_cache_package-0.1.2.tar.gz.

File metadata

  • Download URL: smart_cache_package-0.1.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for smart_cache_package-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a72a5fad243bbfa209f0a13d8395c8ebd4badd4dc4f1280cf080ce3f9c93c1e4
MD5 376284afb44d13a9968ee419656f8b42
BLAKE2b-256 50505daddbc0e546590b3762fcc5607c5bbcf09861cacba48f6a9e52aa1b213f

See more details on using hashes here.

File details

Details for the file smart_cache_package-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for smart_cache_package-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b4d9dba30b0ec83961991c7613477e729e6a789bbeb0ccf1869d653d7344854
MD5 3cfc4299cd386cd9df2922c84136f870
BLAKE2b-256 a7645b044cfdace79bf943568ceadbab78ebf1859bf31a91b0d8cc4412969dc3

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