4242import ai .djl .translate .Translator ;
4343import io .micrometer .observation .ObservationRegistry ;
4444
45+ /**
46+ * Spring configuration class for Redis OM AI features.
47+ * This configuration is automatically enabled when the property 'redis.om.spring.ai.enabled'
48+ * is set to true. It sets up various AI-related beans including embedding models,
49+ * vector databases, and document readers for different AI providers such as OpenAI,
50+ * Azure OpenAI, Ollama, and others.
51+ *
52+ * <p>The configuration provides support for:</p>
53+ * <ul>
54+ * <li>OpenAI embedding models and chat models</li>
55+ * <li>Azure OpenAI integration</li>
56+ * <li>Ollama local models</li>
57+ * <li>Qdrant vector database</li>
58+ * <li>Document processing and text readers</li>
59+ * <li>Image embedding with DJL models</li>
60+ * </ul>
61+ */
4562@ Configuration
4663@ EnableConfigurationProperties (
4764 { AIRedisOMProperties .class }
5168)
5269public class AIRedisConfiguration {
5370
71+ /**
72+ * Default constructor for Spring instantiation.
73+ * This constructor is used by Spring Framework to create instances of this configuration class.
74+ */
75+ public AIRedisConfiguration () {
76+ // Default constructor
77+ }
78+
5479 private static final Log logger = LogFactory .getLog (AIRedisConfiguration .class );
5580
5681 // @Value("${spring.data.redis.host:localhost}")
@@ -83,26 +108,66 @@ public class AIRedisConfiguration {
83108 //// return template;
84109 //// }
85110
111+ /**
112+ * Creates a RestClient.Builder bean for building REST clients.
113+ * This builder is used throughout the AI configuration for making HTTP requests
114+ * to various AI providers.
115+ *
116+ * @return a new RestClient.Builder instance
117+ */
86118 @ Bean
87119 public RestClient .Builder restClientBuilder () {
88120 return RestClient .builder ();
89121 }
90122
123+ /**
124+ * Creates a WebClient.Builder bean for building reactive web clients.
125+ * This builder is used for asynchronous HTTP requests to AI providers
126+ * that support reactive programming models.
127+ *
128+ * @return a new WebClient.Builder instance
129+ */
91130 @ Bean
92131 public WebClient .Builder webClientBuilder () {
93132 return WebClient .builder ();
94133 }
95134
135+ /**
136+ * Creates a default ResponseErrorHandler bean for handling HTTP response errors.
137+ * This handler is used by REST clients to process error responses from AI providers.
138+ *
139+ * @return a DefaultResponseErrorHandler instance
140+ */
96141 @ Bean
97142 public ResponseErrorHandler defaultResponseErrorHandler () {
98143 return new DefaultResponseErrorHandler ();
99144 }
100145
146+ /**
147+ * Creates an ObservationRegistry bean for monitoring and observability.
148+ * This registry is used to track metrics and traces for AI operations,
149+ * enabling monitoring of performance and usage patterns.
150+ *
151+ * @return a new ObservationRegistry instance
152+ */
101153 @ Bean
102154 public ObservationRegistry observationRegistry () {
103155 return ObservationRegistry .create ();
104156 }
105157
158+ /**
159+ * Creates an EmbeddingModelFactory bean responsible for creating embedding models
160+ * for different AI providers. This factory supports multiple providers including
161+ * OpenAI, Azure OpenAI, Ollama, and others.
162+ *
163+ * @param properties AI Redis OM configuration properties
164+ * @param springAiProperties Spring AI configuration properties
165+ * @param restClientBuilder builder for creating REST clients
166+ * @param webClientBuilder builder for creating reactive web clients
167+ * @param responseErrorHandler handler for HTTP response errors
168+ * @param observationRegistry registry for monitoring and observability
169+ * @return a configured EmbeddingModelFactory instance
170+ */
106171 @ Bean
107172 public EmbeddingModelFactory embeddingModelFactory (AIRedisOMProperties properties ,
108173 SpringAiProperties springAiProperties , RestClient .Builder restClientBuilder , WebClient .Builder webClientBuilder ,
@@ -111,13 +176,28 @@ public EmbeddingModelFactory embeddingModelFactory(AIRedisOMProperties propertie
111176 responseErrorHandler , observationRegistry );
112177 }
113178
179+ /**
180+ * Creates a DJL ImageFactory bean for image processing operations.
181+ * This factory is used to create and manipulate images for computer vision tasks
182+ * such as face detection and image embeddings.
183+ *
184+ * @return the singleton ImageFactory instance
185+ */
114186 @ Bean (
115187 name = "djlImageFactory"
116188 )
117189 public ImageFactory imageFactory () {
118190 return ImageFactory .getInstance ();
119191 }
120192
193+ /**
194+ * Creates criteria for loading DJL image embedding models.
195+ * This criteria defines the model type, engine, and URLs for downloading
196+ * the image embedding model used to convert images into vector representations.
197+ *
198+ * @param properties AI Redis OM configuration properties containing DJL settings
199+ * @return criteria for loading image embedding models
200+ */
121201 @ Bean (
122202 name = "djlImageEmbeddingModelCriteria"
123203 )
@@ -128,6 +208,13 @@ public Criteria<Image, float[]> imageEmbeddingModelCriteria(AIRedisOMProperties
128208 .build ();
129209 }
130210
211+ /**
212+ * Creates a translator for face detection models.
213+ * This translator handles the preprocessing and postprocessing of images
214+ * for face detection, including confidence thresholding and non-maximum suppression.
215+ *
216+ * @return a configured FaceDetectionTranslator with predefined detection parameters
217+ */
131218 @ Bean (
132219 name = "djlFaceDetectionTranslator"
133220 )
@@ -141,6 +228,15 @@ public Translator<Image, DetectedObjects> faceDetectionTranslator() {
141228 return new FaceDetectionTranslator (confThresh , nmsThresh , variance , topK , scales , steps );
142229 }
143230
231+ /**
232+ * Creates criteria for loading DJL face detection models.
233+ * This criteria defines the model specifications including the translator,
234+ * engine, and URLs for the face detection model.
235+ *
236+ * @param translator the face detection translator for preprocessing/postprocessing
237+ * @param properties AI Redis OM configuration properties containing DJL settings
238+ * @return criteria for loading face detection models
239+ */
144240 @ Bean (
145241 name = "djlFaceDetectionModelCriteria"
146242 )
@@ -158,6 +254,14 @@ public Criteria<Image, DetectedObjects> faceDetectionModelCriteria( //
158254 .build ();
159255 }
160256
257+ /**
258+ * Loads the DJL face detection model based on the provided criteria.
259+ * This model is used to detect faces in images before extracting face embeddings.
260+ * If the model cannot be loaded, returns null and logs a warning.
261+ *
262+ * @param criteria the criteria for loading the face detection model (nullable)
263+ * @return the loaded face detection model, or null if loading fails
264+ */
161265 @ Bean (
162266 name = "djlFaceDetectionModel"
163267 )
@@ -172,13 +276,29 @@ public ZooModel<Image, DetectedObjects> faceDetectionModel(@Nullable @Qualifier(
172276 }
173277 }
174278
279+ /**
280+ * Creates a translator for face embedding models.
281+ * This translator handles the preprocessing and postprocessing of face images
282+ * to extract facial feature vectors for similarity search.
283+ *
284+ * @return a new FaceFeatureTranslator instance
285+ */
175286 @ Bean (
176287 name = "djlFaceEmbeddingTranslator"
177288 )
178289 public Translator <Image , float []> faceEmbeddingTranslator () {
179290 return new FaceFeatureTranslator ();
180291 }
181292
293+ /**
294+ * Creates criteria for loading DJL face embedding models.
295+ * This criteria defines the model specifications for extracting facial features
296+ * as vector embeddings that can be used for face recognition and similarity search.
297+ *
298+ * @param translator the face embedding translator for preprocessing/postprocessing
299+ * @param properties AI Redis OM configuration properties containing DJL settings
300+ * @return criteria for loading face embedding models
301+ */
182302 @ Bean (
183303 name = "djlFaceEmbeddingModelCriteria"
184304 )
@@ -197,6 +317,14 @@ public Criteria<Image, float[]> faceEmbeddingModelCriteria( //
197317 .build ();
198318 }
199319
320+ /**
321+ * Loads the DJL face embedding model based on the provided criteria.
322+ * This model extracts facial features as vector embeddings for face recognition
323+ * and similarity search. If the model cannot be loaded, returns null and logs a warning.
324+ *
325+ * @param criteria the criteria for loading the face embedding model (nullable)
326+ * @return the loaded face embedding model, or null if loading fails
327+ */
200328 @ Bean (
201329 name = "djlFaceEmbeddingModel"
202330 )
@@ -211,6 +339,17 @@ public ZooModel<Image, float[]> faceEmbeddingModel(@Nullable @Qualifier(
211339 }
212340 }
213341
342+ /**
343+ * Loads the DJL image embedding model based on the provided criteria.
344+ * This model converts general images into vector embeddings for similarity search
345+ * and image retrieval tasks.
346+ *
347+ * @param criteria the criteria for loading the image embedding model (nullable)
348+ * @return the loaded image embedding model, or null if criteria is null
349+ * @throws MalformedModelException if the model format is invalid
350+ * @throws ModelNotFoundException if the model cannot be found
351+ * @throws IOException if there's an error loading the model
352+ */
214353 @ Bean (
215354 name = "djlImageEmbeddingModel"
216355 )
@@ -220,6 +359,14 @@ public ZooModel<Image, float[]> imageModel(@Nullable @Qualifier(
220359 return criteria != null ? ModelZoo .loadModel (criteria ) : null ;
221360 }
222361
362+ /**
363+ * Creates a default image preprocessing pipeline for DJL models.
364+ * This pipeline applies transformations such as center cropping, resizing,
365+ * and tensor conversion to prepare images for embedding models.
366+ *
367+ * @param properties AI Redis OM configuration properties containing pipeline settings
368+ * @return a configured Pipeline with image transformations
369+ */
223370 @ Bean (
224371 name = "djlDefaultImagePipeline"
225372 )
@@ -236,6 +383,14 @@ public Pipeline defaultImagePipeline(AIRedisOMProperties properties) {
236383 .add (new ToTensor ());
237384 }
238385
386+ /**
387+ * Creates a HuggingFace sentence tokenizer for text processing.
388+ * This tokenizer is used to prepare text for sentence embedding models.
389+ * Checks connectivity to HuggingFace before attempting to download the tokenizer.
390+ *
391+ * @param properties AI Redis OM configuration properties containing tokenizer settings
392+ * @return a configured HuggingFaceTokenizer, or null if unable to connect or load
393+ */
239394 @ Bean (
240395 name = "djlSentenceTokenizer"
241396 )
@@ -255,6 +410,21 @@ public HuggingFaceTokenizer sentenceTokenizer(AIRedisOMProperties properties) {
255410 }
256411 }
257412
413+ /**
414+ * Creates the primary Embedder bean responsible for generating embeddings.
415+ * This embedder integrates various AI providers and models to create vector embeddings
416+ * from text, images, and faces. It serves as the central component for the @Vectorize
417+ * annotation functionality.
418+ *
419+ * @param imageEmbeddingModel DJL model for general image embeddings (nullable)
420+ * @param faceEmbeddingModel DJL model for face embeddings (nullable)
421+ * @param imageFactory factory for creating and processing images (nullable)
422+ * @param defaultImagePipeline preprocessing pipeline for images (nullable)
423+ * @param properties AI Redis OM configuration properties
424+ * @param embeddingModelFactory factory for creating embedding models for various providers
425+ * @param ac Spring application context for accessing other beans
426+ * @return a configured DefaultEmbedder instance
427+ */
258428 @ Primary
259429 @ Bean (
260430 name = "featureExtractor"
0 commit comments