Package com.tnsai.llm.cache
Interface SemanticCache
- All Known Implementing Classes:
InMemorySemanticCache
public interface SemanticCache
Semantic cache for LLM responses based on prompt similarity.
SemanticCache stores LLM responses and retrieves them based on semantic similarity rather than exact string matching. This enables cache hits for semantically equivalent prompts like:
- "What is the capital of France?" ≈ "France's capital city?"
- "Explain Python" ≈ "What is Python programming?"
How It Works
- Prompt is converted to embedding vector
- Similar embeddings are searched in cache
- If similarity > threshold, cached response is returned
- Otherwise, LLM is called and response is cached
Usage Example
SemanticCache cache = InMemorySemanticCache.builder()
.embeddingProvider(new OllamaEmbeddingProvider())
.highThreshold(0.95) // Direct hit
.lowThreshold(0.70) // Skip cache
.ttlSeconds(3600) // 1 hour TTL
.maxEntries(10000)
.build();
// Check cache
Optional<CacheEntry> hit = cache.findSimilar("What is Python?", 0.90);
if (hit.isPresent()) {
return hit.get().response(); // Cache hit!
}
// Cache miss - call LLM and store
String response = llm.chat("What is Python?");
cache.put("What is Python?", response);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordCache statistics.static final recordResult of a similarity search. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all entries from the cache.findAllSimilar(String prompt, double minSimilarity, int maxResults) Finds all similar entries above the threshold.findSimilar(String prompt, double minSimilarity) Finds the most similar cached entry above the threshold.findSimilar(String prompt, Optional<String> systemPrompt, double minSimilarity) Finds the most similar cached entry with system prompt consideration.Gets an entry by exact prompt hash.getStats()Returns cache statistics.Stores a prompt-response pair in the cache.Stores a prompt-response pair with system prompt.booleanRemoves an entry from the cache.voidResets cache statistics.intsize()Returns the current number of entries.
-
Method Details
-
put
Stores a prompt-response pair in the cache.- Parameters:
prompt- The original promptresponse- The LLM response- Returns:
- The created cache entry
-
put
Stores a prompt-response pair with system prompt.- Parameters:
prompt- The user promptsystemPrompt- The system promptresponse- The LLM response- Returns:
- The created cache entry
-
findSimilar
Finds the most similar cached entry above the threshold.- Parameters:
prompt- The prompt to search forminSimilarity- Minimum similarity threshold (0-1)- Returns:
- The most similar entry if found, empty otherwise
-
findSimilar
Optional<CacheEntry> findSimilar(String prompt, Optional<String> systemPrompt, double minSimilarity) Finds the most similar cached entry with system prompt consideration.- Parameters:
prompt- The user promptsystemPrompt- The system promptminSimilarity- Minimum similarity threshold- Returns:
- The most similar entry if found
-
findAllSimilar
List<SemanticCache.SimilarityResult> findAllSimilar(String prompt, double minSimilarity, int maxResults) Finds all similar entries above the threshold.- Parameters:
prompt- The prompt to search forminSimilarity- Minimum similarity thresholdmaxResults- Maximum number of results- Returns:
- List of similar entries sorted by similarity (descending)
-
getByHash
Gets an entry by exact prompt hash.- Parameters:
promptHash- The prompt hash- Returns:
- The entry if found
-
remove
Removes an entry from the cache.- Parameters:
id- The entry ID- Returns:
- true if entry was removed
-
clear
void clear()Clears all entries from the cache. -
size
int size()Returns the current number of entries. -
getStats
SemanticCache.CacheStats getStats()Returns cache statistics. -
resetStats
void resetStats()Resets cache statistics.
-