Created
May 1, 2023 18:46
-
-
Save hwchase17/77ba7d4139d2ef6400991d7f10d2f894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CohereRerank wrapper to use as a document compressor | |
from typing import Any, Dict, Sequence | |
from pydantic import root_validator | |
from langchain.retrievers.document_compressors.base import BaseDocumentCompressor | |
from langchain.schema import Document | |
from langchain.utils import get_from_dict_or_env | |
class CohereRerank(BaseDocumentCompressor): | |
client: Any | |
top_n: int = 3 | |
model: str = "rerank-english-v2.0" | |
@root_validator() | |
def validate_environment(cls, values: Dict) -> Dict: | |
"""Validate that api key and python package exists in environment.""" | |
cohere_api_key = get_from_dict_or_env( | |
values, "cohere_api_key", "COHERE_API_KEY" | |
) | |
try: | |
import cohere | |
values["client"] = cohere.Client(cohere_api_key) | |
except ImportError: | |
raise ValueError( | |
"Could not import cohere python package. " | |
"Please install it with `pip install cohere`." | |
) | |
return values | |
def compress_documents( | |
self, documents: Sequence[Document], query: str | |
) -> Sequence[Document]: | |
doc_list = list(documents) | |
_docs = [d.page_content for d in doc_list] | |
results = self.client.rerank( | |
model=self.model, query=query, documents=_docs, top_n=self.top_n | |
) | |
final_results = [] | |
for r in results: | |
doc = doc_list[r.index] | |
doc.metadata["relevance_score"] = r.relevance_score | |
final_results.append(doc) | |
return final_results | |
async def acompress_documents( | |
self, documents: Sequence[Document], query: str | |
) -> Sequence[Document]: | |
raise NotImplementedError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cnevelle-blueprint This gist is out of date. See: langchain-ai/langchain@84cfa76
Hopefully this error will be solved for you now because of the Pydantic logic update.