Class VCRChatModel

java.lang.Object
com.redis.vl.test.vcr.VCRChatModel
All Implemented Interfaces:
dev.langchain4j.model.chat.ChatLanguageModel

public final class VCRChatModel extends Object implements dev.langchain4j.model.chat.ChatLanguageModel
VCR wrapper for LangChain4J ChatLanguageModel that records and replays LLM responses.

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

Usage:


 ChatLanguageModel openAiModel = OpenAiChatModel.builder()
     .apiKey(System.getenv("OPENAI_API_KEY"))
     .build();

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

 // Use exactly like the original model
 Response<AiMessage> response = vcrModel.generate(UserMessage.from("Hello"));
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    VCRChatModel(dev.langchain4j.model.chat.ChatLanguageModel delegate)
    Creates a new VCRChatModel wrapping the given delegate.
    VCRChatModel(dev.langchain4j.model.chat.ChatLanguageModel delegate, VCRCassetteStore cassetteStore)
    Creates a new VCRChatModel wrapping the given delegate with Redis storage.
  • Method Summary

    Modifier and Type
    Method
    Description
    generate(String userMessage)
     
    dev.langchain4j.model.output.Response<dev.langchain4j.data.message.AiMessage>
    generate(List<dev.langchain4j.data.message.ChatMessage> messages)
     
    int
    Gets the number of cache hits.
    int
    Gets the number of cache misses.
    dev.langchain4j.model.chat.ChatLanguageModel
    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 dev.langchain4j.model.chat.ChatLanguageModel

    chat, generate, generate, generate, supportedCapabilities
  • Constructor Details

    • VCRChatModel

      public VCRChatModel(dev.langchain4j.model.chat.ChatLanguageModel delegate)
      Creates a new VCRChatModel wrapping the given delegate.
      Parameters:
      delegate - The actual ChatLanguageModel to wrap
    • VCRChatModel

      public VCRChatModel(dev.langchain4j.model.chat.ChatLanguageModel delegate, VCRCassetteStore cassetteStore)
      Creates a new VCRChatModel wrapping the given delegate with Redis storage.
      Parameters:
      delegate - The actual ChatLanguageModel 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 dev.langchain4j.model.chat.ChatLanguageModel getDelegate()
      Gets the underlying delegate model.
      Returns:
      The wrapped ChatLanguageModel
    • 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.
    • generate

      public dev.langchain4j.model.output.Response<dev.langchain4j.data.message.AiMessage> generate(List<dev.langchain4j.data.message.ChatMessage> messages)
      Specified by:
      generate in interface dev.langchain4j.model.chat.ChatLanguageModel
    • generate

      public String generate(String userMessage)
      Specified by:
      generate in interface dev.langchain4j.model.chat.ChatLanguageModel