Class SemanticCacheAdvisor
- All Implemented Interfaces:
Advisor,BaseAdvisor,BaseChatMemoryAdvisor,CallAdvisor,MemoryAdvisor,StreamAdvisor,org.springframework.core.Ordered
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
Cached responses are isolated by the scope returned by the mandatory
SemanticCacheScopeResolver. Requests for which the resolver returns no scope
bypass the cache entirely.
Cached responses never expire unless a time-to-live is configured with
SemanticCacheAdvisor.Builder.ttl(Duration).
The refusal of a guardrail (see GuardrailRefusal) is returned and not cached.
- Author:
- Brian Sam-Bodden, Soby Chacko, Dimitar Proynov
-
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.ai.chat.client.advisor.api.MemoryAdvisor
CONVERSATION_IDFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsConstructorDescriptionSemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver) Creates a new semantic cache advisor with default order and scheduler.SemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver, int order) Creates a new semantic cache advisor with specified order and default scheduler.SemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver, int order, reactor.core.scheduler.Scheduler scheduler) Creates a new semantic cache advisor with specified order and scheduler.SemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver, int order, reactor.core.scheduler.Scheduler scheduler, @Nullable Duration ttl) Creates a new semantic cache advisor with specified order, scheduler and time-to-live. -
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.MemoryAdvisor
getConversationId
-
Constructor Details
-
SemanticCacheAdvisor
Creates a new semantic cache advisor with default order and scheduler.- Parameters:
cache- The semantic cache implementation to usescopeResolver- The resolver of the scope that isolates cached responses- Since:
- 2.0.2
-
SemanticCacheAdvisor
public SemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver, int order) Creates a new semantic cache advisor with specified order and default scheduler.- Parameters:
cache- The semantic cache implementation to usescopeResolver- The resolver of the scope that isolates cached responsesorder- The order of this advisor in the chain- Since:
- 2.0.2
-
SemanticCacheAdvisor
public SemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver, 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 usescopeResolver- The resolver of the scope that isolates cached responsesorder- The order of this advisor in the chainscheduler- The scheduler for async operations- Since:
- 2.0.2
-
SemanticCacheAdvisor
public SemanticCacheAdvisor(SemanticCache cache, SemanticCacheScopeResolver scopeResolver, int order, reactor.core.scheduler.Scheduler scheduler, @Nullable Duration ttl) Creates a new semantic cache advisor with specified order, scheduler and time-to-live.- Parameters:
cache- The semantic cache implementation to usescopeResolver- The resolver of the scope that isolates cached responsesorder- The order of this advisor in the chainscheduler- The scheduler for async operationsttl- The time-to-live of cached responses, or null for no expiration- Since:
- 2.0.2
-
-
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. If no scope is resolved for the request, the cache is bypassed.- 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 with true streaming - tokens are returned to the user as they arrive, while the response is aggregated and cached asynchronously when the stream completes. If no scope is resolved for the request, the cache is bypassed.- 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
-