Using SingleStore DB Semantic Cache in LangChain
Posted: Feb 20, 2025.
What is SingleStoreDBSemanticCache?
SingleStoreDBSemanticCache is a caching implementation in LangChain that uses SingleStore DB as a backend for semantically caching LLM responses. Unlike traditional exact-match caching, semantic caching allows you to retrieve cached results based on the semantic similarity between prompts, potentially reducing API calls for similar questions.
Reference
The SingleStoreDBSemanticCache class has the following key methods:
Method | Description |
---|---|
lookup(prompt, llm_string) | Looks up if there's a semantically similar cached response for the given prompt and LLM configuration |
update(prompt, llm_string, return_val) | Updates the cache with a new prompt-response pair |
clear(**kwargs) | Clears the cache |
alookup(prompt, llm_string) | Async version of lookup |
aupdate(prompt, llm_string, return_val) | Async version of update |
aclear(**kwargs) | Async version of clear |
How to use SingleStoreDBSemanticCache
Basic Configuration
Here's a basic example of how to set up SingleStoreDBSemanticCache:
This sets up the cache with basic configuration using the default settings.
Advanced Configuration
For more control over the cache behavior and connection settings:
This configuration includes:
- Custom table prefix for cache tables
- Adjusted similarity threshold for semantic matching
- Vector index enablement for better performance
- Connection pool settings
- Custom timeout values
- Specific distance strategy for vector similarity
Using with Vector Indexing
To enable vector indexing for better performance (requires SingleStoreDB 8.5+):
SSL Configuration
For secure connections, you can configure SSL settings:
The SingleStoreDBSemanticCache is particularly useful when you want to:
- Cache LLM responses in a robust, scalable database
- Implement semantic similarity search for cached responses
- Optimize response times by retrieving similar cached answers
- Set up a production-grade caching solution with proper connection pooling and SSL
Remember that the effectiveness of semantic caching depends on:
- The quality of the embedding model used
- The search_threshold setting (lower values allow more liberal matching)
- The vector indexing configuration for performance
- The connection pool settings for concurrent access
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.
LangChain DocsJoin 10,000+ subscribers
Every 2 weeks, latest model releases and industry news.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.