vector-stores/spring-ai-redis-semantic-cache/README.md
This module provides a Redis-based implementation of semantic caching for Spring AI.
Semantic caching allows storing and retrieving chat responses based on the semantic similarity of user queries. This implementation uses Redis vector search capabilities to efficiently find similar queries and return cached responses.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis-semantic-cache</artifactId>
</dependency>
For Spring Boot applications, you can use the starter:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-redis-semantic-cache</artifactId>
</dependency>
// Create Redis client
JedisPooled jedisClient = new JedisPooled("localhost", 6379);
// Create the embedding model
EmbeddingModel embeddingModel = new OpenAiEmbeddingModel(apiKey);
// Create the semantic cache
SemanticCache semanticCache = DefaultSemanticCache.builder()
.jedisClient(jedisClient)
.embeddingModel(embeddingModel)
.similarityThreshold(0.85) // Optional: adjust similarity threshold (0-1)
.build();
// Create the cache advisor
SemanticCacheAdvisor cacheAdvisor = SemanticCacheAdvisor.builder()
.cache(semanticCache)
.build();
// Use with ChatClient
ChatResponse response = ChatClient.builder(chatModel)
.build()
.prompt("What is the capital of France?")
.advisors(cacheAdvisor) // Add the advisor
.call()
.chatResponse();
You can also use the cache directly:
// Store a response
semanticCache.set("What is the capital of France?", parisResponse);
// Store with expiration
semanticCache.set("What's the weather today?", weatherResponse, Duration.ofHours(1));
// Retrieve a semantically similar response
Optional<ChatResponse> response = semanticCache.get("Tell me the capital city of France");
// Clear the cache
semanticCache.clear();
The DefaultSemanticCache can be configured with the following options:
jedisClient - The Redis clientvectorStore - Optional existing vector store to useembeddingModel - The embedding model for vector generationsimilarityThreshold - Threshold for determining similarity (0-1)indexName - The name of the Redis search indexprefix - Key prefix for Redis documentsWhen using Spring Boot and the Redis Semantic Cache starter, the components will be automatically configured.
You can customize behavior using properties in application.properties or application.yml:
spring:
ai:
vectorstore:
redis:
semantic-cache:
host: localhost
port: 6379
similarity-threshold: 0.85
index-name: semantic-cache