Package com.redis.vl.test.vcr
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
ConstructorsConstructorDescriptionVCRChatModel(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 TypeMethodDescriptiondev.langchain4j.model.output.Response<dev.langchain4j.data.message.AiMessage> intGets the number of cache hits.intGets the number of cache misses.dev.langchain4j.model.chat.ChatLanguageModelGets the underlying delegate model.getMode()Gets the current VCR mode.intGets the number of recorded responses.Gets the current test identifier.voidpreloadCassette(String key, String response) Preloads a cassette for testing purposes.voidResets the call counter.voidResets all statistics.voidSets the VCR mode.voidSets the test identifier for cassette key generation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 wrapcassetteStore- The cassette store for persistence
-
-
Method Details
-
setMode
Sets the VCR mode.- Parameters:
mode- The VCR mode to use
-
getMode
Gets the current VCR mode.- Returns:
- The current VCR mode
-
setTestId
Sets the test identifier for cassette key generation.- Parameters:
testId- The test identifier (typically ClassName.methodName)
-
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
Preloads a cassette for testing purposes.- Parameters:
key- The cassette keyresponse- 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:
generatein interfacedev.langchain4j.model.chat.ChatLanguageModel
-
generate
- Specified by:
generatein interfacedev.langchain4j.model.chat.ChatLanguageModel
-