22
33import org .springframework .ai .rag .postretrieval .document .DocumentPostProcessor ;
44import org .springframework .beans .factory .annotation .Value ;
5+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
56import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
67import org .springframework .context .annotation .Bean ;
78import org .springframework .context .annotation .Configuration ;
@@ -24,9 +25,32 @@ public class RerankConfig {
2425 @ Value ("${spring.ai.rerank.cohere.api-key}" )
2526 private String apiKey ;
2627
28+ /**
29+ * Registers a DocumentPostProcessor bean that enables reranking using Cohere.
30+ *
31+ * This bean is only created when the property `spring.ai.rerank.enabled=true` is set.
32+ * The API key is injected from application properties or environment variables.
33+ *
34+ * @return An instance of RerankerPostProcessor backed by Cohere API
35+ */
2736 @ Bean
2837 @ ConditionalOnProperty (name = "spring.ai.rerank.enabled" , havingValue = "true" )
2938 public DocumentPostProcessor rerankerPostProcessor () {
3039 return new RerankerPostProcessor (CohereApi .builder ().apiKey (apiKey ).build ());
3140 }
41+
42+ /**
43+ * Provides a fallback DocumentPostProcessor when reranking is disabled
44+ * or no custom implementation is registered.
45+ *
46+ * This implementation performs no reranking and simply returns the original list of documents.
47+ * If additional post-processing is required, a custom bean should be defined.
48+ *
49+ * @return A pass-through DocumentPostProcessor that returns input as-is
50+ */
51+ @ Bean
52+ @ ConditionalOnMissingBean
53+ public DocumentPostProcessor noOpPostProcessor () {
54+ return (query , documents ) -> documents ;
55+ }
3256}
0 commit comments