Source code for langchain_community.document_compressors.flashrank_rerank
from__future__importannotationsfromtypingimportTYPE_CHECKING,Any,Dict,Optional,Sequencefromlangchain_core.callbacks.managerimportCallbacksfromlangchain_core.documentsimportBaseDocumentCompressor,DocumentfrompydanticimportConfigDict,model_validatorifTYPE_CHECKING:fromflashrankimportRanker,RerankRequestelse:# Avoid pydantic annotation issues when actually instantiating# while keeping this import optionaltry:fromflashrankimportRanker,RerankRequestexceptImportError:passDEFAULT_MODEL_NAME="ms-marco-MultiBERT-L-12"
[docs]classFlashrankRerank(BaseDocumentCompressor):"""Document compressor using Flashrank interface."""client:Ranker"""Flashrank client to use for compressing documents"""top_n:int=3"""Number of documents to return."""score_threshold:float=0.0"""Minimum relevance threshold to return."""model:Optional[str]=None"""Model to use for reranking."""prefix_metadata:str="""""Prefix for flashrank_rerank metadata keys"""model_config=ConfigDict(arbitrary_types_allowed=True,extra="forbid",)@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""Validate that api key and python package exists in environment."""if"client"invalues:returnvalueselse:try:fromflashrankimportRankerexceptImportError:raiseImportError("Could not import flashrank python package. ""Please install it with `pip install flashrank`.")values["model"]=values.get("model",DEFAULT_MODEL_NAME)values["client"]=Ranker(model_name=values["model"])returnvalues