Interface MockChatRule
- All Superinterfaces:
RuntimeType.Api<MockChatRuleConfig>
Represents a rule for mocking chat interactions.
A MockChatRule determines whether it matches a given ChatRequest
and can generate a mock ChatResponse.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MockChatRuleDefault rule that matches any request. -
Method Summary
Modifier and TypeMethodDescriptionstatic MockChatRuleConfig.Builderbuilder()Returns a newMockChatRuleConfig.Builderfor building rule configurations.default StringconcatMessages(Collection<dev.langchain4j.data.message.ChatMessage> messages) Concatenates the textual content of the givenChatMessagecollection into a singleString.static MockChatRuleCreates aMockChatRulefrom the given configuration.static MockChatRulecreate(MockChatRuleConfig config) Creates aMockChatRulefrom the given configuration.static MockChatRulecreate(Consumer<MockChatRuleConfig.Builder> consumer) Creates aMockChatRuleusing a builder consumer to configure the rule.default dev.langchain4j.model.chat.response.ChatResponsedoMock(dev.langchain4j.model.chat.request.ChatRequest req) Creates a mockChatResponsefor the givenChatRequest.default voiddoMock(dev.langchain4j.model.chat.request.ChatRequest chatRequest, dev.langchain4j.model.chat.response.StreamingChatResponseHandler handler) Creates a mock streaming chat response for the given request.booleanmatches(dev.langchain4j.model.chat.request.ChatRequest req) Determines whether this rule matches the given chat request.default Stringmock(dev.langchain4j.model.chat.request.ChatRequest req) Produces a mock textual response for the givenChatRequest.default StringGenerates a mock response for a concatenated request string.default MockChatRuleConfigReturns the prototype configuration for this rule.
-
Field Details
-
DEFAULT_RULE
Default rule that matches any request.
-
-
Method Details
-
builder
Returns a newMockChatRuleConfig.Builderfor building rule configurations.- Returns:
- a builder instance
-
create
Creates aMockChatRuleusing a builder consumer to configure the rule.- Parameters:
consumer- a consumer that modifies theMockChatRuleConfig.Builder- Returns:
- a new
MockChatRule
-
create
Creates aMockChatRulefrom the given configuration.- Parameters:
config- the rule configuration- Returns:
- a new
MockChatRule
-
create
Creates aMockChatRulefrom the given configuration.- Parameters:
config- the rule configuration- Returns:
- a new
MockChatRule
-
matches
boolean matches(dev.langchain4j.model.chat.request.ChatRequest req) Determines whether this rule matches the given chat request.- Parameters:
req- the chat request to evaluate- Returns:
trueif the rule matches,falseotherwise
-
prototype
Returns the prototype configuration for this rule.Default implementation returns
nulland should be overridden by concrete rules.- Specified by:
prototypein interfaceRuntimeType.Api<MockChatRuleConfig>- Returns:
- the
MockChatRuleConfigprototype, ornullif not applicable
-
doMock
default dev.langchain4j.model.chat.response.ChatResponse doMock(dev.langchain4j.model.chat.request.ChatRequest req) Creates a mockChatResponsefor the givenChatRequest.- Parameters:
req- the chat request to mock- Returns:
- chat response
-
doMock
default void doMock(dev.langchain4j.model.chat.request.ChatRequest chatRequest, dev.langchain4j.model.chat.response.StreamingChatResponseHandler handler) Creates a mock streaming chat response for the given request.This default implementation generates a full mock
ChatResponseusingdoMock(ChatRequest)and delivers it to the providedStreamingChatResponseHandlerviaStreamingChatResponseHandler.onCompleteResponse(ChatResponse).- Parameters:
chatRequest- the chat request to mockhandler- the streaming response handler that receives the completed mock response
-
mock
Produces a mock textual response for the givenChatRequest.- Parameters:
req- the chat request- Returns:
- the mocked response string
-
mock
Generates a mock response for a concatenated request string.The default implementation returns a constant placeholder string.
- Parameters:
concatenatedReq- the concatenated request messages- Returns:
- a mock response string
-
concatMessages
Concatenates the textual content of the givenChatMessagecollection into a singleString.By default, the method extracts text based on the message type:
UserMessage→singleText(),AiMessage→text(),SystemMessage→text(). Any other types are ignored.- Parameters:
messages- the collection ofChatMessages to concatenate- Returns:
- the concatenated message text
-