KmemoAdvisor
A caching org.springframework.ai.chat.client.advisor.api.Advisor for Spring AI's ChatClient, backed by a SemanticCache.
Spring AI has the advisor seam but ships no semantic cache, so this is the one-line win: register the advisor and repeated-in-meaning prompts are served from the cache — with kmemo's guards keeping the exchange rate for 250 USD from being served to someone who asked about 100.
val chatClient = ChatClient.builder(chatModel)
.defaultAdvisors(KmemoAdvisor(cache))
.build()The prompt's rendered text (org.springframework.ai.chat.prompt.Prompt.getContents) is the cache key. On a hit the advisor short-circuits the chain and returns the cached answer without calling the model; on a miss it calls the model through the chain, caches the assistant's reply text, and returns the model's real response untouched (metadata intact). Anything that changes what a correct answer looks like — the model, temperature, the system prompt — belongs in the scope, exactly as it does for SemanticCache.
Only the blocking call() path is cached. stream() calls pass straight through — a streamed response is not cached here — so the advisor is safe to register on a client used both ways.
The cache is a suspend API and the advisor SPI is blocking, so lookups run inside runBlocking on the calling request thread. That thread pays the embedding call on a miss; on a hit it pays one embedding and skips the model entirely, which is the whole point.
Parameters
the semantic cache to serve from.
the advisor's order in the chain; keep it early so a hit avoids the work behind it.
derives the cache scope from a request; constant SemanticCache.DEFAULT_SCOPE by default.
Constructors
Properties
Functions
Streaming is not cached; the request passes straight through the chain.