[Tracker]
Update Summary: Add optional re‑ranking step [for ai_search_block]
Short Description: Allow for same-model reranking (if instruct based)
Check-in Date: 12/16/2025
Metadata is used by the AI Tracker. Docs and additional fields here.
[/Tracker]
Problem/Motivation
The AI Search Block currently returns the top RAG max results directly from the retriever. However, highly relevant documents can exist beyond that cutoff, causing important results to be omitted. An optional re‑ranking pass on a larger candidate set can improve result quality.
Proposed resolution
- Rename the existing RAG max results label to “RAG final max results” for clarity.
- Add a checkbox Enable Reranking in the block’s settings form.
- When enabled (via Form API #states), reveal:
- Rerank Candidate Count: number of initial hits to fetch (e.g. 50).
- Rerank Embedding Model: select which embedding model to use for re‑ranking.
- Reranking Template: text-based template for reranking.
- Enhance the retrieval pipeline:
- Fetch Rerank Candidate Count documents.
- Compute embeddings via Rerank Embedding Model and the provided template.
- Sort candidates by similarity to the query.
- Return the top RAG max results (original) as the block output.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | enable-reranking-config.png | 92.95 KB | abhisekmazumdar |
| #11 | ranking-enabled.png | 223.41 KB | abhisekmazumdar |
| #11 | all-red-things.png | 224.56 KB | abhisekmazumdar |
| #10 | without-ranking-enabled.png | 221.26 KB | abhisekmazumdar |
| #9 | reranking.png | 135.22 KB | nikro |
Issue fork ai_search_block-3525092
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
nikro commentedOkay now that we cleared - https://www.drupal.org/project/ai/issues/3525296
And https://www.drupal.org/project/ai_vdb_provider_postgres/issues/3525329
--
Along the way of implementation, realized that https://www.drupal.org/project/ai/issues/3488114 - reranking operation wasn't done yet - so we can't proceed with the FULL reranking (cross-encoder) - so, for now we can just reuse the same embedding model (especially if it's "instruct" bi-encoder) - and use it.
I tested and it does help, it pulled some results closer (i.e. 20 candidates and 5 cut-off, those 5 usually weren't always the same as top 5 would have been). Difference is small but still.
NOTE: this is special for instruct-based embedding models.
Comment #3
nikro commentedComment #5
wouters_f commentedLooks good to me.
Comment #6
nikro commentedI'm picking this one - because the main branch moved, and I need to make sure this one lands nicely, will spend a few minutes / hours testing this one out and polishing it a bit as well.
Comment #7
nikro commentedComment #8
nikro commentedAlright alright, did some testing here and there and it seems to work well.
I'd like someone with DS's (or similar) - E5 Large Instruct embedding model to test this bit.
Comment #9
nikro commentedHere's also an explainer:
If enabled (and it's important: if it's bi-directional encoder, meaning that we can send a special query-based embeddings).
Comment #10
abhisekmazumdarI set this up primarily with the Dropsolid AI Provider.
I tested it end to end. Enabling re-ranking and configuring it works well. The results improved in my small set of content.
Without ranking Enabled
Re-Ranking Enabled
I tested this with OpenAI provider given supported model like text-embedding-3-small. It worked well without error.
For other non-supported models using Groq or Open Router provider, which in my testing (and as far as I know) doesn't have any supported model, the config form shows a model from OpenRouter and when try to search something it throws an error.
The response say: "An error happened."
Followed by logs saying:
Exception: Failed to search: Model [object Object] does not support embeddings output modality in Drupal\ai_search_block\AiSearchBlockHelper->getRagResults() (line 563 of /var/www/html/web/modules/contrib/ai_search_block/src/AiSearchBlockHelper.php).I feel the error is managed and reasonable information is given to the developer to debug it.
Comment #11
abhisekmazumdarComment #12
nikro commentedMerged - and because the errors/warnings in the pipeline were bugging me - opened https://www.drupal.org/project/ai_search_block/issues/3566247 as a follow-up.