Class SemanticCacheAdvisor
java.lang.Object
org.springframework.ai.chat.cache.semantic.SemanticCacheAdvisor
- All Implemented Interfaces:
Advisor,BaseAdvisor,BaseChatMemoryAdvisor,CallAdvisor,StreamAdvisor,org.springframework.core.Ordered
An advisor implementation that provides semantic caching capabilities for chat
responses. This advisor intercepts chat requests and checks for semantically similar
cached responses before allowing the request to proceed to the model.
This advisor implements BaseChatMemoryAdvisor but overrides both
adviseCall(org.springframework.ai.chat.client.ChatClientRequest, org.springframework.ai.chat.client.advisor.api.CallAdvisorChain) and adviseStream(org.springframework.ai.chat.client.ChatClientRequest, org.springframework.ai.chat.client.advisor.api.StreamAdvisorChain) to provide custom caching logic that
doesn't fit the standard before/after pattern.
Key features:
- Semantic similarity based caching of responses
- Support for both synchronous and streaming chat operations
- Configurable execution order in the advisor chain
- Author:
- Brian Sam-Bodden
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class for creating SemanticCacheAdvisor instances. -
Field Summary
Fields inherited from interface org.springframework.ai.chat.client.advisor.api.Advisor
DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDERFields inherited from interface org.springframework.ai.chat.client.advisor.api.BaseAdvisor
DEFAULT_SCHEDULERFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new semantic cache advisor with default order and scheduler.SemanticCacheAdvisor(SemanticCache cache, int order) Creates a new semantic cache advisor with specified order and default scheduler.SemanticCacheAdvisor(SemanticCache cache, int order, reactor.core.scheduler.Scheduler scheduler) Creates a new semantic cache advisor with specified order and scheduler. -
Method Summary
Modifier and TypeMethodDescriptionadviseCall(ChatClientRequest request, CallAdvisorChain chain) Handles synchronous chat requests by checking the cache before proceeding.reactor.core.publisher.Flux<ChatClientResponse> adviseStream(ChatClientRequest request, StreamAdvisorChain chain) Handles streaming chat requests by checking the cache before proceeding.after(ChatClientResponse response, AdvisorChain advisorChain) Not used for semantic cache advisor since we override adviseCall/adviseStream.before(ChatClientRequest request, AdvisorChain advisorChain) Not used for semantic cache advisor since we override adviseCall/adviseStream.static SemanticCacheAdvisor.Builderbuilder()Creates a new builder for constructing SemanticCacheAdvisor instances.intgetOrder()reactor.core.scheduler.SchedulerScheduler used for processing the advisor logic when streaming.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.chat.client.advisor.api.BaseAdvisor
getNameMethods inherited from interface org.springframework.ai.chat.client.advisor.api.BaseChatMemoryAdvisor
getConversationId
-
Constructor Details
-
SemanticCacheAdvisor
Creates a new semantic cache advisor with default order and scheduler.- Parameters:
cache- The semantic cache implementation to use
-
SemanticCacheAdvisor
Creates a new semantic cache advisor with specified order and default scheduler.- Parameters:
cache- The semantic cache implementation to useorder- The order of this advisor in the chain
-
SemanticCacheAdvisor
public SemanticCacheAdvisor(SemanticCache cache, int order, reactor.core.scheduler.Scheduler scheduler) Creates a new semantic cache advisor with specified order and scheduler.- Parameters:
cache- The semantic cache implementation to useorder- The order of this advisor in the chainscheduler- The scheduler for async operations
-
-
Method Details
-
getOrder
public int getOrder()- Specified by:
getOrderin interfaceorg.springframework.core.Ordered
-
getScheduler
public reactor.core.scheduler.Scheduler getScheduler()Description copied from interface:BaseAdvisorScheduler used for processing the advisor logic when streaming.- Specified by:
getSchedulerin interfaceBaseAdvisor
-
adviseCall
Handles synchronous chat requests by checking the cache before proceeding. If a semantically similar response is found in the cache, it is returned immediately. Otherwise, the request proceeds through the chain and the response is cached.- Specified by:
adviseCallin interfaceBaseAdvisor- Specified by:
adviseCallin interfaceCallAdvisor- Parameters:
request- The chat client request to processchain- The advisor chain to continue processing if needed- Returns:
- The response, either from cache or from the model
-
adviseStream
public reactor.core.publisher.Flux<ChatClientResponse> adviseStream(ChatClientRequest request, StreamAdvisorChain chain) Handles streaming chat requests by checking the cache before proceeding. If a semantically similar response is found in the cache, it is returned as a single item flux. Otherwise, the request proceeds through the chain and the final response is cached.- Specified by:
adviseStreamin interfaceBaseAdvisor- Specified by:
adviseStreamin interfaceStreamAdvisor- Parameters:
request- The chat client request to processchain- The advisor chain to continue processing if needed- Returns:
- A Flux of responses, either from cache or from the model
-
before
Not used for semantic cache advisor since we override adviseCall/adviseStream.- Specified by:
beforein interfaceBaseAdvisor
-
after
Not used for semantic cache advisor since we override adviseCall/adviseStream.- Specified by:
afterin interfaceBaseAdvisor
-
builder
Creates a new builder for constructing SemanticCacheAdvisor instances.- Returns:
- A new builder instance
-