> For the complete documentation index, see [llms.txt](https://guides.ia.numerique.gouv.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guides.ia.numerique.gouv.fr/albert-api/guides/reranking.md).

# Reranking

L’endpoint **`POST /v1/rerank`** réordonne une liste de documents textuels selon leur pertinence par rapport à une **requête**, via un modèle de type **`text-classification`**.

## Requête (`CreateRerank`)

* **`query`** (requis) — la question ou requête utilisateur ;
* **`documents`** (requis) — tableau de chaînes à classer ;
* **`model`** (requis) — identifiant d’un modèle `text-classification` retourné par `GET /v1/models` ;
* **`top_n`** — nombre max de résultats à retourner.

## Intégration typique

1. **Recherche large** — récupérez des candidats avec `POST /v1/search` (ou un moteur de recherche externe) ;
2. **Rerank** — appelez `POST /v1/rerank` pour améliorer l’ordre ;
3. **Chat / UI** — utilisez les meilleurs résultats pour générer une réponse ou afficher une liste ordonnée.

## Exemples (curl / Python / JavaScript)

{% tabs %}
{% tab title="curl" %}

```bash
curl -sS "https://albert.api.etalab.gouv.fr/v1/rerank" \
  -H "Authorization: Bearer $ALBERT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "REMPLACER_PAR_MODELE_RERANK",
    "query": "Quelle est la capitale de la France ?",
    "documents": [
      "Paris est la capitale.",
      "Le croissant est une viennoiserie.",
      "Lyon est une ville française."
    ],
    "top_n": 2
  }'
```

{% endtab %}

{% tab title="Python" %}

```python
import os
import requests

base_url = "https://albert.api.etalab.gouv.fr/v1"
api_key = os.environ["ALBERT_API_KEY"]
headers = {"Authorization": f"Bearer {api_key}"}

resp = requests.post(
    url=f"{base_url}/rerank",
    headers=headers,
    json={
        "model": "REMPLACER_PAR_MODELE_RERANK",
        "query": "Quelle est la capitale de la France ?",
        "documents": [
            "Paris est la capitale.",
            "Le croissant est une viennoiserie.",
            "Lyon est une ville française.",
        ],
        "top_n": 2,
    },
)
resp.raise_for_status()
print(resp.json())
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
const resp = await fetch("https://albert.api.etalab.gouv.fr/v1/rerank", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ALBERT_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "REMPLACER_PAR_MODELE_RERANK",
    query: "Quelle est la capitale de la France ?",
    documents: [
      "Paris est la capitale.",
      "Le croissant est une viennoiserie.",
      "Lyon est une ville française.",
    ],
    top_n: 2,
  }),
});

if (!resp.ok) throw new Error(await resp.text());
console.log(await resp.json());
```

{% endtab %}
{% endtabs %}

Pour la structure exacte de la réponse (scores, indices, etc.), voir la page de l’endpoint **Rerank** : [page de l’endpoint Rerank](https://doc.incubateur.net/alliance/albert-api/api-reference/liste-des-endpoint/rerank).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://guides.ia.numerique.gouv.fr/albert-api/guides/reranking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
