Metadata-Version: 2.3
Name: langchain-reranker
Version: 0.1.0
Summary: Add init_reranker to the LangChain ecosystem
Keywords: langchain,reranker,rerank,rag,retrieval
Author: Ashitaka-Laputa-IV
Author-email: Ashitaka-Laputa-IV <Ashitaka-Laputa-IV@outlook.com>
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Dist: langchain-core>=1.0
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# langchain-reranker

Add `init_reranker` to the LangChain ecosystem, following the same conventions as
`init_chat_model` and `init_embeddings`.

## Installation

```bash
pip install langchain-reranker
```

Provider integration packages are optional and installed on demand, e.g.
`pip install langchain-cohere`.

## Usage

```python
from langchain_reranker.rerankers import init_reranker

reranker = init_reranker("cohere:rerank-v3.5")

compressed = reranker.compress_documents(documents, query="what is langchain?")
```

`init_reranker` returns a `BaseDocumentCompressor`, so it plugs directly into
existing retrieval pipelines such as a contextual compression retriever.

```python
reranker = init_reranker(model="jina-reranker-v2-base-multilingual", provider="jina")
```

## Custom rerankers

Implement `rerank` and inherit `compress_documents` for free.

```python
from langchain_reranker.rerankers import BaseReranker, RerankResult


class MyReranker(BaseReranker):
    model: str = "my-reranker"

    def rerank(self, query, documents, top_n=None):
        scores = my_api(query, [doc.page_content for doc in documents])
        ranked = sorted(zip(documents, scores), key=lambda pair: -pair[1])
        return [
            RerankResult(index=i, relevance_score=float(score), document=doc)
            for i, (doc, score) in enumerate(ranked)
        ]
```

## Local cross encoders

```python
reranker = init_reranker("huggingface:BAAI/bge-reranker-v2-m3")
```
