mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-04 06:37:58 +00:00
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
from typing import Any, Dict, List, Tuple
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
|
|
from langchain_community.cross_encoders.base import BaseCrossEncoder
|
|
|
|
DEFAULT_MODEL_NAME = "BAAI/bge-reranker-base"
|
|
|
|
|
|
class HuggingFaceCrossEncoder(BaseModel, BaseCrossEncoder):
|
|
"""HuggingFace cross encoder models.
|
|
|
|
Example:
|
|
.. code-block:: python
|
|
|
|
from langchain_community.cross_encoders import HuggingFaceCrossEncoder
|
|
|
|
model_name = "BAAI/bge-reranker-base"
|
|
model_kwargs = {'device': 'cpu'}
|
|
hf = HuggingFaceCrossEncoder(
|
|
model_name=model_name,
|
|
model_kwargs=model_kwargs
|
|
)
|
|
"""
|
|
|
|
client: Any = None #: :meta private:
|
|
model_name: str = DEFAULT_MODEL_NAME
|
|
"""Model name to use."""
|
|
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
"""Keyword arguments to pass to the model."""
|
|
|
|
def __init__(self, **kwargs: Any):
|
|
"""Initialize the sentence_transformer."""
|
|
super().__init__(**kwargs)
|
|
try:
|
|
import sentence_transformers
|
|
|
|
except ImportError as exc:
|
|
raise ImportError(
|
|
"Could not import sentence_transformers python package. "
|
|
"Please install it with `pip install sentence-transformers`."
|
|
) from exc
|
|
|
|
self.client = sentence_transformers.CrossEncoder(
|
|
self.model_name, **self.model_kwargs
|
|
)
|
|
|
|
model_config = ConfigDict(extra="forbid", protected_namespaces=())
|
|
|
|
def score(self, text_pairs: List[Tuple[str, str]]) -> List[float]:
|
|
"""Compute similarity scores using a HuggingFace transformer model.
|
|
|
|
Args:
|
|
text_pairs: The list of text text_pairs to score the similarity.
|
|
|
|
Returns:
|
|
List of scores, one for each pair.
|
|
"""
|
|
scores = self.client.predict(text_pairs)
|
|
# Some models e.g bert-multilingual-passage-reranking-msmarco
|
|
# gives two score not_relevant and relevant as compare with the query.
|
|
if len(scores.shape) > 1: # we are going to get the relevant scores
|
|
scores = map(lambda x: x[1], scores)
|
|
return scores
|