2
2
3
3
import org .springframework .ai .rag .postretrieval .document .DocumentPostProcessor ;
4
4
import org .springframework .beans .factory .annotation .Value ;
5
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
5
6
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
6
7
import org .springframework .context .annotation .Bean ;
7
8
import org .springframework .context .annotation .Configuration ;
@@ -24,9 +25,32 @@ public class RerankConfig {
24
25
@ Value ("${spring.ai.rerank.cohere.api-key}" )
25
26
private String apiKey ;
26
27
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
+ */
27
36
@ Bean
28
37
@ ConditionalOnProperty (name = "spring.ai.rerank.enabled" , havingValue = "true" )
29
38
public DocumentPostProcessor rerankerPostProcessor () {
30
39
return new RerankerPostProcessor (CohereApi .builder ().apiKey (apiKey ).build ());
31
40
}
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
+ }
32
56
}
0 commit comments