Skip to main content
Cohere

Search documentation

Type to search this documentation.

On this pageOverview

An Overview of Cohere's Rerank Model

The Rerank API endpoint, powered by the Rerank models, is a simple and very powerful tool for semantic search. Given a query and a list of documents, Rerank indexes the documents from most to least semantically relevant to the query.

In the example below, we use the Rerank API endpoint to index the list of documents from most to least relevant to the query What is the capital of the United States?.

Request

In this example, the documents being passed in are a list of strings:

PYTHON
import cohere

co = cohere.Client(api_key="<YOUR API KEY>")

query = "What is the capital of the United States?"
docs = [
    "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
    "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
    "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
    "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
    "Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",
]
results = co.rerank(
    model="rerank-v4.0-pro",
    query=query,
    documents=docs,
    top_n=5,
    return_documents=True,
)

Response

Python
id='ac062223-3b12-4e47-98ca-f1bba2544782'
results=[
    RerankResponseResultsItem(document=RerankResponseResultsItemDocument(
                                       text='Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.'),
                                       index=3,
                                       relevance_score=0.943264), 
    RerankResponseResultsItem(document=RerankResponseResultsItemDocument(
                                        text='Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.'),
                                        index=2,
                                        relevance_score=0.62209207),
    RerankResponseResultsItem(document=RerankResponseResultsItemDocument(
                                        text='The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.'),
                                        index=1,
                                        relevance_score=0.6054258),
    RerankResponseResultsItem(document=RerankResponseResultsItemDocument(
                                        text='Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.'),
                                        index=0,
                                        relevance_score=0.59040135),
    RerankResponseResultsItem(document=RerankResponseResultsItemDocument(
                                        text='Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.'),
                                        index=4,
                                        relevance_score=0.4664567)
    ] 
meta=ApiMeta(api_version=ApiMetaApiVersion(version='1',
                                           is_deprecated=None,
                                           is_experimental=None),
            billed_units=ApiMetaBilledUnits(images=None,
                                            input_tokens=None,
                                            output_tokens=None,
                                            search_units=1.0, classifications=None), 
            tokens=None,
            cached_tokens=None,
            warnings=None)

Alternatively, you can pass in a JSON object and specify the fields you'd like to rank over. If you do not pass in any rank_fields, it will default to the text key.

Request

PYTHON
query = "What is the capital of the United States?"
docs = [
    {
        "Title": "Facts about Carson City",
        "Content": "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
    },
    {
        "Title": "The Commonwealth of Northern Mariana Islands",
        "Content": "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
    },
    {
        "Title": "The Capital of United States Virgin Islands",
        "Content": "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
    },
    {
        "Title": "Washington D.C.",
        "Content": "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
    },
    {
        "Title": "Capital Punishment in the US",
        "Content": "Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",
    },
]
results = co.rerank(
    model="rerank-v4.0-pro",
    query=query,
    documents=docs,
    rank_fields=["Title", "Content"],
    top_n=5,
    return_documents=True,
)

In the docs parameter, we are passing in a list of objects which have the key values: ['Title' ,'Content']. As part of the Rerank call, we are specifying which keys to rank over, as well as the order in which the key value pairs should be considered.

PYTHON
id = "c2b7173a-3b6f-49b9-a87e-ef560ebdb81a"
results = [
    RerankResponseResultsItem(
        document=RerankResponseResultsItemDocument(
            text=None,
            Content="Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
            Title="Washington D.C.",
        ),
        index=3,
        relevance_score=0.9497813,
    ),
    RerankResponseResultsItem(
        document=RerankResponseResultsItemDocument(
            text=None,
            Content="Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
            Title="The Capital of United States Virgin Islands",
        ),
        index=2,
        relevance_score=0.69064254,
    ),
    RerankResponseResultsItem(
        document=RerankResponseResultsItemDocument(
            text=None,
            Content="Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
            Title="Facts about Carson City",
        ),
        index=0,
        relevance_score=0.57901955,
    ),
    RerankResponseResultsItem(
        document=RerankResponseResultsItemDocument(
            text=None,
            Content="The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
            Title="The Commonwealth of Northern Mariana Islands",
        ),
        index=1,
        relevance_score=0.5482865,
    ),
    RerankResponseResultsItem(
        document=RerankResponseResultsItemDocument(
            text=None,
            Content="Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",
            Title="Capital Punishment in the US",
        ),
        index=4,
        relevance_score=0.49375027,
    ),
]
meta = ApiMeta(
    api_version=ApiMetaApiVersion(
        version="1", is_deprecated=None, is_experimental=None
    ),
    billed_units=ApiMetaBilledUnits(
        images=None,
        input_tokens=None,
        output_tokens=None,
        search_units=1.0,
        classifications=None,
    ),
    tokens=None,
    cached_tokens=None,
    warnings=None,
)

Cohere's Rerank models have been trained for performance across 100+ languages.

When choosing the model, please note the following language support:

  • Rerank 4.0 (both 'fast' and 'pro'): A single multilingual model (rerank-v4.0-pro and rerank-v4.0-fast)
  • Rerank 3.5: A single multilingual model (rerank-v3.5)
  • Rerank 3.0: Separate English-only and multilingual models (rerank-english-v3.0 and rerank-multilingual-v3.0)

The following table provides the list of languages supported by the Rerank models. Please note that performance may vary across languages.

ISO Code Language Name
af Afrikaans
am Amharic
ar Arabic
as Assamese
az Azerbaijani
be Belarusian
bg Bulgarian
bn Bengali
bo Tibetan
bs Bosnian
ca Catalan
ceb Cebuano
co Corsican
cs Czech
cy Welsh
da Danish
de German
el Greek
en English
eo Esperanto
es Spanish
et Estonian
eu Basque
fa Persian
fi Finnish
fr French
fy Frisian
ga Irish
gd Scots_gaelic
gl Galician
gu Gujarati
ha Hausa
haw Hawaiian
he Hebrew
hi Hindi
hmn Hmong
hr Croatian
ht Haitian_creole
hu Hungarian
hy Armenian
id Indonesian
ig Igbo
is Icelandic
it Italian
ja Japanese
jv Javanese
ka Georgian
kk Kazakh
km Khmer
kn Kannada
ko Korean
ku Kurdish
ky Kyrgyz
La Latin
Lb Luxembourgish
Lo Laothian
Lt Lithuanian
Lv Latvian
mg Malagasy
mi Maori
mk Macedonian
ml Malayalam
mn Mongolian
mr Marathi
ms Malay
mt Maltese
my Burmese
ne Nepali
nl Dutch
no Norwegian
ny Nyanja
or Oriya
pa Punjabi
pl Polish
pt Portuguese
ro Romanian
ru Russian
rw Kinyarwanda
si Sinhalese
sk Slovak
sl Slovenian
sm Samoan
sn Shona
so Somali
sq Albanian
sr Serbian
st Sesotho
su Sundanese
sv Swedish
sw Swahili
ta Tamil
te Telugu
tg Tajik
th Thai
tk Turkmen
tl Tagalog
tr Turkish
tt Tatar
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zh Chinese
zu Zulu
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu