Class VCREmbeddingInterceptor

java.lang.Object
com.redis.vl.test.vcr.VCREmbeddingInterceptor

public abstract class VCREmbeddingInterceptor extends Object
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 Details

    • VCREmbeddingInterceptor

      protected VCREmbeddingInterceptor()
      Creates a new interceptor without Redis (for unit testing).
    • VCREmbeddingInterceptor

      public VCREmbeddingInterceptor(VCRCassetteStore cassetteStore)
      Creates a new interceptor with Redis cassette storage.
      Parameters:
      cassetteStore - the cassette store
  • Method Details

    • setMode

      public void setMode(VCRMode mode)
      Sets the VCR mode.
      Parameters:
      mode - the mode
    • getMode

      public VCRMode getMode()
      Gets the current VCR mode.
      Returns:
      the mode
    • setTestId

      public void setTestId(String testId)
      Sets the current test identifier.
      Parameters:
      testId - the test ID
    • setModelName

      public void setModelName(String modelName)
      Sets the model name for cassette metadata.
      Parameters:
      modelName - the model name
    • embed

      public float[] embed(String text)
      Generates a single embedding, intercepting based on VCR mode.
      Parameters:
      text - the text to embed
      Returns:
      the embedding vector
    • embedBatch

      public List<float[]> embedBatch(List<String> texts)
      Generates batch embeddings, intercepting based on VCR mode.
      Parameters:
      texts - the texts to embed
      Returns:
      list of embedding vectors
    • preloadCassette

      public void preloadCassette(String key, float[] embedding)
      Preloads a cassette into the in-memory cache (for testing).
      Parameters:
      key - the cassette key
      embedding - the embedding to cache
    • preloadBatchCassette

      public void preloadBatchCassette(String key, float[][] embeddings)
      Preloads a batch cassette into the in-memory cache (for testing).
      Parameters:
      key - the cassette key
      embeddings - the embeddings to cache
    • getRecordedCount

      public int getRecordedCount()
      Gets the number of recorded cassettes.
      Returns:
      the count
    • getRecordedKeys

      public List<String> 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

      protected abstract float[] callRealEmbedding(String text)
      Called to perform the actual embedding call. Subclasses must implement this.
      Parameters:
      text - the text to embed
      Returns:
      the embedding vector
    • callRealBatchEmbedding

      protected abstract List<float[]> callRealBatchEmbedding(List<String> texts)
      Called to perform actual batch embedding call. Subclasses must implement this.
      Parameters:
      texts - the texts to embed
      Returns:
      list of embedding vectors