Skip to content

本页尚未翻译

本页内容仍为英文原文,源自官方文档 integrations/reranking/cohere。中文翻译正在分批进行中。

Cohere Reranker ​

Integrate Cohere's powerful reranking API with LanceDB for enhanced search results. Supports English and multilingual models with configurable scoring options for vector, FTS, and hybrid search.

Cohere Reranker ​

This reranker uses the Cohere API to rerank the search results. You can use this reranker by passing CohereReranker() to the rerank() method. Note that you'll either need to set the COHERE_API_KEY environment variable or pass the api_key argument to use this reranker.

Note: Supported query types: Hybrid, Vector, and FTS.

bash
pip install cohere

Replace the credential placeholders with your provider credentials. In application code, load credentials from environment variables or a secrets manager.

python
import tempfile
from pathlib import Path

import lancedb
from lancedb.embeddings import get_registry
from lancedb.pydantic import LanceModel, Vector
from lancedb.rerankers import CohereReranker

embedder = get_registry().get("sentence-transformers").create()
db = lancedb.connect(str(Path(tempfile.mkdtemp()) / "cohere-rerank"))

class Schema(LanceModel):
    text: str = embedder.SourceField()
    vector: Vector(embedder.ndims()) = embedder.VectorField()

data = [
    {"text": "hello world"},
    {"text": "goodbye world"},
]
tbl = db.create_table("test", schema=Schema)
tbl.add(data)
reranker = CohereReranker(api_key="your-api-key")

# Run vector search with a reranker
vector_results = tbl.search("hello").rerank(reranker=reranker).to_list()
print(vector_results)

# Build the full-text index before either FTS or hybrid search.
tbl.create_fts_index("text", replace=True)

# Run FTS search with a reranker
fts_results = (
    tbl.search("hello", query_type="fts").rerank(reranker=reranker).to_list()
)
print(fts_results)

# Run hybrid search with a reranker
hybrid_results = (
    tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
)
print(hybrid_results)

Accepted Arguments ​

ArgumentTypeDefaultDescription
model_namestr"rerank-english-v2.0"The name of the reranker model to use. Available cohere models are: rerank-english-v2.0, rerank-multilingual-v2.0
columnstr"text"The name of the column to use as input to the cross encoder model.
top_nstrNoneThe number of results to return. If None, will return all results.
api_keystrNoneThe API key for the Cohere API. If not provided, the COHERE_API_KEY environment variable is used.
return_scorestr"relevance"Options are "relevance" or "all". The type of score to return. If "relevance", will return only the _relevance_score column. If "all" is supported, will return relevance score along with the vector and/or fts scores depending on query type

Supported Scores for each query type ​

You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type:

return_scoreStatusDescription
relevance✅ SupportedResults only have the _relevance_score column
all❌ Not SupportedResults have vector(_distance) and FTS(score) along with Hybrid Search score(_relevance_score)
return_scoreStatusDescription
relevance✅ SupportedResults only have the _relevance_score column
all✅ SupportedResults have vector(_distance) along with Hybrid Search score(_relevance_score)
return_scoreStatusDescription
relevance✅ SupportedResults only have the _relevance_score column
all✅ SupportedResults have FTS(score) along with Hybrid Search score(_relevance_score)