Appearance
Rerank
Rerank a list of documents by relevance to a query.
Create Rerank
POST https://api.3xcoder.com/v1/rerankReranks documents based on their relevance to a given query, returning them in order of relevance with scores.
Request Body
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Reranking model ID |
query | string | Yes | The query to compare documents against |
documents | array | Yes | Array of document strings to rerank |
top_n | integer | No | Number of top results to return. Default: all documents |
Response
json
{
"object": "list",
"data": [
{
"object": "rerank.result",
"index": 2,
"relevance_score": 0.95
},
{
"object": "rerank.result",
"index": 0,
"relevance_score": 0.72
},
{
"object": "rerank.result",
"index": 1,
"relevance_score": 0.31
}
],
"model": "rerank-model",
"usage": {
"total_tokens": 150
}
}Examples
bash
curl https://api.3xcoder.com/v1/rerank \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "rerank-model",
"query": "What is machine learning?",
"documents": [
"Machine learning is a subset of artificial intelligence that enables systems to learn from data.",
"The weather forecast predicts rain tomorrow.",
"Deep learning uses neural networks with many layers to analyze complex patterns."
],
"top_n": 2
}'python
import requests
response = requests.post(
"https://api.3xcoder.com/v1/rerank",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer your-api-key"
},
json={
"model": "rerank-model",
"query": "What is machine learning?",
"documents": [
"Machine learning is a subset of artificial intelligence that enables systems to learn from data.",
"The weather forecast predicts rain tomorrow.",
"Deep learning uses neural networks with many layers to analyze complex patterns."
],
"top_n": 2
}
)
results = response.json()
for item in results["data"]:
print(f"Document {item['index']}: score {item['relevance_score']:.4f}")javascript
const response = await fetch('https://api.3xcoder.com/v1/rerank', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
},
body: JSON.stringify({
model: 'rerank-model',
query: 'What is machine learning?',
documents: [
'Machine learning is a subset of artificial intelligence that enables systems to learn from data.',
'The weather forecast predicts rain tomorrow.',
'Deep learning uses neural networks with many layers to analyze complex patterns.'
],
top_n: 2
})
})
const data = await response.json()
data.data.forEach(item => {
console.log(`Document ${item.index}: score ${item.relevance_score}`)
})