-
Checked other resources
Commit to Help
Example Coderetriever = qdrant.as_retriever(search_kwargs={"k": 5})
compressor = FlashrankRerank(model="ms-marco-MiniLM-L-12-v2")
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor, base_retriever=retriever
) DescriptionI am currently building a RAG, I would like to do reranking with FlashrankRerank. System Infopip freeze aiosignal==1.3.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To resolve the Here is how you can do it: FlashrankRerank.update_forward_refs() You should call this method after the class definition and before you instantiate or use the from __future__ import annotations
from typing import TYPE_CHECKING, Dict, Optional, Sequence
from langchain_core.callbacks.manager import Callbacks
from langchain_core.documents import BaseDocumentCompressor, Document
from langchain_core.pydantic_v1 import Extra, root_validator
if TYPE_CHECKING:
from flashrank import Ranker, RerankRequest
else:
# Avoid pydantic annotation issues when actually instantiating
# while keeping this import optional
try:
from flashrank import Ranker, RerankRequest
except ImportError:
pass
DEFAULT_MODEL_NAME = "ms-marco-MultiBERT-L-12"
class FlashrankRerank(BaseDocumentCompressor):
"""Document compressor using Flashrank interface."""
client: Ranker
"""Flashrank client to use for compressing documents"""
top_n: int = 3
"""Number of documents to return."""
model: Optional[str] = None
"""Model to use for reranking."""
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True
@root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
try:
from flashrank import Ranker
except ImportError:
raise ImportError(
"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"])
return values
def compress_documents(
self,
documents: Sequence[Document],
query: str,
callbacks: Optional[Callbacks] = None,
) -> Sequence[Document]:
passages = [
{"id": i, "text": doc.page_content, "meta": doc.metadata}
for i, doc in enumerate(documents)
]
rerank_request = RerankRequest(query=query, passages=passages)
rerank_response = self.client.rerank(rerank_request)[: self.top_n]
final_results = []
for r in rerank_response:
metadata = r["meta"]
metadata["relevance_score"] = r["score"]
doc = Document(
page_content=r["text"],
metadata=metadata,
)
final_results.append(doc)
return final_results
# Call update_forward_refs to resolve any forward references
FlashrankRerank.update_forward_refs() Additionally, ensure that you are importing from typing import TYPE_CHECKING, Any
from langchain._api import create_importer
if TYPE_CHECKING:
from langchain_community.document_compressors.flashrank_rerank import (
FlashrankRerank,
)
# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"FlashrankRerank": "langchain_community.document_compressors.flashrank_rerank"
}
_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)
__all__ = [
"FlashrankRerank",
] This should resolve the error related to the |
Beta Was this translation helpful? Give feedback.
To resolve the
FlashrankRerank
error: "field 'client' not yet prepared so type is still a ForwardRef, you might need to call FlashrankRerank.update_forward_refs()", you need to call theupdate_forward_refs
method on theFlashrankRerank
class. This method resolves any forward references in the type annotations.Here is how you can do it:
You should call this method after the class definition and before you instantiate or use the
FlashrankRerank
class. Here is an example of how to integrate it into your code: