# Rerank documents

`POST https://my.neuronai.uz/api/v1/rerank`

Справочник API › Реранжирование

Orders documents by how relevant each is to a query.

It works with Cohere's SDKs, at `/api/v1/rerank` and `/api/v2/rerank`. Billed per token, for the query and every document.

**Authentication:** `Authorization: Bearer <API key>`, a key with `rerank:write`.

**Pricing:** per 1K tokens: Input 0.1 credits.

## Тело запроса (`application/json`)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `model` | string | Yes | At most 128 characters. |
| `query` | string | Yes |  |
| `top_n` | integer \| null | No | Minimum 1. |
| `return_documents` | boolean \| null | No |  |
| `documents` | string[] | Yes |  |

## Ответ (200, `application/json`)

The documents, most relevant first. `index` is each one's position in `documents`, and `usage` counts the tokens billed.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | Yes |  |
| `model` | string | Yes |  |
| `results` | object[] | Yes |  |
| `results[].index` | integer | Yes |  |
| `results[].relevance_score` | number | Yes |  |
| `results[].document` | object | No |  |
| `results[].document.text` | string | Yes |  |
| `meta` | object | Yes |  |
| `meta.billed_units` | object | Yes |  |
| `meta.billed_units.search_units` | integer | Yes |  |
| `meta.billed_units.input_tokens` | integer | Yes |  |
| `usage` | object | Yes |  |
| `usage.prompt_tokens` | integer | Yes |  |
| `usage.total_tokens` | integer | Yes |  |

```json
{
  "id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41",
  "model": "gemma-4-26b-a4b",
  "results": [
    {
      "index": 0,
      "relevance_score": 0.92,
      "document": {
        "text": "Assalomu alaykum!"
      }
    }
  ],
  "meta": {
    "billed_units": {
      "search_units": 1,
      "input_tokens": 12
    }
  },
  "usage": {
    "prompt_tokens": 24,
    "total_tokens": 42
  }
}
```

### Заголовки

| Header | Type | Description |
| --- | --- | --- |
| `X-Cost` | number | The price billed, in credits. |
| `X-Request-Id` | string | Identifies this request. A JSON body carries it as `request_id` too; quote it to support. |
| `X-RateLimit-Limit` | integer | The requests a minute the rate limit on this endpoint allows. Sent once the limit has counted the request, so not when the key itself is refused. |
| `X-RateLimit-Remaining` | integer | How many of them are left this minute. |

## Ошибки

Every error has the same JSON shape:

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_request",
    "message": "The request is malformed or failed validation.",
    "param": null
  },
  "request_id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41"
}
```

| Status | Meaning |
| --- | --- |
| 401 | The API key is missing or not recognized (`invalid_api_key`), revoked (`api_key_revoked`), disabled (`api_key_disabled`) or expired (`api_key_expired`). |
| 402 | Not enough balance (`insufficient_balance`), or over the key's monthly spend limit (`spend_limit_exceeded`). Nothing is charged. |
| 403 | The key lacks the permission this endpoint needs (`insufficient_permissions`), or the client IP is not in its allowlist (`ip_not_allowed`). |
| 404 | `model` is not a rerank model this API serves: `model_not_found`. |
| 422 | The request failed validation (`invalid_request`), it has too many documents (`too_many_documents`), the query with a document is too long (`input_too_large`), or the model refused the input (`upstream_rejected_input`). Nothing is charged. |
| 429 | The model is busy (`upstream_rate_limited`), or a rate or concurrency limit was reached (`rate_limit_exceeded`, `concurrency_limit_exceeded`). Retry after `Retry-After` seconds. Nothing is charged. |
| 502 | The rerank model failed or could not be reached: `upstream_error`. Nothing is charged. |
| default | Any other error, in the same shape, such as `server_error` (500) or `service_unavailable` (503). |

## Examples

### cURL

```bash
curl -X POST "https://my.neuronai.uz/api/v1/rerank" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <your-api-key>" \
  -d '{
    "model": "bge-reranker-v2-m3",
    "query": "ob-havo",
    "documents": ["Bugun havo issiq.", "Docker qollanmasi."],
    "top_n": 5
  }'
```

### TypeScript SDK

```typescript
import {NeuronAI} from "@naiuz/sdk";

const client = new NeuronAI();

const ranked = await client.rerank.create({
    model: "bge-reranker-v2-m3",
    query: "ob-havo",
    documents: ["Bugun havo issiq.", "Docker qollanmasi."],
    top_n: 5,
});

for (const result of ranked.results) {
    console.log(result.index, result.relevance_score);
}
```

### Python SDK

```python
from naiuz import NeuronAI

client = NeuronAI()

ranked = client.rerank.create(
    model="bge-reranker-v2-m3",
    query="ob-havo",
    documents=["Bugun havo issiq.", "Docker qollanmasi."],
    top_n=5,
)

for result in ranked.results:
    print(result.index, result.relevance_score)
```

### PHP SDK

```php
$client = new \Naiuz\NeuronAI();

$ranked = $client->rerank->create([
    'model' => 'bge-reranker-v2-m3',
    'query' => 'ob-havo',
    'documents' => ['Bugun havo issiq.', 'Docker qollanmasi.'],
    'top_n' => 5,
]);

foreach ($ranked->results as $result) {
    echo $result->index, ' ', $result->relevance_score, PHP_EOL;
}
```

### JavaScript

```javascript
const response = await fetch('https://my.neuronai.uz/api/v1/rerank', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer <your-api-key>',
  },
  body: JSON.stringify({
    model: 'bge-reranker-v2-m3',
    query: 'ob-havo',
    documents: ['Bugun havo issiq.', 'Docker qollanmasi.'],
    top_n: 5,
  }),
});

const { results } = await response.json();
```

---

Source: https://docs.neuronai.uz/ru/api/reference/reranking/rerank
