Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain/fix-cohere-reranker-rerank-method #19486

Merged
merged 9 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
jjovalle99 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ def rerank(
model = model or self.model
top_n = top_n if (top_n is None or top_n > 0) else self.top_n
results = self.client.rerank(
query, docs, model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc
query=query,
documents=docs,
model=model,
top_n=top_n,
max_chunks_per_doc=max_chunks_per_doc,
)
if hasattr(results, "results"):
results = getattr(results, "results")
result_dicts = []
for res in results:
result_dicts.append(
Expand Down
25 changes: 12 additions & 13 deletions libs/langchain/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jinja2 = {version = "^3", optional = true}
tiktoken = {version = ">=0.3.2,<0.6.0", optional = true, python=">=3.9"}
qdrant-client = {version = "^1.3.1", optional = true, python = ">=3.8.1,<3.12"}
dataclasses-json = ">= 0.5.7, < 0.7"
cohere = {version = "^4", optional = true}
jjovalle99 marked this conversation as resolved.
Show resolved Hide resolved
cohere = {version = ">=4,<6", optional = true}
openai = {version = "<2", optional = true}
nlpcloud = {version = "^1", optional = true}
huggingface_hub = {version = "^0", optional = true}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Test the cohere reranker."""
import os

from langchain.retrievers.document_compressors.cohere_rerank import CohereRerank

os.environ["COHERE_API_KEY"] = "foo"


def test_cohere_reranker_init() -> None:
"""Test the cohere reranker initializes correctly."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os

import pytest
from pytest_mock import MockerFixture

from langchain.retrievers.document_compressors import CohereRerank
from langchain.schema import Document

os.environ["COHERE_API_KEY"] = "foo"

Expand All @@ -14,3 +16,37 @@ def test_init() -> None:
CohereRerank(
top_n=5, model="rerank-english_v2.0", cohere_api_key="foo", user_agent="bar"
)


@pytest.mark.requires("cohere")
def test_rerank(mocker: MockerFixture) -> None:
mock_client = mocker.MagicMock()
mock_result = mocker.MagicMock()
mock_result.results = [
mocker.MagicMock(index=0, relevance_score=0.8),
mocker.MagicMock(index=1, relevance_score=0.6),
]
mock_client.rerank.return_value = mock_result

test_documents = [
Document(page_content="This is a test document."),
Document(page_content="Another test document."),
]
test_query = "Test query"

mocker.patch("cohere.Client", return_value=mock_client)

reranker = CohereRerank(cohere_api_key="foo")
results = reranker.rerank(test_documents, test_query)

mock_client.rerank.assert_called_once_with(
query=test_query,
documents=[doc.page_content for doc in test_documents],
model="rerank-english-v2.0",
top_n=3,
max_chunks_per_doc=None,
)
assert results == [
{"index": 0, "relevance_score": 0.8},
{"index": 1, "relevance_score": 0.6},
]