LiteLLMReranker.rerank:v0
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
26
27
import weave
@weave.op()
def rerank(self, query, docs, top_n=None):
"""
Reranks the given documents based on their relevance to the query.
Args:
query (str): The query string.
docs (List[Dict[str, Any]]): A list of documents to be reranked.
top_n (int, optional): The number of top documents to return. Defaults to None.
Returns:
List[Dict[str, Any]]: A list of reranked documents with relevance scores.
"""
documents = [doc["text"] for doc in docs]
response = rerank(
model=self.model,
query=query,
documents=documents,
top_n=top_n or len(docs)
)
outputs = []
for doc in response.results:
reranked_doc = docs[doc["index"]]
reranked_doc["relevance_score"] = doc["relevance_score"]