Class WatsonxScoringModel
- All Implemented Interfaces:
ScoringModel
ScoringModel implementation that integrates IBM watsonx.ai with LangChain4j.
Example usage:
ScoringModel scoringModel = new WatsonxScoringModel.builder()
.baseUrl("https://...") // or use CloudRegion
.apiKey("...")
.projectId("...")
.modelId("cross-encoder/ms-marco-minilm-l-12-v2")
.build();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class for constructingWatsonxScoringModelinstances with configurable parameters. -
Method Summary
Modifier and TypeMethodDescriptionstatic WatsonxScoringModel.Builderbuilder()Returns a newWatsonxScoringModel.Builderinstance.The model's default per-call parameters, applied byScoringModel.score(ScoringRequest)andScoringModel.scoreAsync(ScoringRequest)to every request and overridden by any parameters set on the request itself.doScore(ScoringRequest scoringRequest) The provider hook behindScoringModel.score(ScoringRequest).The listeners notified before each call ofScoringModel.score(ScoringRequest)andScoringModel.scoreAsync(ScoringRequest), after each successful call and when a call fails.provider()TheModelProviderof this scoring model (for exampleModelProvider.COHERE).scoreAll(List<TextSegment> segments, String query) Scores all providedTextSegments against a given query.scoreAll(List<TextSegment> segments, String query, com.ibm.watsonx.ai.rerank.RerankParameters parameters) Scores all providedTextSegments against a given query using the givenRerankParameters.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ScoringModel
doScoreAsync, score, score, score, scoreAsync
-
Method Details
-
defaultRequestParameters
Description copied from interface:ScoringModelThe model's default per-call parameters, applied byScoringModel.score(ScoringRequest)andScoringModel.scoreAsync(ScoringRequest)to every request and overridden by any parameters set on the request itself. The default isScoringRequestParameters.EMPTY.- Specified by:
defaultRequestParametersin interfaceScoringModel
-
listeners
Description copied from interface:ScoringModelThe listeners notified before each call ofScoringModel.score(ScoringRequest)andScoringModel.scoreAsync(ScoringRequest), after each successful call and when a call fails. Listeners are configured on the builder of the scoring model. Defaults to an empty list.- Specified by:
listenersin interfaceScoringModel
-
provider
Description copied from interface:ScoringModelTheModelProviderof this scoring model (for exampleModelProvider.COHERE). It is made available to thelisteners. Defaults toModelProvider.OTHER.- Specified by:
providerin interfaceScoringModel
-
scoreAll
Description copied from interface:ScoringModelScores all providedTextSegments against a given query.- Specified by:
scoreAllin interfaceScoringModel- Parameters:
segments- The list ofTextSegments to score.query- The query against which to score the segments.- Returns:
- the list of scores. The order of scores corresponds to the order of
TextSegments.
-
doScore
Description copied from interface:ScoringModelThe provider hook behindScoringModel.score(ScoringRequest).The default scores the documents with
ScoringModel.scoreAll(List, String), so a model that implements onlyScoringModel.scoreAll(List, String)also supportsScoringModel.score(ScoringRequest). To do so, the default turns each document into aTextSegment, and aTextSegmentcannot be blank: a model that implements onlyScoringModel.scoreAll(List, String)therefore cannot score blank documents throughScoringModel.score(ScoringRequest), and such a request fails with anIllegalArgumentException.To support
listeners, override this method and implementScoringModel.scoreAll(List, String)by callingScoringModel.score(ScoringRequest), so that the listeners are also notified whenScoringModel.scoreAll(List, String)is called. Never makeScoringModel.scoreAll(List, String)callScoringModel.score(ScoringRequest)without overriding this method: each method would call the other.- Specified by:
doScorein interfaceScoringModel- Parameters:
scoringRequest- the request, with the default parameters already applied.- Returns:
- the scores, in the order of
ScoringRequest.documents(), and the response metadata.
-
scoreAll
public Response<List<Double>> scoreAll(List<TextSegment> segments, String query, com.ibm.watsonx.ai.rerank.RerankParameters parameters) Scores all providedTextSegments against a given query using the givenRerankParameters.Unlike
scoreAll(List, String), this method does not notify thelisteners.- Parameters:
segments- The list ofTextSegments to score.query- The query against which to score the segments.parameters- the rerank parameters to use.- Returns:
- the list of scores. The order of scores corresponds to the order of
TextSegments.
-
builder
Returns a newWatsonxScoringModel.Builderinstance.Example usage:
ScoringModel scoringModel = new WatsonxScoringModel.builder() .baseUrl("https://...") // or use CloudRegion .apiKey("...") .projectId("...") .modelId("cross-encoder/ms-marco-minilm-l-12-v2") .build();
-