CohereRanker
Rank documents based on their similarity to the query using Cohere models.
Basic Information
- Type:
haystack_integrations.components.rankers.cohere.ranker.CohereRanker - Components it can connect with:
Retriever:CohereRankerreceives documents fromRetriever.PromptBuilder:CohereRankercan send the ranked documents toPromptBuilderto be used in a prompt.- Any component that outputs documents or accepts documents as input.
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| query | str | The user query. | |
| documents | List[Document] | The documents to rank. | |
| top_k | Optional[int] | None | The maximum number of documents you want the Ranker to return. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| documents | List[Document] | List of Documents most similar to the given query in descending order of similarity. |
Overview
CohereRanker ranks documents based on their semantic similarity to the user query. For a list of supported ranking models, see Cohere documentation.
Documents are ordered from most to least semantically relevant to the query.
Authorization
You need a Cohere API key to use this component. Connect deepset to your Cohere account on the Integrations page.
Add Workspace-Level Integration
- Click your profile icon and choose Settings.
- Go to Workspace>Integrations.
- Find the provider you want to connect and click Connect next to them.
- Enter the API key and any other required details.
- Click Connect. You can use this integration in pipelines and indexes in the current workspace.
Add Organization-Level Integration
- Click your profile icon and choose Settings.
- Go to Organization>Integrations.
- Find the provider you want to connect and click Connect next to them.
- Enter the API key and any other required details.
- Click Connect. You can use this integration in pipelines and indexes in all workspaces in the current organization.
Usage Example
Initializing the Component
components:
CohereRanker:
type: haystack_integrations.components.rankers.cohere.ranker.CohereRanker
init_parameters:
Using the Component in a Pipeline
This ia an example of a document search pipeline where CohereRanker receives joined documents from both a keyword and a semantic retriever. It then ranks the documents based on their similarity to the user query and outputs them as the final result.
components:
bm25_retriever:
type: haystack_integrations.components.retrievers.opensearch.bm25_retriever.OpenSearchBM25Retriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
use_ssl: true
verify_certs: false
hosts:
- ${OPENSEARCH_HOST}
http_auth:
- ${OPENSEARCH_USER}
- ${OPENSEARCH_PASSWORD}
embedding_dim: 768
similarity: cosine
index: ''
max_chunk_bytes: 104857600
return_embedding: false
method:
mappings:
settings:
create_index: true
timeout:
top_k: 20
embedding_retriever:
type: haystack_integrations.components.retrievers.opensearch.embedding_retriever.OpenSearchEmbeddingRetriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
use_ssl: true
verify_certs: false
hosts:
- ${OPENSEARCH_HOST}
http_auth:
- ${OPENSEARCH_USER}
- ${OPENSEARCH_PASSWORD}
embedding_dim: 768
similarity: cosine
index: ''
max_chunk_bytes: 104857600
return_embedding: false
method:
mappings:
settings:
create_index: true
timeout:
top_k: 20
document_joiner:
type: haystack.components.joiners.document_joiner.DocumentJoiner
init_parameters:
join_mode: concatenate
DeepsetNvidiaTextEmbedder:
type: deepset_cloud_custom_nodes.embedders.nvidia.text_embedder.DeepsetNvidiaTextEmbedder
init_parameters:
model: intfloat/multilingual-e5-base
prefix: ''
suffix: ''
truncate:
normalize_embeddings: false
timeout:
backend_kwargs:
CohereRanker:
type: haystack_integrations.components.rankers.cohere.ranker.CohereRanker
init_parameters:
model: rerank-v3.5
top_k: 10
api_key:
type: env_var
env_vars:
- COHERE_API_KEY
- CO_API_KEY
strict: false
api_base_url: https://api.cohere.com
meta_fields_to_embed:
meta_data_separator: \n
max_tokens_per_doc: 4096
connections:
- sender: bm25_retriever.documents
receiver: document_joiner.documents
- sender: embedding_retriever.documents
receiver: document_joiner.documents
- sender: DeepsetNvidiaTextEmbedder.embedding
receiver: embedding_retriever.query_embedding
- sender: document_joiner.documents
receiver: CohereRanker.documents
max_runs_per_component: 100
metadata: {}
inputs:
query:
- bm25_retriever.query
- DeepsetNvidiaTextEmbedder.text
- CohereRanker.query
filters:
- bm25_retriever.filters
- embedding_retriever.filters
outputs:
documents: CohereRanker.documents
Parameters
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
| model | str | rerank-v3.5 | Cohere model name. Check the list of supported models in the Cohere documentation. |
| top_k | int | 10 | The maximum number of documents to return. |
| api_key | Secret | Secret.from_env_var(['COHERE_API_KEY', 'CO_API_KEY']) | Cohere API key. |
| api_base_url | str | https://api.cohere.com | The base URL of the Cohere API. |
| meta_fields_to_embed | Optional[List[str]] | None | List of meta fields that should be concatenated with the document content for reranking. |
| meta_data_separator | str | \n | Separator used to concatenate the meta fields to the Document content. |
| max_tokens_per_doc | int | 4096 | The maximum number of tokens to embed for each document defaults to 4096. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
| query | str | Query string. | |
| documents | List[Document] | List of documents to rank. | |
| top_k | Optional[int] | None | The maximum number of documents you want the Ranker to return. |
Was this page helpful?