acpcchatbotadmin's picture
Upload 40 files
481dfe6 verified
Raw History Blame Contribute Delete
851 Bytes
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)