Skip to main content

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: CohereRanker receives documents from Retriever.
    • PromptBuilder: CohereRanker can send the ranked documents to PromptBuilder to be used in a prompt.
    • Any component that outputs documents or accepts documents as input.

Inputs

ParameterTypeDefaultDescription
querystrThe user query.
documentsList[Document]The documents to rank.
top_kOptional[int]NoneThe maximum number of documents you want the Ranker to return.

Outputs

ParameterTypeDefaultDescription
documentsList[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

  1. Click your profile icon and choose Settings.
  2. Go to Workspace>Integrations.
  3. Find the provider you want to connect and click Connect next to them.
  4. Enter the API key and any other required details.
  5. Click Connect. You can use this integration in pipelines and indexes in the current workspace.

Add Organization-Level Integration

  1. Click your profile icon and choose Settings.
  2. Go to Organization>Integrations.
  3. Find the provider you want to connect and click Connect next to them.
  4. Enter the API key and any other required details.
  5. 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:

ParameterTypeDefaultDescription
modelstrrerank-v3.5Cohere model name. Check the list of supported models in the Cohere documentation.
top_kint10The maximum number of documents to return.
api_keySecretSecret.from_env_var(['COHERE_API_KEY', 'CO_API_KEY'])Cohere API key.
api_base_urlstrhttps://api.cohere.comThe base URL of the Cohere API.
meta_fields_to_embedOptional[List[str]]NoneList of meta fields that should be concatenated with the document content for reranking.
meta_data_separatorstr\nSeparator used to concatenate the meta fields to the Document content.
max_tokens_per_docint4096The 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.

ParameterTypeDefaultDescription
querystrQuery string.
documentsList[Document]List of documents to rank.
top_kOptional[int]NoneThe maximum number of documents you want the Ranker to return.