File size: 851 Bytes
481dfe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
from langchain_cohere import CohereRerank
from langchain_core.documents import Document
from app.config import settings


def get_reranker() -> CohereRerank:
    return CohereRerank(
        model=settings.reranker_model,
        cohere_api_key=settings.cohere_api_key,
        top_n=settings.rerank_top_n,
    )


def rerank_documents(query: str, documents: list[Document]) -> list[Document]:
    """Rerank retrieved documents using Cohere reranker."""
    if not documents:
        return []

    # Filter out documents with empty or whitespace-only content
    valid_docs = [
        doc for doc in documents
        if doc.page_content and doc.page_content.strip()
    ]

    if not valid_docs:
        return []

    reranker = get_reranker()
    return reranker.compress_documents(documents=valid_docs, query=query)