Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/content/docs/ai-search/configuration/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The table below lists all available configuration options:
| [Query rewrite system prompt](/ai-search/configuration/system-prompt/) | yes | Custom system prompt to guide query rewriting behavior |
| [Match threshold](/ai-search/configuration/retrieval-configuration/) | yes | Minimum similarity score required for a vector match |
| [Maximum number of results](/ai-search/configuration/retrieval-configuration/) | yes | Maximum number of vector matches returned (`top_k`) |
| [Reranking](/ai-search/configuration/reranking/) | yes | Rerank to reorder retrieved results by semantic relevance using a reranking model after initial retrieval |
| [Generation model](/ai-search/configuration/models/) | yes | Model used to generate the final response |
| [Generation system prompt](/ai-search/configuration/system-prompt/) | yes | Custom system prompt to guide response generation |
| [Similarity caching](/ai-search/configuration/cache/) | yes | Enable or disable caching of responses for similar (not just exact) prompts |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Production models are the actively supported and recommended models that are sta
| **Workers AI** | `@cf/baai/bge-m3` | 1,024 | 512 | cosine |
| | `@cf/baai/bge-large-en-v1.5` | 1,024 | 512 | cosine |

### Reranking
| Provider | Alias | Input tokens |
|---|---|---|
| **Workers AI** | `@cf/baai/bge-reranker-base` | 512 |

## Transition models

There are currently no models marked for end-of-life.
61 changes: 61 additions & 0 deletions src/content/docs/ai-search/configuration/reranking.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
pcx_content_type: concept
title: Reranking
sidebar:
order: 4
---

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.

## How it works

By default, reranking is **disabled** for all AI Search instances. You can enable it during creation or later from the settings page.

When enabled, AI Search will:

1. Retrieve a set of relevant results from your index, constrained by your `max_num_of_results` and `score_threshold` parameters.
2. Pass those results through a [reranking model](/ai-search/configuration/models/supported-models/)
3. Return the reranked results, which the text generation model can use for answer generation.

Reranking helps improve accuracy, especially for large or noisy datasets where vector similarity alone may not produce the optimal ordering.

## Configuration

You can configure reranking in several ways:

### Configure via API

You can also configure via the API. 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it sounds a bit weird saying also here when this is the first example.


- Enable or disable reranking per request
- Specify the reranking model

For example:

```javascript
const answer = await env.AI.autorag("my-autorag").aiSearch({
query: "How do I train a llama to deliver coffee?",
model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
reranking: {
enabled: true,
model: "@cf/baai/bge-reranker-base"
}
});
```

### Configure in dashboard for new AI Search

When creating a new RAG in the dashboard:

1. In the Retrieval configuration step, open the Reranking dropdown
2. Toggle Reranking on
3. Select the reranking model

### Configure in dashboard for existing AI Search

To update reranking for an existing instance:

1. Go to your AI Search instance
2. Open the Settings tab
3. Enable or disable reranking, and select the reranking model

8 changes: 6 additions & 2 deletions src/content/docs/ai-search/usage/rest-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai-search/rags/{
"rewrite_query": false,
"max_num_results": 10,
"ranking_options": {
"score_threshold": 0.3
"score_threshold": 0.3,
"enabled": true,
"model": "@cf/baai/bge-reranker-base"
Comment on lines +56 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These attributes don't go in this object. They are exposed through a new object, reranking.

},
"stream": true,
}'
Expand Down Expand Up @@ -89,7 +91,9 @@ curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai-search/rags/{
"rewrite_query": true,
"max_num_results": 10,
"ranking_options": {
"score_threshold": 0.3
"score_threshold": 0.3,
"enabled": true
"model": "@cf/baai/bge-reranker-base"
Comment on lines +95 to +96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

},
}'

Expand Down
4 changes: 4 additions & 0 deletions src/content/docs/ai-search/usage/workers-binding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const answer = await env.AI.autorag("my-autorag").aiSearch({
max_num_results: 2,
ranking_options: {
score_threshold: 0.3,
enabled: true,
model: "@cf/baai/bge-reranker-base"
Comment on lines +50 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

},
stream: true,
});
Expand Down Expand Up @@ -116,6 +118,8 @@ const answer = await env.AI.autorag("my-autorag").search({
max_num_results: 2,
ranking_options: {
score_threshold: 0.3,
enabled: true,
model: "@cf/baai/bge-reranker-base"
Comment on lines +121 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

},
});
```
Expand Down
6 changes: 6 additions & 0 deletions src/content/partials/ai-search/ai-search-api-params.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Configurations for customizing result ranking. Defaults to `{}`.
- `score_threshold` <Type text="number" /> <MetaInfo text="optional" />
- The minimum match score required for a result to be considered a match. Defaults to `0`. Must be between `0` and `1`.

- `enabled` <Type text="boolean" /> <MetaInfo text="optional" />
- Enables or disables reranking, which reorders retrieved results based on semantic relevance using a reranking model. Defaults to `false`.

- `model` <Type text="string" /> <MetaInfo text="optional" />
- The reranking model to use when reranking is enabled.

Comment on lines +30 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, these should be placed under a new object called reranking.

`stream` <Type text="boolean" /> <MetaInfo text="optional" />

Returns a stream of results as they are available. Defaults to `false`.
Expand Down
6 changes: 6 additions & 0 deletions src/content/partials/ai-search/search-api-params.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Configurations for customizing result ranking. Defaults to `{}`.
- `score_threshold` <Type text="number" /> <MetaInfo text="optional" />
- The minimum match score required for a result to be considered a match. Defaults to `0`. Must be between `0` and `1`.

- `enabled` <Type text="boolean" /> <MetaInfo text="optional" />
- Enables or disables reranking, which reorders retrieved results based on semantic relevance using a reranking model. Defaults to `false`.

- `model` <Type text="string" /> <MetaInfo text="optional" />
- The reranking model to use when reranking is enabled.

Comment on lines +26 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

`filters` <Type text="object" /> <MetaInfo text="optional" />

Narrow down search results based on metadata, like folder and date, so only relevant content is retrieved. For more details, refer to [Metadata filtering](/ai-search/configuration/metadata).
Loading