|
| 1 | +--- |
| 2 | +pcx_content_type: concept |
| 3 | +title: Reranking |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +--- |
| 7 | + |
| 8 | +import { DashButton } from "~/components"; |
| 9 | + |
| 10 | +Reranking can help improve the quality of AI Search results by reordering retrieved documents based on semantic relevance to the user’s query. It applies a secondary model after retrieval to "rerank" the top results before they are outputted. |
| 11 | + |
| 12 | +## How it works |
| 13 | + |
| 14 | +By default, reranking is **disabled** for all AI Search instances. You can enable it during creation or later from the settings page. |
| 15 | + |
| 16 | +When enabled, AI Search will: |
| 17 | + |
| 18 | +1. Retrieve a set of relevant results from your index, constrained by your `max_num_of_results` and `score_threshold` parameters. |
| 19 | +2. Pass those results through a [reranking model](/ai-search/configuration/models/supported-models/). |
| 20 | +3. Return the reranked results, which the text generation model can use for answer generation. |
| 21 | + |
| 22 | +Reranking helps improve accuracy, especially for large or noisy datasets where vector similarity alone may not produce the optimal ordering. |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +You can configure reranking in several ways: |
| 27 | + |
| 28 | +### Configure via API |
| 29 | + |
| 30 | +When you make a `/search` or `/ai-search` request using the [Workers Binding](/ai-search/usage/workers-binding/) or [REST API](/ai-search/usage/rest-api/), you can: |
| 31 | + |
| 32 | +- Enable or disable reranking per request |
| 33 | +- Specify the reranking model |
| 34 | + |
| 35 | +For example: |
| 36 | + |
| 37 | +```javascript |
| 38 | +const answer = await env.AI.autorag("my-autorag").aiSearch({ |
| 39 | + query: "How do I train a llama to deliver coffee?", |
| 40 | + model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast", |
| 41 | + reranking: { |
| 42 | + enabled: true, |
| 43 | + model: "@cf/baai/bge-reranker-base" |
| 44 | + } |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +### Configure in dashboard for new AI Search |
| 49 | + |
| 50 | +When creating a new RAG in the dashboard: |
| 51 | + |
| 52 | +1. Go to **AI Search** in the Cloudflare dashboard. |
| 53 | + |
| 54 | + <DashButton url="/?to=/:account/ai/ai-search" /> |
| 55 | + |
| 56 | +2. Select **Create** > **Get started**. |
| 57 | +3. In the **Retrieval configuration** step, open the **Reranking** dropdown. |
| 58 | +4. Toggle **Reranking** on. |
| 59 | +5. Select the reranking model. |
| 60 | +6. Complete your setup. |
| 61 | + |
| 62 | +### Configure in dashboard for existing AI Search |
| 63 | + |
| 64 | +To update reranking for an existing instance: |
| 65 | + |
| 66 | +1. Go to **AI Search** in the Cloudflare dashboard. |
| 67 | + |
| 68 | + <DashButton url="/?to=/:account/ai/ai-search" /> |
| 69 | + |
| 70 | +2. Select an existing AI Search instance. |
| 71 | +3. Go to the **Settings** tab. |
| 72 | +4. Under **Reranking**, toggle reranking on. |
| 73 | +5. Select the reranking model. |
| 74 | + |
0 commit comments