Package com.redis.vl.test.vcr
Class VCREmbeddingInterceptor
java.lang.Object
com.redis.vl.test.vcr.VCREmbeddingInterceptor
Intercepts embedding calls for VCR recording and playback.
This interceptor captures embedding API calls during test recording and replays them during playback, enabling deterministic and fast tests without actual API calls.
Usage:
VCREmbeddingInterceptor interceptor = new VCREmbeddingInterceptor(cassetteStore);
interceptor.setMode(VCRMode.PLAYBACK_OR_RECORD);
interceptor.setTestId("MyTest.testMethod");
// In your test
float[] embedding = interceptor.embed("text to embed");
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new interceptor without Redis (for unit testing).VCREmbeddingInterceptor(VCRCassetteStore cassetteStore) Creates a new interceptor with Redis cassette storage. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract List<float[]> callRealBatchEmbedding(List<String> texts) Called to perform actual batch embedding call.protected abstract float[]callRealEmbedding(String text) Called to perform the actual embedding call.float[]Generates a single embedding, intercepting based on VCR mode.List<float[]> embedBatch(List<String> texts) Generates batch embeddings, intercepting based on VCR mode.longGets the cache hit count.longGets the cache miss count.getMode()Gets the current VCR mode.intGets the number of recorded cassettes.Gets the recorded cassette keys.voidpreloadBatchCassette(String key, float[][] embeddings) Preloads a batch cassette into the in-memory cache (for testing).voidpreloadCassette(String key, float[] embedding) Preloads a cassette into the in-memory cache (for testing).voidResets the call counter (call at start of each test).voidResets statistics.voidSets the VCR mode.voidsetModelName(String modelName) Sets the model name for cassette metadata.voidSets the current test identifier.
-
Constructor Details
-
VCREmbeddingInterceptor
protected VCREmbeddingInterceptor()Creates a new interceptor without Redis (for unit testing). -
VCREmbeddingInterceptor
Creates a new interceptor with Redis cassette storage.- Parameters:
cassetteStore- the cassette store
-
-
Method Details
-
setMode
Sets the VCR mode.- Parameters:
mode- the mode
-
getMode
Gets the current VCR mode.- Returns:
- the mode
-
setTestId
Sets the current test identifier.- Parameters:
testId- the test ID
-
setModelName
Sets the model name for cassette metadata.- Parameters:
modelName- the model name
-
embed
Generates a single embedding, intercepting based on VCR mode.- Parameters:
text- the text to embed- Returns:
- the embedding vector
-
embedBatch
Generates batch embeddings, intercepting based on VCR mode.- Parameters:
texts- the texts to embed- Returns:
- list of embedding vectors
-
preloadCassette
Preloads a cassette into the in-memory cache (for testing).- Parameters:
key- the cassette keyembedding- the embedding to cache
-
preloadBatchCassette
Preloads a batch cassette into the in-memory cache (for testing).- Parameters:
key- the cassette keyembeddings- the embeddings to cache
-
getRecordedCount
public int getRecordedCount()Gets the number of recorded cassettes.- Returns:
- the count
-
getRecordedKeys
Gets the recorded cassette keys.- Returns:
- list of keys
-
getCacheHits
public long getCacheHits()Gets the cache hit count.- Returns:
- number of cache hits
-
getCacheMisses
public long getCacheMisses()Gets the cache miss count.- Returns:
- number of cache misses
-
resetStatistics
public void resetStatistics()Resets statistics. -
resetCallCounter
public void resetCallCounter()Resets the call counter (call at start of each test). -
callRealEmbedding
Called to perform the actual embedding call. Subclasses must implement this.- Parameters:
text- the text to embed- Returns:
- the embedding vector
-
callRealBatchEmbedding
Called to perform actual batch embedding call. Subclasses must implement this.- Parameters:
texts- the texts to embed- Returns:
- list of embedding vectors
-