Download app/retrieval/reranker.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 851 Bytes
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/retrieval/reranker.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/retrieval/reranker.py
-
curl -L -o reranker.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/retrieval/reranker.py
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) | |