-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Providing unit tests for LiteLLMReranker class.
- Loading branch information
1 parent
2ecde29
commit 9046252
Showing
3 changed files
with
66 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/ragbits-document-search/tests/unit/test_rerankers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from pathlib import Path | ||
import pytest | ||
from ragbits.document_search.documents.document import DocumentMeta, DocumentType | ||
from ragbits.document_search.documents.element import Element, TextElement | ||
from ragbits.document_search.documents.sources import LocalFileSource | ||
from ragbits.document_search.retrieval.rerankers.litellm import LiteLLMReranker | ||
|
||
|
||
@pytest.fixture | ||
def mock_litellm_response(monkeypatch): | ||
class MockResponse: | ||
results = [{"index": 1}, {"index": 0}] | ||
|
||
def mock_rerank(*args, **kwargs): | ||
return MockResponse() | ||
|
||
monkeypatch.setattr("litellm.rerank", mock_rerank) | ||
|
||
|
||
@pytest.fixture | ||
def reranker(): | ||
return LiteLLMReranker( | ||
model="test_model", | ||
top_n=2, | ||
return_documents=True, | ||
rank_fields=["content"], | ||
max_chunks_per_doc=1, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mock_document_meta(): | ||
return DocumentMeta(document_type=DocumentType.TXT, source=LocalFileSource(path=Path("test.txt"))) | ||
|
||
|
||
@pytest.fixture | ||
def mock_custom_element(mock_document_meta): | ||
class CustomElement(Element): | ||
|
||
def get_key(self): | ||
return "test_key" | ||
|
||
return CustomElement(element_type="test_type", document_meta=mock_document_meta) | ||
|
||
|
||
def test_rerank_success(reranker, mock_litellm_response, mock_document_meta): | ||
chunks = [TextElement(content="chunk1", document_meta=mock_document_meta), TextElement(content="chunk2", document_meta=mock_document_meta)] | ||
query = "test query" | ||
|
||
reranked_chunks = reranker.rerank(chunks, query) | ||
|
||
assert reranked_chunks[0].content == "chunk2" | ||
assert reranked_chunks[1].content == "chunk1" | ||
|
||
|
||
def test_rerank_invalid_chunks(reranker, mock_custom_element): | ||
chunks = [mock_custom_element] | ||
query = "test query" | ||
|
||
with pytest.raises(ValueError, match="All chunks must be TextElement instances"): | ||
reranker.rerank(chunks, query) |