Skip to content

Add first-class document reranking (RerankModel + RAG DocumentPostProcessor implementation) #6524

Description

@vaquarkhan

Problem
The modular RAG pipeline in RetrievalAugmentationAdvisor already wires a post-retrieval reranking step that loops over documentPostProcessors:

// 5. Post-process the documents.
for (var documentPostProcessor : this.documentPostProcessors) {
documents = documentPostProcessor.process(originalQuery, documents);
}
But Spring AI ships no implementation of this hook (implements DocumentPostProcessor → 0 matches), and there is no RerankModel abstraction in spring-ai-model. So out of the box, retrieved documents can only be ordered by first-stage vector similarity. Anyone wanting reranking — the primary lever for RAG precision@k — must hand-roll the plumbing with no portable API, auto-config, or observability.

Proposed solution
Add a portable, provider-agnostic RerankModel extends Model<RerankRequest, RerankResponse> in spring-ai-model (mirroring EmbeddingModel), plus a RerankingDocumentPostProcessor in spring-ai-rag that adapts it into the existing advisor — no change to the DocumentPostProcessor interface or the advisor contract. Ship a local ONNX cross-encoder backend first (runs in CI, no API key), then hosted backends (Cohere / Bedrock / Jina) as separate opt-in modules with auto-config + Micrometer observation.

Goals
Portable RerankModel abstraction consistent with existing Model<Req,Resp> design
RerankingDocumentPostProcessor with top-N truncation and score-threshold filtering
Self-contained local backend (no credentials), hosted backends incrementally

Anti-goals
No change to DocumentPostProcessor / RetrievalAugmentationAdvisor (the hook already exists)
No new retrieval/index engine; reranking only reorders an existing candidate set
Not shipping every hosted provider at once
Backwards compatibility
Purely additive and opt-in. No existing public API changes; no behavior change unless a reranker is explicitly added.

I'm happy to implement this. I've already prototyped the RerankModel abstraction in spring-ai-model and a RerankingDocumentPostProcessor in spring-ai-rag with unit tests, and can open a single feat/document-reranking PR (abstraction + RAG adapter + a local ONNX cross-encoder backend so it's CI-testable without API keys) once the direction is confirmed. Happy to start with a design/*.adoc if the team prefers the design-doc process.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions