From afdf692037fe71755ad71425cdcedf9c1b2bc76b Mon Sep 17 00:00:00 2001 From: JINO ROHIT Date: Thu, 12 Dec 2024 16:28:51 +0530 Subject: [PATCH] raises ValueError when num_label !=1 when using Crossencoder.rank() (#3126) * raises value error when num_label > 1 when using Crossencoder.rank() * updated error message --- sentence_transformers/cross_encoder/CrossEncoder.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sentence_transformers/cross_encoder/CrossEncoder.py b/sentence_transformers/cross_encoder/CrossEncoder.py index c4345017a..3d7ee0abb 100644 --- a/sentence_transformers/cross_encoder/CrossEncoder.py +++ b/sentence_transformers/cross_encoder/CrossEncoder.py @@ -527,6 +527,11 @@ def rank( 'score': -5.082967, 'text': "The 'Harry Potter' series, which consists of seven fantasy novels written by British author J.K. Rowling, is among the most popular and critically acclaimed books of the modern era."}] """ + if self.config.num_labels != 1: + raise ValueError( + "CrossEncoder.rank() only works for models with num_labels=1. " + "Consider using CrossEncoder.predict() with input pairs instead." + ) query_doc_pairs = [[query, doc] for doc in documents] scores = self.predict( sentences=query_doc_pairs,