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:
import cohere
co = cohere.ClientV2()
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-v3.5", query=query, documents=docs, top_n=5
)
{
"id": "97813271-fe74-465d-b9d5-577e77079253",
"results": [
{
"index": 3, // "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) ..."
"relevance_score": 0.9990564
},
{
"index": 4, // "Capital punishment has existed in the United States since before the United States was a country. As of 2017 ..."
"relevance_score": 0.7516481
},
{
"index": 1, // "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division ..."
"relevance_score": 0.08882029
},
{
"index": 0, // "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a ..."
"relevance_score": 0.058238626
},
{
"index": 2, // ""Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people ..."
"relevance_score": 0.019946935
}
],
"meta": {
"api_version": {
"version": "2"
},
"billed_units": {
"search_units": 1
}
}
}
Multilingual Reranking
Cohere’s Rerank models have been trained for performance across 100+ languages.
When choosing the model, please note the following language support:
Rerank 3.0: Separate English-only and multilingual models (rerank-english-v3.0 and rerank-multilingual-v3.0)
Rerank 3.5: A single multilingual model (rerank-v3.5)
The following table provides the list of languages supported by the Rerank models. Please note that performance may vary across languages.
No comments:
Post a Comment