Class ChatRequestAssert

java.lang.Object
org.assertj.core.api.AbstractAssert<ChatRequestAssert, ChatRequest>
org.assertj.core.api.AbstractObjectAssert<ChatRequestAssert, ChatRequest>
dev.langchain4j.test.model.ChatRequestAssert
All Implemented Interfaces:
org.assertj.core.api.Assert<ChatRequestAssert, ChatRequest>, org.assertj.core.api.Descriptable<ChatRequestAssert>, org.assertj.core.api.ExtensionPoints<ChatRequestAssert, ChatRequest>

@Experimental public final class ChatRequestAssert extends org.assertj.core.api.AbstractObjectAssert<ChatRequestAssert, ChatRequest>
Assertions on a request sent to a chat model: its messages and the tools it offers. Use them to check what an AI Service sent to the model, for example that the system message and the prompt template were filled in as expected. To get the requests, register a ChatModelListener on the chat model that the AI Service uses:
import static dev.langchain4j.test.LangChain4jAssertions.assertThat;

List<ChatRequest> requests = new CopyOnWriteArrayList<>();
ChatModelListener requestCollector = new ChatModelListener() {
    @Override
    public void onRequest(ChatModelRequestContext requestContext) {
        requests.add(requestContext.chatRequest());
    }
};
// for example OpenAiChatModel.builder()...listeners(requestCollector).build()

assistant.chat("Cancel booking MS-777");

assertThat(requests.get(0))
        .hasSystemMessageContaining("support agent")
        .hasUserMessageContaining("MS-777")
        .hasToolSpecification("cancelBooking");
When a framework creates the chat model for you, register the listener the way that framework expects, for example as a bean. When an assertion fails, the message lists all messages or tools of the request; each message text is cut to 1000 characters.
  • Field Summary

    Fields inherited from class org.assertj.core.api.AbstractAssert

    actual, info, myself, objects, throwUnsupportedExceptionOnEquals
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates assertions on the given chat request.
  • Method Summary

    Modifier and Type
    Method
    Description
    Asserts that the last user message of the request contains the given text.
    Asserts that the request has a system message containing the given text.
    Asserts that the request offers the model the tool with the given name.
    Asserts that the request has a user message containing the given text.

    Methods inherited from class org.assertj.core.api.AbstractObjectAssert

    as, as, doesNotReturn, extracting, extracting, extracting, extracting, extracting, extracting, extractingForProxy, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, hasOnlyFields, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison

    Methods inherited from class org.assertj.core.api.AbstractAssert

    actual, areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, doesNotMatch, doesNotMatch, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, usingEquals, usingEquals, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.assertj.core.api.Descriptable

    as, describedAs, describedAs
  • Constructor Details

  • Method Details

    • hasSystemMessageContaining

      public ChatRequestAssert hasSystemMessageContaining(String text)
      Asserts that the request has a system message containing the given text.
    • hasUserMessageContaining

      public ChatRequestAssert hasUserMessageContaining(String text)
      Asserts that the request has a user message containing the given text. All user messages of the request are checked, including those from the chat memory, not only the last one: to check only the message of the current turn, use hasLastUserMessageContaining(String). The text of a user message includes what the AI Service added to it, for example the retrieved contents.
    • hasLastUserMessageContaining

      public ChatRequestAssert hasLastUserMessageContaining(String text)
      Asserts that the last user message of the request contains the given text. In the first request of an AI Service invocation, this is the message of the current turn: the messages from the chat memory come before it. The text of a user message includes what the AI Service added to it, for example the retrieved contents.
    • hasToolSpecification

      public ChatRequestAssert hasToolSpecification(String toolName)
      Asserts that the request offers the model the tool with the given name.