@@ -104,7 +104,7 @@ public String getModelId(Map<String, Object> options) {
104
104
* @return Formatted text with highlighting
105
105
*/
106
106
public String getHighlightedSentences (String modelId , String question , String context ) {
107
- List <Map <String , Object >> result = fetchHighlightingResults (modelId , question , context );
107
+ List <Map <String , Object >> result = fetchModelResults (modelId , question , context );
108
108
if (result == null ) {
109
109
return StringUtils .EMPTY ;
110
110
}
@@ -120,7 +120,7 @@ public String getHighlightedSentences(String modelId, String question, String co
120
120
* @param context The document text
121
121
* @return The highlighting results
122
122
*/
123
- public List <Map <String , Object >> fetchHighlightingResults (String modelId , String question , String context ) {
123
+ public List <Map <String , Object >> fetchModelResults (String modelId , String question , String context ) {
124
124
125
125
CountDownLatch latch = new CountDownLatch (1 );
126
126
AtomicReference <List <Map <String , Object >>> resultRef = new AtomicReference <>();
@@ -165,44 +165,31 @@ public List<Map<String, Object>> fetchHighlightingResults(String modelId, String
165
165
* @return Formatted text with highlighting
166
166
*/
167
167
public String applyHighlighting (String context , List <Map <String , Object >> highlightResults ) {
168
- // Collect all valid highlight positions
169
168
List <int []> validHighlights = new ArrayList <>();
170
169
171
- // Process each highlight result
172
- for (Map <String , Object > result : highlightResults ) {
173
- if (result == null ) {
174
- continue ;
175
- }
176
-
177
- // Get the "highlights" list from the result
178
- Object highlightsObj = result .get (MODEL_INFERENCE_RESULT_KEY );
179
-
180
- // Safely check if the object is a List
181
- if (!(highlightsObj instanceof List <?> highlightsList )) {
182
- continue ;
183
- }
184
-
185
- // Process the list safely
186
- if (highlightsList .isEmpty ()) {
187
- continue ;
188
- }
189
-
190
- // Process each item in the list, checking if it's a Map
191
- for (Object item : highlightsList ) {
192
- if (item instanceof Map <?, ?> map ) {
193
- // Create a type-safe map
194
- Map <String , Object > safeMap = new java .util .HashMap <>();
195
- for (Map .Entry <?, ?> entry : map .entrySet ()) {
196
- safeMap .put (entry .getKey ().toString (), entry .getValue ());
197
- }
198
-
199
- // Extract and validate positions
200
- Object startObj = safeMap .get (MODEL_INFERENCE_RESULT_START_KEY );
201
- Object endObj = safeMap .get (MODEL_INFERENCE_RESULT_END_KEY );
202
-
203
- int [] positions = validateHighlightPositions (startObj , endObj , context .length ());
204
- if (positions != null ) {
205
- validHighlights .add (positions );
170
+ if (highlightResults != null && !highlightResults .isEmpty ()) {
171
+ Map <String , Object > result = highlightResults .getFirst ();
172
+ if (result != null ) {
173
+ // Get the "highlights" list from the result
174
+ Object highlightsObj = result .get (MODEL_INFERENCE_RESULT_KEY );
175
+
176
+ if (highlightsObj instanceof List <?> highlightsList ) {
177
+ for (Object item : highlightsList ) {
178
+ if (item instanceof Map <?, ?> map ) {
179
+ Map <String , Object > safeMap = new java .util .HashMap <>();
180
+ for (Map .Entry <?, ?> entry : map .entrySet ()) {
181
+ safeMap .put (entry .getKey ().toString (), entry .getValue ());
182
+ }
183
+
184
+ // Extract and validate positions
185
+ Object startObj = safeMap .get (MODEL_INFERENCE_RESULT_START_KEY );
186
+ Object endObj = safeMap .get (MODEL_INFERENCE_RESULT_END_KEY );
187
+
188
+ int [] positions = validateHighlightPositions (startObj , endObj , context .length ());
189
+ if (positions != null ) {
190
+ validHighlights .add (positions );
191
+ }
192
+ }
206
193
}
207
194
}
208
195
}
@@ -216,7 +203,6 @@ public String applyHighlighting(String context, List<Map<String, Object>> highli
216
203
// Sort highlights by start position (ascending)
217
204
validHighlights .sort (Comparator .comparingInt (pos -> pos [0 ]));
218
205
219
- // Construct the highlighted text in O(n) time
220
206
return constructHighlightedText (context , validHighlights );
221
207
}
222
208
@@ -273,7 +259,7 @@ private List<int[]> mergeOverlappingHighlights(List<int[]> highlights) {
273
259
}
274
260
275
261
List <int []> merged = new ArrayList <>();
276
- int [] current = highlights .get ( 0 );
262
+ int [] current = highlights .getFirst ( );
277
263
merged .add (current );
278
264
279
265
for (int i = 1 ; i < highlights .size (); i ++) {
0 commit comments