Package com.redis.vl.test.vcr
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
ConstructorsConstructorDescriptionVCRSpringAIChatModel(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 TypeMethodDescriptioncall(org.springframework.ai.chat.messages.Message... messages) org.springframework.ai.chat.model.ChatResponsecall(org.springframework.ai.chat.prompt.Prompt prompt) intGets the number of cache hits.intGets the number of cache misses.org.springframework.ai.chat.model.ChatModelGets 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 org.springframework.ai.chat.model.ChatModel
getDefaultOptions, streamMethods 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 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 org.springframework.ai.chat.model.ChatModel getDelegate()Gets the underlying delegate model.- Returns:
- The wrapped ChatModel
-
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. -
call
public org.springframework.ai.chat.model.ChatResponse call(org.springframework.ai.chat.prompt.Prompt prompt) - Specified by:
callin interfaceorg.springframework.ai.chat.model.ChatModel- Specified by:
callin interfaceorg.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>
-
call
- Specified by:
callin interfaceorg.springframework.ai.chat.model.ChatModel
-
call
- Specified by:
callin interfaceorg.springframework.ai.chat.model.ChatModel
-