All Superinterfaces:
RuntimeType.Api<MockChatRuleConfig>

public interface MockChatRule extends 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

    Fields
    Modifier and Type
    Field
    Description
    static final MockChatRule
    Default rule that matches any request.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a new MockChatRuleConfig.Builder for building rule configurations.
    default String
    concatMessages(Collection<dev.langchain4j.data.message.ChatMessage> messages)
    Concatenates the textual content of the given ChatMessage collection into a single String.
    create(Config config)
    Creates a MockChatRule from the given configuration.
    Creates a MockChatRule from the given configuration.
    Creates a MockChatRule using a builder consumer to configure the rule.
    default dev.langchain4j.model.chat.response.ChatResponse
    doMock(dev.langchain4j.model.chat.request.ChatRequest req)
    Creates a mock ChatResponse for the given ChatRequest.
    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.
    boolean
    matches(dev.langchain4j.model.chat.request.ChatRequest req)
    Determines whether this rule matches the given chat request.
    default String
    mock(dev.langchain4j.model.chat.request.ChatRequest req)
    Produces a mock textual response for the given ChatRequest.
    default String
    mock(String concatenatedReq)
    Generates a mock response for a concatenated request string.
    Returns the prototype configuration for this rule.
  • Field Details

    • DEFAULT_RULE

      static final MockChatRule DEFAULT_RULE
      Default rule that matches any request.
  • Method Details

    • builder

      static MockChatRuleConfig.Builder builder()
      Returns a new MockChatRuleConfig.Builder for building rule configurations.
      Returns:
      a builder instance
    • create

      Creates a MockChatRule using a builder consumer to configure the rule.
      Parameters:
      consumer - a consumer that modifies the MockChatRuleConfig.Builder
      Returns:
      a new MockChatRule
    • create

      static MockChatRule create(Config config)
      Creates a MockChatRule from the given configuration.
      Parameters:
      config - the rule configuration
      Returns:
      a new MockChatRule
    • create

      static MockChatRule create(MockChatRuleConfig config)
      Creates a MockChatRule from 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:
      true if the rule matches, false otherwise
    • prototype

      default MockChatRuleConfig prototype()
      Returns the prototype configuration for this rule.

      Default implementation returns null and should be overridden by concrete rules.

      Specified by:
      prototype in interface RuntimeType.Api<MockChatRuleConfig>
      Returns:
      the MockChatRuleConfig prototype, or null if not applicable
    • doMock

      default dev.langchain4j.model.chat.response.ChatResponse doMock(dev.langchain4j.model.chat.request.ChatRequest req)
      Creates a mock ChatResponse for the given ChatRequest.
      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 ChatResponse using doMock(ChatRequest) and delivers it to the provided StreamingChatResponseHandler via StreamingChatResponseHandler.onCompleteResponse(ChatResponse).

      Parameters:
      chatRequest - the chat request to mock
      handler - the streaming response handler that receives the completed mock response
    • mock

      default String mock(dev.langchain4j.model.chat.request.ChatRequest req)
      Produces a mock textual response for the given ChatRequest.
      Parameters:
      req - the chat request
      Returns:
      the mocked response string
    • mock

      default String mock(String concatenatedReq)
      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

      default String concatMessages(Collection<dev.langchain4j.data.message.ChatMessage> messages)
      Concatenates the textual content of the given ChatMessage collection into a single String.

      By default, the method extracts text based on the message type: UserMessagesingleText(), AiMessagetext(), SystemMessagetext(). Any other types are ignored.

      Parameters:
      messages - the collection of ChatMessages to concatenate
      Returns:
      the concatenated message text