Class VCRSpringAIChatModel

java.lang.Object
com.redis.vl.test.vcr.VCRSpringAIChatModel
All Implemented Interfaces:
org.springframework.ai.chat.model.ChatModel, org.springframework.ai.chat.model.StreamingChatModel, org.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>, org.springframework.ai.model.StreamingModel<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>

public final class VCRSpringAIChatModel extends Object implements org.springframework.ai.chat.model.ChatModel
VCR wrapper for Spring AI ChatModel that records and replays LLM responses.

This class implements the ChatModel interface, allowing it to be used as a drop-in replacement for any Spring AI chat model. It provides VCR (Video Cassette Recorder) functionality to record LLM responses during test execution and replay them in subsequent runs.

Usage:


 ChatModel openAiModel = new OpenAiChatModel(openAiApi);

 VCRSpringAIChatModel vcrModel = new VCRSpringAIChatModel(openAiModel);
 vcrModel.setMode(VCRMode.PLAYBACK_OR_RECORD);
 vcrModel.setTestId("MyTest.testMethod");

 // Use exactly like the original model
 String response = vcrModel.call("Hello");
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    VCRSpringAIChatModel(org.springframework.ai.chat.model.ChatModel delegate)
    Creates a new VCRSpringAIChatModel wrapping the given delegate.
    VCRSpringAIChatModel(org.springframework.ai.chat.model.ChatModel delegate, VCRCassetteStore cassetteStore)
    Creates a new VCRSpringAIChatModel wrapping the given delegate with Redis storage.
  • Method Summary

    Modifier and Type
    Method
    Description
    call(String message)
     
    call(org.springframework.ai.chat.messages.Message... messages)
     
    org.springframework.ai.chat.model.ChatResponse
    call(org.springframework.ai.chat.prompt.Prompt prompt)
     
    int
    Gets the number of cache hits.
    int
    Gets the number of cache misses.
    org.springframework.ai.chat.model.ChatModel
    Gets the underlying delegate model.
    Gets the current VCR mode.
    int
    Gets the number of recorded responses.
    Gets the current test identifier.
    void
    preloadCassette(String key, String response)
    Preloads a cassette for testing purposes.
    void
    Resets the call counter.
    void
    Resets all statistics.
    void
    Sets the VCR mode.
    void
    setTestId(String testId)
    Sets the test identifier for cassette key generation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.ai.chat.model.ChatModel

    getDefaultOptions, stream

    Methods inherited from interface org.springframework.ai.chat.model.StreamingChatModel

    stream, stream
  • Constructor Details

    • VCRSpringAIChatModel

      public VCRSpringAIChatModel(org.springframework.ai.chat.model.ChatModel delegate)
      Creates a new VCRSpringAIChatModel wrapping the given delegate.
      Parameters:
      delegate - The actual ChatModel to wrap
    • VCRSpringAIChatModel

      public VCRSpringAIChatModel(org.springframework.ai.chat.model.ChatModel delegate, VCRCassetteStore cassetteStore)
      Creates a new VCRSpringAIChatModel wrapping the given delegate with Redis storage.
      Parameters:
      delegate - The actual ChatModel to wrap
      cassetteStore - The cassette store for persistence
  • Method Details

    • setMode

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

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

      public void setTestId(String testId)
      Sets the test identifier for cassette key generation.
      Parameters:
      testId - The test identifier (typically ClassName.methodName)
    • getTestId

      public String getTestId()
      Gets the current test identifier.
      Returns:
      The current test identifier
    • resetCallCounter

      public void resetCallCounter()
      Resets the call counter. Useful when starting a new test method.
    • getDelegate

      public org.springframework.ai.chat.model.ChatModel getDelegate()
      Gets the underlying delegate model.
      Returns:
      The wrapped ChatModel
    • preloadCassette

      public void preloadCassette(String key, String response)
      Preloads a cassette for testing purposes.
      Parameters:
      key - The cassette key
      response - The response text to cache
    • getCacheHits

      public int getCacheHits()
      Gets the number of cache hits.
      Returns:
      Cache hit count
    • getCacheMisses

      public int getCacheMisses()
      Gets the number of cache misses.
      Returns:
      Cache miss count
    • getRecordedCount

      public int getRecordedCount()
      Gets the number of recorded responses.
      Returns:
      Recorded count
    • resetStatistics

      public void resetStatistics()
      Resets all statistics.
    • call

      public org.springframework.ai.chat.model.ChatResponse call(org.springframework.ai.chat.prompt.Prompt prompt)
      Specified by:
      call in interface org.springframework.ai.chat.model.ChatModel
      Specified by:
      call in interface org.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>
    • call

      public String call(String message)
      Specified by:
      call in interface org.springframework.ai.chat.model.ChatModel
    • call

      public String call(org.springframework.ai.chat.messages.Message... messages)
      Specified by:
      call in interface org.springframework.ai.chat.model.ChatModel