BM25sRetriever.predict:v2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import weave
@weave.op()
def predict(self, query: str, top_k: int = 2):
"""
Predicts the top-k most relevant chunks for a given query using the BM25 algorithm.
This function is a wrapper around the `retrieve` method. It takes an input query string,
tokenizes it using the BM25 tokenizer, and retrieves the top-k most relevant chunks from
the BM25 index. The results are returned as a list of dictionaries, each containing a chunk
and its corresponding relevance score.
!!! example "Example Usage"
```python
import weave
from dotenv import load_dotenv
from medrag_multi_modal.retrieval.text_retrieval import BM25sRetriever
load_dotenv()
weave.init(project_name="ml-colabs/medrag-multi-modal")
retriever = BM25sRetriever()
retriever = BM25sRetriever().from_index(index_repo_id="geekyrakshit/grays-anatomy-index")
retrieved_chunks = retriever.predict(query="What are Ribosomes?")
```