For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
DASHBOARDPLAYGROUNDDOCSCOMMUNITYLOG IN
Guides and conceptsAPI ReferenceRelease NotesLLMUCookbooks
Guides and conceptsAPI ReferenceRelease NotesLLMUCookbooks
  • Cohere API
    • About
    • Teams and Roles
    • Errors
  • Endpoints
      • POSTRerank API (v1)
  • Deprecated
  • Audio
  • Batches
    • GETList batches
    • POSTCreate a batch
    • GETRetrieve a batch
    • POSTCancel a batch
LogoLogodocs
DASHBOARDPLAYGROUNDDOCSCOMMUNITYLOG IN
Endpointsv1/rerank

Rerank API (v1)

POST
https://api.cohere.com/v1/rerank
POST
/v1/rerank
1import cohere
2
3co = cohere.Client()
4
5docs = [
6 "Carson City is the capital city of the American state of Nevada.",
7 "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
8 "Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
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.",
10 "Capital punishment has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
11]
12
13response = co.rerank(
14 model="rerank-v4.0-pro",
15 query="What is the capital of the United States?",
16 documents=docs,
17 top_n=3,
18)
19print(response)
1{
2 "results": [
3 {
4 "index": 3,
5 "relevance_score": 0.999071
6 },
7 {
8 "index": 4,
9 "relevance_score": 0.7867867
10 },
11 {
12 "index": 0,
13 "relevance_score": 0.32713068
14 }
15 ],
16 "id": "8bc745a3-7871-4597-822e-18c95d5df48c",
17 "meta": {
18 "api_version": {
19 "version": "1"
20 },
21 "billed_units": {
22 "search_units": 1
23 }
24 }
25}
This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
Was this page helpful?
Previous

Create a Dataset

Next
Built with

Authentication

AuthorizationBearer

Bearer authentication of the form Bearer <token>, where token is your auth token.

Headers

X-Client-NamestringOptional
The name of the project that is making the request.

Request

querystringRequired
The search query
documentslist of strings or maps from strings to stringsRequired

A list of document objects or strings to rerank. If a document is provided the text fields is required and all other fields will be preserved in the response.

The total max chunks (length of documents * max_chunks_per_doc) must be less than 10000.

We recommend a maximum of 1,000 documents for optimal endpoint performance.

modelstringOptional

The identifier of the model to use, eg rerank-v3.5.

top_nintegerOptional>=1
The number of most relevant documents or indices to return, defaults to the length of the documents
rank_fieldslist of stringsOptional
If a JSON object is provided, you can specify which keys you would like to have considered for reranking. The model will rerank based on order of the fields passed in (i.e. rank_fields=['title','author','text'] will rerank using the values in title, author, text sequentially. If the length of title, author, and text exceeds the context length of the model, the chunking will not re-consider earlier fields). If not provided, the model will use the default text field for ranking.
return_documentsbooleanOptionalDefaults to false
- If false, returns results without the doc text - the api will return a list of {index, relevance score} where index is inferred from the list passed into the request. - If true, returns results with the doc text passed in - the api will return an ordered list of {index, text, relevance score} where index + text refers to the list passed into the request.
max_chunks_per_docintegerOptionalDefaults to 10
The maximum number of chunks to produce internally from a document

Response

OK
resultslist of objects
An ordered list of ranked documents
idstring
metaobject

Errors

400
Bad Request Error
401
Unauthorized Error
403
Forbidden Error
404
Not Found Error
422
Unprocessable Entity Error
429
Too Many Requests Error
498
Invalid Token Error
499
Client Closed Request Error
500
Internal Server Error
501
Not Implemented Error
503
Service Unavailable Error
504
Gateway Timeout Error

If a JSON object is provided, you can specify which keys you would like to have considered for reranking. The model will rerank based on order of the fields passed in (i.e. rank_fields=[‘title’,‘author’,‘text’] will rerank using the values in title, author, text sequentially. If the length of title, author, and text exceeds the context length of the model, the chunking will not re-consider earlier fields). If not provided, the model will use the default text field for ranking.

  • If false, returns results without the doc text - the api will return a list of {index, relevance score} where index is inferred from the list passed into the request.
  • If true, returns results with the doc text passed in - the api will return an ordered list of {index, text, relevance score} where index + text refers to the list passed into the request.