cohere_rerank

Sends document strings to the Cohere API, which returns them ranked by their relevance to a specified query.

The output of this processor is an array of strings, ordered by their relevance to the query.

Introduced in version 4.53.0.

# Configuration fields, showing default values
label: ""
cohere_rerank:
  base_url: https://api.cohere.com
  api_key: "" # No default (required)
  model: rerank-v3.5 # No default (required)
  query: "" # No default (required)
  documents: "" # No default (required)
  top_n: 0
  max_tokens_per_doc: 4096

Metadata

  • relevance_scores: An array of scores for each input document that indicates how relevant it is to the query. The scores are in the same order as the documents in the input. The higher the score, the more relevant the document.

Examples

Rerank some documents based on a query

Rerank some documents based on a query

input:
  generate:
    interval: 1s
    mapping: |
      root = {
        "query": fake("sentence"),
        "docs": [fake("paragraph"), fake("paragraph"), fake("paragraph")],
      }
pipeline:
  processors:
  - cohere_rerank:
      model: rerank-v3.5
      api_key: "${COHERE_API_KEY}"
      query: "${!this.query}"
      documents: "root = this.docs"
output:
  stdout: {}

Fields

api_key

Your API key for the Cohere API.

This field contains sensitive information that usually shouldn’t be added to a configuration directly. For more information, see Secrets.

Type: string

base_url

The base URL to use for API requests.

Type: string

documents

A list of text strings that are compared to the specified query. For optimal performance:

  • Send fewer than 1000 documents in a single request

  • Send structured data in YAML format

Type: string

max_tokens_per_doc

This processor automatically truncates long documents to the specified number of tokens.

Type: int

Default: 4096

model

The name of the Cohere LLM you want to use.

Type: string

# Examples:
model: rerank-v3.5

query

The search query you want to execute. This field supports interpolation functions.

Type: string

top_n

The number of documents to return when the query is executed. If set to 0, all documents are returned.

Type: int

Default: 0