Skip to main content
POST
/
rerank
# Docs for v2 can be found by changing the above selector ^
from together import Together
import os

client = Together(
    api_key=os.environ.get("TOGETHER_API_KEY"),
)

documents = [
    {
        "title": "Llama",
        "text": "The llama is a domesticated South American camelid, widely used as a meat and pack animal by Andean cultures since the pre-Columbian era."
    },
    {
        "title": "Panda",
        "text": "The giant panda (Ailuropoda melanoleuca), also known as the panda bear or simply panda, is a bear species endemic to China."
    },
    {
        "title": "Guanaco",
        "text": "The guanaco is a camelid native to South America, closely related to the llama. Guanacos are one of two wild South American camelids; the other species is the vicuña, which lives at higher elevations."
    },
    {
        "title": "Wild Bactrian camel",
        "text": "The wild Bactrian camel (Camelus ferus) is an endangered species of camel endemic to Northwest China and southwestern Mongolia."
    }
]

response = client.rerank.create(
    model="Salesforce/Llama-Rank-v1",
    query="What animals can I find near Peru?",
    documents=documents,
)

for result in response.results:
    print(f"Rank: {result.index + 1}")
    print(f"Title: {documents[result.index]['title']}")
    print(f"Text: {documents[result.index]['text']}")
{
  "object": "rerank",
  "model": "salesforce/turboranker-0.8-3778-6328",
  "results": [
    {
      "index": 0,
      "relevance_score": 0.29980177813003117,
      "document": {
        "text": "{\"title\":\"Llama\",\"text\":\"The llama is a domesticated South American camelid, widely used as a meat and pack animal by Andean cultures since the pre-Columbian era.\"}"
      }
    },
    {
      "index": 2,
      "relevance_score": 0.2752447527354349,
      "document": {
        "text": "{\"title\":\"Guanaco\",\"text\":\"The guanaco is a camelid native to South America, closely related to the llama. Guanacos are one of two wild South American camelids; the other species is the vicuña, which lives at higher elevations.\"}"
      }
    }
  ],
  "id": "9dfa1a09-5ebc-4a40-970f-586cb8f4ae47",
  "usage": {
    "prompt_tokens": 1837,
    "completion_tokens": 0,
    "total_tokens": 1837
  }
}

Authorizations

Authorization
string
header
default:default
required

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

Body

application/json
model
required

The model to be used for the rerank request.

See all of Together AI's rerank models

Available options:
Salesforce/Llama-Rank-v1
Example:

"Salesforce/Llama-Rank-V1"

query
string
required

The search query to be used for ranking.

Example:

"What animals can I find near Peru?"

documents
required

List of documents, which can be either strings or objects.

Example:
[
  {
    "title": "Llama",
    "text": "The llama is a domesticated South American camelid, widely used as a meat and pack animal by Andean cultures since the pre-Columbian era."
  },
  {
    "title": "Panda",
    "text": "The giant panda (Ailuropoda melanoleuca), also known as the panda bear or simply panda, is a bear species endemic to China."
  },
  {
    "title": "Guanaco",
    "text": "The guanaco is a camelid native to South America, closely related to the llama. Guanacos are one of two wild South American camelids; the other species is the vicuña, which lives at higher elevations."
  },
  {
    "title": "Wild Bactrian camel",
    "text": "The wild Bactrian camel (Camelus ferus) is an endangered species of camel endemic to Northwest China and southwestern Mongolia."
  }
]
top_n
integer

The number of top results to return.

Example:

2

return_documents
boolean

Whether to return supplied documents with the response.

Example:

true

rank_fields
string[]

List of keys in the JSON Object document to rank by. Defaults to use all supplied keys for ranking.

Example:
["title", "text"]

Response

200

object
enum<string>
required

Object type

Available options:
rerank
Example:

"rerank"

model
string
required

The model to be used for the rerank request.

Example:

"salesforce/turboranker-0.8-3778-6328"

results
object[]
required
Example:
[
  {
    "index": 0,
    "relevance_score": 0.29980177813003117,
    "document": {
      "text": "{\"title\":\"Llama\",\"text\":\"The llama is a domesticated South American camelid, widely used as a meat and pack animal by Andean cultures since the pre-Columbian era.\"}"
    }
  },
  {
    "index": 2,
    "relevance_score": 0.2752447527354349,
    "document": {
      "text": "{\"title\":\"Guanaco\",\"text\":\"The guanaco is a camelid native to South America, closely related to the llama. Guanacos are one of two wild South American camelids; the other species is the vicuña, which lives at higher elevations.\"}"
    }
  }
]
id
string

Request ID

Example:

"9dfa1a09-5ebc-4a40-970f-586cb8f4ae47"

usage
object
Example:
{
  "prompt_tokens": 1837,
  "completion_tokens": 0,
  "total_tokens": 1837
}