How Rerank Works

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.

Get Started

Example with Texts

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
1import cohere
2co = cohere.ClientV2()
3
4query = "What is the capital of the United States?"
5docs = [
6 "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.",
7 "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.",
8 "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.",
9 "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.",
10 "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."
11]
12
13results = co.rerank(
14 model="rerank-v3.5",
15 query=query,
16 documents=docs,
17 top_n=5)

Response

1{
2 "id": "97813271-fe74-465d-b9d5-577e77079253",
3 "results": [
4 {
5 "index": 3, // "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) ..."
6 "relevance_score": 0.9990564
7 },
8 {
9 "index": 4, // "Capital punishment has existed in the United States since before the United States was a country. As of 2017 ..."
10 "relevance_score": 0.7516481
11 },
12 {
13 "index": 1, // "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division ..."
14 "relevance_score": 0.08882029
15 },
16 {
17 "index": 0, // "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a ..."
18 "relevance_score": 0.058238626
19 },
20 {
21 "index": 2, // ""Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people ..."
22 "relevance_score": 0.019946935
23 }
24 ],
25 "meta": {
26 "api_version": {
27 "version": "2"
28 },
29 "billed_units": {
30 "search_units": 1
31 }
32 }
33}

Example with Structured Data:

If your documents contain structured data, for best performance we recommend formatting them as YAML strings.

Request

PYTHON
1import yaml
2import cohere
3
4co = cohere.ClientV2()
5
6query = "What is the capital of the United States?"
7docs = [
8 {
9 "Title": "Facts about Carson City",
10 "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."
11 },
12 {
13 "Title": "The Commonwealth of Northern Mariana Islands",
14 "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."
15 },
16 {
17 "Title": "The Capital of United States Virgin Islands",
18 "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."
19 },
20 {
21 "Title": "Washington D.C.",
22 "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."
23 },
24 {
25 "Title": "Capital Punishment in the US",
26 "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."
27 }
28]
29
30yaml_docs = [yaml.dump(doc, sort_keys=False) for doc in docs]
31
32results = co.rerank(
33 model="rerank-v3.5",
34 query=query,
35 documents=yaml_docs,
36 top_n=5
37)

In the documents parameter, we are passing in a list YAML strings, representing the structured data.

Response

1{
2 "id": "75a94aa7-6761-4a64-a2ae-4bc0a62bc601",
3 "results": [
4 {
5 "index": 3,
6 "relevance_score": 0.9987405
7 },
8 {
9 "index": 4,
10 "relevance_score": 0.5011778
11 },
12 {
13 "index": 2,
14 "relevance_score": 0.10070161
15 },
16 {
17 "index": 1,
18 "relevance_score": 0.03197956
19 },
20 {
21 "index": 0,
22 "relevance_score": 0.019456575
23 }
24 ],
25 "meta": {
26 "api_version": {
27 "version": "2022-12-06"
28 },
29 "billed_units": {
30 "search_units": 1
31 }
32 }
33}

Multilingual Reranking

Cohere’s rerank-v3.5 and rerank-multilingual-v3.0 models have been trained for performance across a variety of languages. Please note that performance may vary across languages. The model is trained on the following languages:

ISO CodeLanguage Name
afAfrikaans
amAmharic
arArabic
asAssamese
azAzerbaijani
beBelarusian
bgBulgarian
bnBengali
boTibetan
bsBosnian
caCatalan
cebCebuano
coCorsican
csCzech
cyWelsh
daDanish
deGerman
elGreek
enEnglish
eoEsperanto
esSpanish
etEstonian
euBasque
faPersian
fiFinnish
frFrench
fyFrisian
gaIrish
gdScots_gaelic
glGalician
guGujarati
haHausa
hawHawaiian
heHebrew
hiHindi
hmnHmong
hrCroatian
htHaitian_creole
huHungarian
hyArmenian
idIndonesian
igIgbo
isIcelandic
itItalian
jaJapanese
jvJavanese
kaGeorgian
kkKazakh
kmKhmer
knKannada
koKorean
kuKurdish
kyKyrgyz
LaLatin
LbLuxembourgish
LoLaothian
LtLithuanian
LvLatvian
mgMalagasy
miMaori
mkMacedonian
mlMalayalam
mnMongolian
mrMarathi
msMalay
mtMaltese
myBurmese
neNepali
nlDutch
noNorwegian
nyNyanja
orOriya
paPunjabi
plPolish
ptPortuguese
roRomanian
ruRussian
rwKinyarwanda
siSinhalese
skSlovak
slSlovenian
smSamoan
snShona
soSomali
sqAlbanian
srSerbian
stSesotho
suSundanese
svSwedish
swSwahili
taTamil
teTelugu
tgTajik
thThai
tkTurkmen
tlTagalog
trTurkish
ttTatar
ugUighur
ukUkrainian
urUrdu
uzUzbek
viVietnamese
woWolof
xhXhosa
yiYiddish
yoYoruba
zhChinese
zuZulu