[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:
    1. Fetch Rerank Candidate Count documents.
    2. Compute embeddings via Rerank Embedding Model and the provided template.
    3. Sort candidates by similarity to the query.
    4. Return the top RAG max results (original) as the block output.
Command icon 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

nikro created an issue. See original summary.

nikro’s picture

Okay 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.

nikro’s picture

Assigned: nikro » Unassigned
Status: Active » Needs review

wouters_f’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

nikro’s picture

Assigned: Unassigned » nikro
Status: Reviewed & tested by the community » Active

I'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.

nikro’s picture

Issue summary: View changes
nikro’s picture

Assigned: nikro » Unassigned
Status: Active » Needs review

Alright 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.

nikro’s picture

StatusFileSize
new135.22 KB

Here'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).

abhisekmazumdar’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new221.26 KB

I 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.

abhisekmazumdar’s picture

StatusFileSize
new224.56 KB
new223.41 KB
new92.95 KB
nikro’s picture

Status: Reviewed & tested by the community » Fixed

Merged - 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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • nikro committed 14b4e263 on 1.0.x
    Issue #3525092 by nikro: Add optional re‑ranking step
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.