[docs]classVoyageAIRerank(BaseDocumentCompressor):"""Document compressor that uses `VoyageAI Rerank API`."""client:voyageai.Client=None# type: ignoreaclient:voyageai.AsyncClient=None# type: ignore"""VoyageAI clients to use for compressing documents."""voyage_api_key:Optional[SecretStr]=None"""VoyageAI API key. Must be specified directly or via environment variable VOYAGE_API_KEY."""model:str"""Model to use for reranking."""top_k:Optional[int]=None"""Number of documents to return."""truncation:bool=Truemodel_config=ConfigDict(arbitrary_types_allowed=True,)@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""Validate that api key exists in environment."""voyage_api_key=values.get("voyage_api_key")oros.getenv("VOYAGE_API_KEY",None)ifvoyage_api_key:api_key_secretstr=convert_to_secret_str(voyage_api_key)values["voyage_api_key"]=api_key_secretstrapi_key_str=api_key_secretstr.get_secret_value()else:api_key_str=Nonevalues["client"]=voyageai.Client(api_key=api_key_str)values["aclient"]=voyageai.AsyncClient(api_key=api_key_str)returnvaluesdef_rerank(self,documents:Sequence[Union[str,Document]],query:str,)->RerankingObject:"""Returns an ordered list of documents ordered by their relevance to the provided query. Args: query: The query to use for reranking. documents: A sequence of documents to rerank. """docs=[doc.page_contentifisinstance(doc,Document)elsedocfordocindocuments]returnself.client.rerank(query=query,documents=docs,model=self.model,top_k=self.top_k,truncation=self.truncation,)asyncdef_arerank(self,documents:Sequence[Union[str,Document]],query:str,)->RerankingObject:"""Returns an ordered list of documents ordered by their relevance to the provided query. Args: query: The query to use for reranking. documents: A sequence of documents to rerank. """docs=[doc.page_contentifisinstance(doc,Document)elsedocfordocindocuments]returnawaitself.aclient.rerank(query=query,documents=docs,model=self.model,top_k=self.top_k,truncation=self.truncation,)
[docs]defcompress_documents(self,documents:Sequence[Document],query:str,callbacks:Optional[Callbacks]=None,)->Sequence[Document]:""" Compress documents using VoyageAI's rerank API. Args: documents: A sequence of documents to compress. query: The query to use for compressing the documents. callbacks: Callbacks to run during the compression process. Returns: A sequence of compressed documents in relevance_score order. """iflen(documents)==0:return[]compressed=[]forresinself._rerank(documents,query).results:doc=documents[res.index]doc_copy=Document(doc.page_content,metadata=deepcopy(doc.metadata))doc_copy.metadata["relevance_score"]=res.relevance_scorecompressed.append(doc_copy)returncompressed
[docs]asyncdefacompress_documents(self,documents:Sequence[Document],query:str,callbacks:Optional[Callbacks]=None,)->Sequence[Document]:""" Compress documents using VoyageAI's rerank API. Args: documents: A sequence of documents to compress. query: The query to use for compressing the documents. callbacks: Callbacks to run during the compression process. Returns: A sequence of compressed documents in relevance_score order. """iflen(documents)==0:return[]compressed=[]forresin(awaitself._arerank(documents,query)).results:doc=documents[res.index]doc_copy=Document(doc.page_content,metadata=deepcopy(doc.metadata))doc_copy.metadata["relevance_score"]=res.relevance_scorecompressed.append(doc_copy)returncompressed