cohere[patch]: Fix cohere rerank (#19624)

Fix cohere rerank inspired by
https://github.com/langchain-ai/langchain/pull/19486
pull/18393/head
billytrend-cohere 3 months ago committed by GitHub
parent 8ab7bb3166
commit 85f57ab4cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -69,10 +69,14 @@ class CohereRerank(BaseDocumentCompressor):
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,
)
result_dicts = []
for res in results:
for res in results.results:
result_dicts.append(
{"index": res.index, "relevance_score": res.relevance_score}
)

@ -0,0 +1,16 @@
"""Test Cohere reranks."""
from langchain_core.documents import Document
from langchain_cohere import CohereRerank
def test_langchain_cohere_rerank_documents() -> None:
"""Test cohere rerank."""
rerank = CohereRerank()
test_documents = [
Document(page_content="This is a test document."),
Document(page_content="Another test document."),
]
test_query = "Test query"
results = rerank.rerank(test_documents, test_query)
assert len(results) == 2
Loading…
Cancel
Save