Reranking
How Rerank Works
Traditional semantic search consists of a two-part system. An initial retrieval mechanism does an approximate sweep over a collection of documents and creates a document list. Then, a re-ranker mechanism will take this candidate document list and re-rank the elements. With Rerank, you can improve your models by re-organizing your results based on certain parameters.
Get Started
Example Request
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 (the death penalty) 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-english-v2.0", query=query, documents=docs, top_n=3)
Parameters:
documents
(required) - a list of JSON or string objects.- a list of document objects to rerank - it is assumed the object has a
text
key.
- a list of document objects to rerank - it is assumed the object has a
query
(required) - string- The search query that you would like the documents to be ranked against.
model
(required) - two models:- An english model,
rerank-english-v2.0
. - A multilingual model ,
rerank-multilingual-v2.0
.
- An english model,
top_n
(optional) - integer, default is the length of the documents list passed in.- the number of documents returned, ranked in relevance against your query.
max_chunks_per_doc
(optional)- integer, default is10
- If your document exceeds 512 tokens, this will determine the maximum number of chunks a document can be split into. For example, if your document is 6000 tokens, with the default of
10
, the document will be split into 10 chunks each of 512 tokens and the last 880 tokens will be disregarded.
- If your document exceeds 512 tokens, this will determine the maximum number of chunks a document can be split into. For example, if your document is 6000 tokens, with the default of
return_documents
(optional, API only) - boolean, default set asfalse
.- if
true
, the documents will be returned along with their associated text. - if
false
, the documents will not be returned and the response object will have{index, relevance_score}
whereindex
is the document order.In the SDK, documents are always returned, and
return_documents
is not a valid parameter.
- if
Example Response
{
"id":"0ded10ab-3326-41e3-aed3-241220ce5fc4",
"results":[
{
"document":
{
"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.9871293
},
{
"document":
{
"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.29961726
},
{
"document":
{
"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.08977329
},
{
"document":
{
"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.041462272
}
],
"meta":{"api_version":{"version":"2022-12-06"}}}
Multilingual Reranking
Cohere offers a multilingual model, rerank-multilingual-v2.0
. The model is trained on the following languages:
English, Chinese, French, German, Indonesian, Italian, Portuguese, Russian, Spanish, Arabic, Dutch, Hindi, Japanese, Vietnamese
Updated 29 days ago