Add semantic-cache module: CallAdvisor over a Redis Stack vector index with a real in-process embedding model; threshold sweep (hit rate against wrong answers), 600-request replay with an illustrative cost model, tenant filter, expiry, look-alike and embedding-model-change traps; optional real local model

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01G8ikz8xdWuTP5yun8DZ1hk
This commit is contained in:
2026-10-11 09:46:19 +00:00
co-authored by Claude Sonnet 5.5
parent cdee85d3f4
commit d51ef56468
24 changed files with 1383 additions and 0 deletions
@@ -0,0 +1,63 @@
package com.ankurm.semanticcache;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.metadata.ChatResponseMetadata;
import org.springframework.ai.chat.metadata.DefaultUsage;
import org.springframework.ai.chat.prompt.Prompt;
/**
* A model that knows the right answer for each dataset question and counts every call it receives. The answer
* for an intent is a fixed paragraph; what matters to a cache is whether the right paragraph comes back.
*/
public final class ScriptedModel implements ChatModel {
/** About 90 tokens by the usual four-characters-per-token rule. Stand-in text, same length for every intent. */
static final String BODY = " Please open the Help Centre, choose the matching topic and follow the steps shown there. "
+ "If the page does not match your situation, contact support with your order number and we will "
+ "pick it up from there within one business day. Keep your confirmation email, it speeds things up.";
private final Map<String, String> intentByText;
public final AtomicLong calls = new AtomicLong();
public final AtomicLong promptTokens = new AtomicLong();
public final AtomicLong completionTokens = new AtomicLong();
private final long delayMillis;
public ScriptedModel(Map<String, String> intentByText, long delayMillis) {
this.intentByText = intentByText;
this.delayMillis = delayMillis;
}
public static String answerFor(String intent) {
return "[" + intent + "]" + BODY;
}
public static int tokens(String s) { return Math.max(1, s.length() / 4); }
@Override
public ChatResponse call(Prompt prompt) {
calls.incrementAndGet();
String q = prompt.getUserMessage().getText();
String intent = intentByText.getOrDefault(q, "unknown");
String answer = answerFor(intent);
int in = tokens(prompt.getContents()), out = tokens(answer);
promptTokens.addAndGet(in);
completionTokens.addAndGet(out);
if (delayMillis > 0) {
try {
Thread.sleep(delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return ChatResponse.builder()
.generations(java.util.List.of(new Generation(new AssistantMessage(answer))))
.metadata(ChatResponseMetadata.builder().usage(new DefaultUsage(in, out)).build())
.build();
}
}