{
  "id": "reranker",
  "title": "Rerankers",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/0.8.2/api/reranker/",
  "summary": "",
  "content": "\n\n## CohereReranker\n\n\u003ca id=\"coherereranker-api\"\u003e\u003c/a\u003e\n\n### `class CohereReranker(model='rerank-english-v3.0', rank_by=None, limit=5, return_score=True, api_config=None)`\n\nBases: `BaseReranker`\n\nThe CohereReranker class uses Cohere’s API to rerank documents based on an\ninput query.\n\nThis reranker is designed to interact with Cohere’s /rerank API,\nrequiring an API key for authentication. The key can be provided\ndirectly in the api_config dictionary or through the COHERE_API_KEY\nenvironment variable. User must obtain an API key from Cohere’s website\n([https://dashboard.cohere.com/](https://dashboard.cohere.com/)). Additionally, the cohere python\nclient must be installed with pip install cohere.\n\n```python\nfrom redisvl.utils.rerank import CohereReranker\n\n# set up the Cohere reranker with some configuration\nreranker = CohereReranker(rank_by=[\"content\"], limit=2)\n# rerank raw search results based on user input/query\nresults = reranker.rank(\n    query=\"your input query text here\",\n    docs=[\n        {\"content\": \"document 1\"},\n        {\"content\": \"document 2\"},\n        {\"content\": \"document 3\"}\n    ]\n)\n```\n\nInitialize the CohereReranker with specified model, ranking criteria,\nand API configuration.\n\n* **Parameters:**\n  * **model** (*str*) – The identifier for the Cohere model used for reranking.\n    Defaults to ‘rerank-english-v3.0’.\n  * **rank_by** (*Optional* *[* *List* *[* *str* *]* *]*) – Optional list of keys specifying the\n    attributes in the documents that should be considered for\n    ranking. None means ranking will rely on the model’s default\n    behavior.\n  * **limit** (*int*) – The maximum number of results to return after\n    reranking. Must be a positive integer.\n  * **return_score** (*bool*) – Whether to return scores alongside the\n    reranked results.\n  * **api_config** (*Optional* *[* *Dict* *]* *,* *optional*) – Dictionary containing the API key.\n    Defaults to None.\n* **Raises:**\n  * **ImportError** – If the cohere library is not installed.\n  * **ValueError** – If the API key is not provided.\n\n#### `async arank(query, docs, **kwargs)`\n\nRerank documents based on the provided query using the Cohere rerank API.\n\nThis method processes the user’s query and the provided documents to\nrerank them in a manner that is potentially more relevant to the\nquery’s context.\n\n* **Parameters:**\n  * **query** (*str*) – The user’s search query.\n  * **docs** (*Union* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *List* *[* *str* *]* *]*) – The list of documents\n    to be ranked, either as dictionaries or strings.\n* **Returns:**\n  The reranked list of documents and optionally associated scores.\n* **Return type:**\n  Union[Tuple[Union[List[Dict[str, Any]], List[str]], float], List[Dict[str, Any]]]\n\n#### `model_post_init(context, /)`\n\nThis function is meant to behave like a BaseModel method to initialise private attributes.\n\nIt takes context as an argument since that’s what pydantic-core passes when calling it.\n\n* **Parameters:**\n  * **self** (*BaseModel*) – The BaseModel instance.\n  * **context** (*Any*) – The context.\n* **Return type:**\n  None\n\n#### `rank(query, docs, **kwargs)`\n\nRerank documents based on the provided query using the Cohere rerank API.\n\nThis method processes the user’s query and the provided documents to\nrerank them in a manner that is potentially more relevant to the\nquery’s context.\n\n* **Parameters:**\n  * **query** (*str*) – The user’s search query.\n  * **docs** (*Union* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *List* *[* *str* *]* *]*) – The list of documents\n    to be ranked, either as dictionaries or strings.\n* **Returns:**\n  The reranked list of documents and optionally associated scores.\n* **Return type:**\n  Union[Tuple[Union[List[Dict[str, Any]], List[str]], float], List[Dict[str, Any]]]\n\n#### `model_config: ClassVar[ConfigDict] = {}`\n\nConfiguration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].\n\n## HFCrossEncoderReranker\n\n\u003ca id=\"hfcrossencoderreranker-api\"\u003e\u003c/a\u003e\n\n### `class HFCrossEncoderReranker(model='cross-encoder/ms-marco-MiniLM-L-6-v2', limit=3, return_score=True, *, rank_by=None)`\n\nBases: `BaseReranker`\n\nThe HFCrossEncoderReranker class uses a cross-encoder models from Hugging Face\nto rerank documents based on an input query.\n\nThis reranker loads a cross-encoder model using the CrossEncoder class\nfrom the sentence_transformers library. It requires the\nsentence_transformers library to be installed.\n\n```python\nfrom redisvl.utils.rerank import HFCrossEncoderReranker\n\n# set up the HFCrossEncoderReranker with a specific model\nreranker = HFCrossEncoderReranker(model_name=\"cross-encoder/ms-marco-MiniLM-L-6-v2\", limit=3)\n# rerank raw search results based on user input/query\nresults = reranker.rank(\n    query=\"your input query text here\",\n    docs=[\n        {\"content\": \"document 1\"},\n        {\"content\": \"document 2\"},\n        {\"content\": \"document 3\"}\n    ]\n)\n```\n\nInitialize the HFCrossEncoderReranker with a specified model and ranking criteria.\n\n* **Parameters:**\n  * **model** (*str*) – The name or path of the cross-encoder model to use for reranking.\n    Defaults to ‘cross-encoder/ms-marco-MiniLM-L-6-v2’.\n  * **limit** (*int*) – The maximum number of results to return after reranking. Must be a positive integer.\n  * **return_score** (*bool*) – Whether to return scores alongside the reranked results.\n  * **rank_by** (*List* *[* *str* *]*  *|* *None*)\n\n#### `async arank(query, docs, **kwargs)`\n\nAsynchronously rerank documents based on the provided query using the loaded cross-encoder model.\n\nThis method processes the user’s query and the provided documents to rerank them\nin a manner that is potentially more relevant to the query’s context.\n\n* **Parameters:**\n  * **query** (*str*) – The user’s search query.\n  * **docs** (*Union* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *List* *[* *str* *]* *]*) – The list of documents to be ranked,\n    either as dictionaries or strings.\n* **Returns:**\n  The reranked list of documents and optionally associated scores.\n* **Return type:**\n  Union[Tuple[List[Dict[str, Any]], List[float]], List[Dict[str, Any]]]\n\n#### `model_post_init(context, /)`\n\nThis function is meant to behave like a BaseModel method to initialise private attributes.\n\nIt takes context as an argument since that’s what pydantic-core passes when calling it.\n\n* **Parameters:**\n  * **self** (*BaseModel*) – The BaseModel instance.\n  * **context** (*Any*) – The context.\n* **Return type:**\n  None\n\n#### `rank(query, docs, **kwargs)`\n\nRerank documents based on the provided query using the loaded cross-encoder model.\n\nThis method processes the user’s query and the provided documents to rerank them\nin a manner that is potentially more relevant to the query’s context.\n\n* **Parameters:**\n  * **query** (*str*) – The user’s search query.\n  * **docs** (*Union* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *List* *[* *str* *]* *]*) – The list of documents to be ranked,\n    either as dictionaries or strings.\n* **Returns:**\n  The reranked list of documents and optionally associated scores.\n* **Return type:**\n  Union[Tuple[List[Dict[str, Any]], List[float]], List[Dict[str, Any]]]\n\n#### `model_config: ClassVar[ConfigDict] = {}`\n\nConfiguration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].\n\n## VoyageAIReranker\n\n\u003ca id=\"voyageaireranker-api\"\u003e\u003c/a\u003e\n\n### `class VoyageAIReranker(model, rank_by=None, limit=5, return_score=True, api_config=None)`\n\nBases: `BaseReranker`\n\nThe VoyageAIReranker class uses VoyageAI’s API to rerank documents based on an\ninput query.\n\nThis reranker is designed to interact with VoyageAI’s /rerank API,\nrequiring an API key for authentication. The key can be provided\ndirectly in the api_config dictionary or through the VOYAGE_API_KEY\nenvironment variable. User must obtain an API key from VoyageAI’s website\n([https://dash.voyageai.com/](https://dash.voyageai.com/)). Additionally, the voyageai python\nclient must be installed with pip install voyageai.\n\n```python\nfrom redisvl.utils.rerank import VoyageAIReranker\n\n# set up the VoyageAI reranker with some configuration\nreranker = VoyageAIReranker(rank_by=[\"content\"], limit=2)\n# rerank raw search results based on user input/query\nresults = reranker.rank(\n    query=\"your input query text here\",\n    docs=[\n        {\"content\": \"document 1\"},\n        {\"content\": \"document 2\"},\n        {\"content\": \"document 3\"}\n    ]\n)\n```\n\nInitialize the VoyageAIReranker with specified model, ranking criteria,\nand API configuration.\n\n* **Parameters:**\n  * **model** (*str*) – The identifier for the VoyageAI model used for reranking.\n  * **rank_by** (*Optional* *[* *List* *[* *str* *]* *]*) – Optional list of keys specifying the\n    attributes in the documents that should be considered for\n    ranking. None means ranking will rely on the model’s default\n    behavior.\n  * **limit** (*int*) – The maximum number of results to return after\n    reranking. Must be a positive integer.\n  * **return_score** (*bool*) – Whether to return scores alongside the\n    reranked results.\n  * **api_config** (*Optional* *[* *Dict* *]* *,* *optional*) – Dictionary containing the API key.\n    Defaults to None.\n* **Raises:**\n  * **ImportError** – If the voyageai library is not installed.\n  * **ValueError** – If the API key is not provided.\n\n#### `async arank(query, docs, **kwargs)`\n\nRerank documents based on the provided query using the VoyageAI rerank API.\n\nThis method processes the user’s query and the provided documents to\nrerank them in a manner that is potentially more relevant to the\nquery’s context.\n\n* **Parameters:**\n  * **query** (*str*) – The user’s search query.\n  * **docs** (*Union* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *List* *[* *str* *]* *]*) – The list of documents\n    to be ranked, either as dictionaries or strings.\n* **Returns:**\n  The reranked list of documents and optionally associated scores.\n* **Return type:**\n  Union[Tuple[Union[List[Dict[str, Any]], List[str]], float], List[Dict[str, Any]]]\n\n#### `model_post_init(context, /)`\n\nThis function is meant to behave like a BaseModel method to initialise private attributes.\n\nIt takes context as an argument since that’s what pydantic-core passes when calling it.\n\n* **Parameters:**\n  * **self** (*BaseModel*) – The BaseModel instance.\n  * **context** (*Any*) – The context.\n* **Return type:**\n  None\n\n#### `rank(query, docs, **kwargs)`\n\nRerank documents based on the provided query using the VoyageAI rerank API.\n\nThis method processes the user’s query and the provided documents to\nrerank them in a manner that is potentially more relevant to the\nquery’s context.\n\n* **Parameters:**\n  * **query** (*str*) – The user’s search query.\n  * **docs** (*Union* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *List* *[* *str* *]* *]*) – The list of documents\n    to be ranked, either as dictionaries or strings.\n* **Returns:**\n  The reranked list of documents and optionally associated scores.\n* **Return type:**\n  Union[Tuple[Union[List[Dict[str, Any]], List[str]], float], List[Dict[str, Any]]]\n\n#### `model_config: ClassVar[ConfigDict] = {}`\n\nConfiguration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].\n",
  "tags": [],
  "last_updated": "2026-04-01T08:10:08-05:00"
}

