Skip to content

Commit

Permalink
fix: Make TransformersSimilarityRanker run with single document list (
Browse files Browse the repository at this point in the history
#6503)

* Make `TransformersSimilarityRanker` run with single document list

* Add release note

* Remove unused import in test
  • Loading branch information
bogdankostic authored Dec 8, 2023
1 parent 2767cd2 commit 728383a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion haystack/components/rankers/transformers_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def run(self, query: str, documents: List[Document], top_k: Optional[int] = None
self.device
)
with torch.inference_mode():
similarity_scores = self.model(**features).logits.squeeze() # type: ignore
similarity_scores = self.model(**features).logits.squeeze(dim=1) # type: ignore

_, sorted_indices = torch.sort(similarity_scores, descending=True)
ranked_docs = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fixes:
- Make `TransformersSimilarityRanker` run with a list containing a single document as input.
13 changes: 13 additions & 0 deletions test/components/rankers/test_transformers_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,16 @@ def test_run_top_k(self, query, docs_before_texts, expected_first_text):

sorted_scores = sorted([doc.score for doc in docs_after], reverse=True)
assert [doc.score for doc in docs_after] == sorted_scores

@pytest.mark.integration
def test_run_single_document(self):
"""
Test if the component runs with a single document.
"""
ranker = TransformersSimilarityRanker(model_name_or_path="cross-encoder/ms-marco-MiniLM-L-6-v2")
ranker.warm_up()
docs_before = [Document(content="Berlin")]
output = ranker.run(query="City in Germany", documents=docs_before)
docs_after = output["documents"]

assert len(docs_after) == 1

0 comments on commit 728383a

Please sign in to comment.