A highly flexible and agnostic semantic caching layer for LLM applications.
Project description
🧠 semantiker: Local LLM Semantic Caching Utility
Overview
semantiker is a lightweight Python utility designed to introduce a high-performance semantic caching layer between your application and a local Large Language Model (LLM) server. Its primary goal is to minimize redundant LLM calls by leveraging vector embeddings to check for semantically similar historical queries.
This utility is essential for developers building cost-effective, real-time applications using local inference, as it drastically reduces latency and computational load.
✨ Features: Problem Solved
The semantiker package addresses the core issue of costly and slow repetition inherent in working with LLMs by providing:
- Semantic Cache Hitting: Uses vector similarity checks (cosine similarity) to match new queries against the cache, ensuring relevant hits even when the input phrasing is different.
- Latency Reduction: By serving similar queries from the cache, response times drop from seconds (LLM inference) to milliseconds (vector lookup).
- Ollama Integration: Seamlessly works with the Ollama API, allowing for rapid deployment using popular local models like Mistral, Llama 2, or Code Llama.
- Configurable Threshold: Allows developers to fine-tune the strictness of the cache matching to balance performance gains against answer relevance.
⚙️ Prerequisites
To test and run the application, you must have a local LLM environment configured:
| Component | Requirement | Setup Command |
|---|---|---|
| Python | Version 3.8+ | N/A |
| Ollama | Must be installed and running locally. | [Download Ollama] |
| LLM Model | A model must be pulled and available in Ollama. | ollama pull mistral |
📦 Installation
To install the core package along with the dependencies required to run the included FastAPI test application:
# 1. Install the core library
pip install semantiker
# 2. Install dependencies for the FastAPI Test Application
pip install fastapi uvicorn pydantic
🛑 Current Limitations (V1.0)
While the core functionality is robust, the current version of semantiker maintains a focused scope.
- Client Dependency: Currently, only the Ollama API is supported via the
OllamaClient. - Vector Storage: The system relies exclusively on an in-memory FAISS index for vector storage, meaning the cache is reset every time the application restarts.
- Cache Type: Only supports in-memory dictionaries for storing the LLM responses.
🛠️ Architectural Design for Extensibility
The core strength of semantiker lies in its modular design, which is specifically built to overcome the V1.0 limitations easily. This clean separation of concerns provides immediate provisions for future support of external services and custom components:
- Decoupled Clients: The
LLMClient(for LLM communication) is separated from theSemanticCacheUtility(for caching logic). This provision allows new client classes (e.g.,OpenAIClient,GeminiClient) to be added by simply implementing the abstract client interface. - Pluggable Storage: The current in-memory FAISS and Python dictionaries can be easily swapped out for persistent solutions. Abstract storage interfaces can be introduced for vector indexes and key-value stores, allowing seamless integration with Chroma, Pinecone, or Redis.
- Custom Embeddings: The architecture is set up to allow the user to provide a custom Sentence Transformer model, decoupling the embedding generation from the LLM provider.
🚀 Testing the Application
To test the semantic caching in action, use the provided FastAPI example application:
-
Navigate to the test directory:
cd test-fast-api-app
-
Run the server (ensure Ollama is running first):
uvicorn main:app --reload
The API documentation will be available at
http://localhost:8000/docs. -
Test the API by sending two queries that are semantically similar but phrased differently (e.g., "best rain cloud" and "which cloud is best for rain") and observe the difference in latency to confirm the cache hit.
🔮 Future Extensions
| Improvement | Scope and How to Achieve It |
|---|---|
| Persistence Layer | Scope: Maintain cache integrity across application restarts and scale horizontally. How: Implement abstract CacheStorage interfaces and create concrete implementations for external storage like Redis (for key-value) and dedicated vector databases. |
| External API Clients | Scope: Enable seamless deployment to major cloud services. How: Create dedicated client classes (e.g., OpenAIClient, GeminiClient) implementing the unified AbstractLLMClient interface. |
| Custom Embedding Models | Scope: Decouple embedding generation from the Ollama LLM call for fine-grained control and performance. How: Integrate the Sentence Transformers library and allow configuration of local embedding models within the SemanticCacheUtility. |
| Time-to-Live (TTL) | Scope: Prevent the application from returning stale information. How: Implement cache expiration logic to check timestamps on cached entries and automatically force a cache miss if the entry is older than a configurable ttl_seconds. |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file semantiker-0.1.1.tar.gz.
File metadata
- Download URL: semantiker-0.1.1.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3d714e1734d685ee979aa9f6c5f44e207ca65100291ec3cfd6435e3ef9e53a0
|
|
| MD5 |
959eb63353f56466cb5afe10f6fcf211
|
|
| BLAKE2b-256 |
276953b4dc142fe192bedb1367f814802bad365957a7afe101dd896d6dafe07f
|
File details
Details for the file semantiker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: semantiker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f407e685ca0c5ada6f82d5f8a0f64e3cbc43a53452eeca6ff1269c120ecf9040
|
|
| MD5 |
bb066cd81a53d609b1fd5236bf395ccb
|
|
| BLAKE2b-256 |
9f9e60f80127a2f0545085b17c702cfca94dc4b10f64a16722e28bf58015d1ec
|