Class MockScoringModel

java.lang.Object
dev.langchain4j.test.mock.MockScoringModel
All Implemented Interfaces:
ScoringModel

@Experimental public class MockScoringModel extends Object implements ScoringModel
A ScoringModel for tests: it needs no network, scores each document the way the test tells it to, and records the requests it receives.

Script the answers step by step, one step per request. One call of score(...), scoreAll(...) or scoreAsync(...) is one request, also when it scores several documents. For example, a model that gives every document of every request the same score:

MockScoringModel model = MockScoringModel.thatScores(0.8).thenRepeats();
Every that... method creates a mock with its first step, and every then... method adds a step. Each step answers exactly one request. A request after the last step fails with an IllegalStateException that shows the query of the request, so a test notices when the code calls the model more often than the script expects, unless the code under test handles the error itself. score(...) throws it, and scoreAsync(...) returns a future that fails with it. So a mock that is called more than once, for example to re-rank the results of every question of a conversation, ends its script with thenRepeats(), which answers every further request with the last step.

A script can let a request fail and the next one succeed, for example to test how your application handles a re-ranking model that times out once, with LangChain4j's TimeoutException, and then recovers:

MockScoringModel model = MockScoringModel.thatFails(new TimeoutException("Read timed out"))
        .thenScores(document -> document.contains("refund") ? 0.9 : 0.1)
        .thenRepeats();
Like on every mock in this package, thatResponds(...) and thenResponds(...) give a step as the whole response, here a ScoringResponse: the request is answered with exactly that response, including its metadata, for example the token usage that a cost check reads. The response is used as given, also when its number of scores differs from the number of documents of the request, for example to test how your code handles a re-ranking provider that returns too few scores.

Unless a step gives the whole response, a response reports the model name of the request. scoreAsync(...) completes on another thread, where a step that computes its scores also computes them.

A mock remembers which steps were taken and which requests it received, so create a new one for each test. A mock shared between tests, for example as a bean, fails with a clear message as soon as its script runs out, unless the script ends with thenRepeats(). The steps are taken in the order the requests arrive. A mock keeps every request it receives, so it is not meant for load tests or other long runs.