2020import org .elasticsearch .core .TimeValue ;
2121import org .elasticsearch .inference .ChunkedInferenceServiceResults ;
2222import org .elasticsearch .inference .ChunkingOptions ;
23+ import org .elasticsearch .inference .ChunkingSettings ;
2324import org .elasticsearch .inference .EmptyTaskSettings ;
2425import org .elasticsearch .inference .InferenceServiceConfiguration ;
2526import org .elasticsearch .inference .InferenceServiceResults ;
6970import static org .elasticsearch .xpack .inference .Utils .getPersistedConfigMap ;
7071import static org .elasticsearch .xpack .inference .Utils .inferenceUtilityPool ;
7172import static org .elasticsearch .xpack .inference .Utils .mockClusterServiceEmpty ;
73+ import static org .elasticsearch .xpack .inference .chunking .ChunkingSettingsTests .createRandomChunkingSettings ;
74+ import static org .elasticsearch .xpack .inference .chunking .ChunkingSettingsTests .createRandomChunkingSettingsMap ;
7275import static org .elasticsearch .xpack .inference .external .http .Utils .entityAsMap ;
7376import static org .elasticsearch .xpack .inference .external .http .Utils .getUrl ;
7477import static org .elasticsearch .xpack .inference .results .TextEmbeddingResultsTests .buildExpectationFloat ;
@@ -124,6 +127,7 @@ public void testParseRequestConfig_CreatesAIbmWatsonxEmbeddingsModel() throws IO
124127 assertThat (embeddingsModel .getServiceSettings ().url (), is (URI .create (url )));
125128 assertThat (embeddingsModel .getServiceSettings ().apiVersion (), is (apiVersion ));
126129 assertThat (embeddingsModel .getSecretSettings ().apiKey ().toString (), is (apiKey ));
130+ assertThat (embeddingsModel .getConfigurations ().getChunkingSettings (), instanceOf (ChunkingSettings .class ));
127131 }, e -> fail ("Model parsing should have succeeded, but failed: " + e .getMessage ()));
128132
129133 service .parseRequestConfig (
@@ -150,6 +154,45 @@ public void testParseRequestConfig_CreatesAIbmWatsonxEmbeddingsModel() throws IO
150154 }
151155 }
152156
157+ public void testParseRequestConfig_CreatesAIbmWatsonxEmbeddingsModelWhenChunkingSettingsProvided () throws IOException {
158+ try (var service = createIbmWatsonxService ()) {
159+ ActionListener <Model > modelListener = ActionListener .wrap (model -> {
160+ assertThat (model , instanceOf (IbmWatsonxEmbeddingsModel .class ));
161+
162+ var embeddingsModel = (IbmWatsonxEmbeddingsModel ) model ;
163+ assertThat (embeddingsModel .getServiceSettings ().modelId (), is (modelId ));
164+ assertThat (embeddingsModel .getServiceSettings ().projectId (), is (projectId ));
165+ assertThat (embeddingsModel .getServiceSettings ().url (), is (URI .create (url )));
166+ assertThat (embeddingsModel .getServiceSettings ().apiVersion (), is (apiVersion ));
167+ assertThat (embeddingsModel .getSecretSettings ().apiKey ().toString (), is (apiKey ));
168+ assertThat (embeddingsModel .getConfigurations ().getChunkingSettings (), instanceOf (ChunkingSettings .class ));
169+ }, e -> fail ("Model parsing should have succeeded, but failed: " + e .getMessage ()));
170+
171+ service .parseRequestConfig (
172+ "id" ,
173+ TaskType .TEXT_EMBEDDING ,
174+ getRequestConfigMap (
175+ new HashMap <>(
176+ Map .of (
177+ ServiceFields .MODEL_ID ,
178+ modelId ,
179+ IbmWatsonxServiceFields .PROJECT_ID ,
180+ projectId ,
181+ ServiceFields .URL ,
182+ url ,
183+ IbmWatsonxServiceFields .API_VERSION ,
184+ apiVersion
185+ )
186+ ),
187+ new HashMap <>(Map .of ()),
188+ createRandomChunkingSettingsMap (),
189+ getSecretSettingsMap (apiKey )
190+ ),
191+ modelListener
192+ );
193+ }
194+ }
195+
153196 public void testParseRequestConfig_ThrowsUnsupportedModelType () throws IOException {
154197 try (var service = createIbmWatsonxService ()) {
155198 var failureListener = getModelListenerForException (
@@ -235,6 +278,47 @@ public void testParsePersistedConfigWithSecrets_CreatesAIbmWatsonxEmbeddingsMode
235278 assertThat (embeddingsModel .getServiceSettings ().apiVersion (), is (apiVersion ));
236279 assertThat (embeddingsModel .getTaskSettings (), is (EmptyTaskSettings .INSTANCE ));
237280 assertThat (embeddingsModel .getSecretSettings ().apiKey ().toString (), is (apiKey ));
281+ assertThat (embeddingsModel .getConfigurations ().getChunkingSettings (), instanceOf (ChunkingSettings .class ));
282+ }
283+ }
284+
285+ public void testParsePersistedConfigWithSecrets_CreatesAIbmWatsonxEmbeddingsModelWhenChunkingSettingsProvided () throws IOException {
286+ try (var service = createIbmWatsonxService ()) {
287+ var persistedConfig = getPersistedConfigMap (
288+ new HashMap <>(
289+ Map .of (
290+ ServiceFields .MODEL_ID ,
291+ modelId ,
292+ IbmWatsonxServiceFields .PROJECT_ID ,
293+ projectId ,
294+ ServiceFields .URL ,
295+ url ,
296+ IbmWatsonxServiceFields .API_VERSION ,
297+ apiVersion
298+ )
299+ ),
300+ getTaskSettingsMapEmpty (),
301+ createRandomChunkingSettingsMap (),
302+ getSecretSettingsMap (apiKey )
303+ );
304+
305+ var model = service .parsePersistedConfigWithSecrets (
306+ "id" ,
307+ TaskType .TEXT_EMBEDDING ,
308+ persistedConfig .config (),
309+ persistedConfig .secrets ()
310+ );
311+
312+ assertThat (model , instanceOf (IbmWatsonxEmbeddingsModel .class ));
313+
314+ var embeddingsModel = (IbmWatsonxEmbeddingsModel ) model ;
315+ assertThat (embeddingsModel .getServiceSettings ().modelId (), is (modelId ));
316+ assertThat (embeddingsModel .getServiceSettings ().projectId (), is (projectId ));
317+ assertThat (embeddingsModel .getServiceSettings ().url (), is (URI .create (url )));
318+ assertThat (embeddingsModel .getServiceSettings ().apiVersion (), is (apiVersion ));
319+ assertThat (embeddingsModel .getTaskSettings (), is (EmptyTaskSettings .INSTANCE ));
320+ assertThat (embeddingsModel .getSecretSettings ().apiKey ().toString (), is (apiKey ));
321+ assertThat (embeddingsModel .getConfigurations ().getChunkingSettings (), instanceOf (ChunkingSettings .class ));
238322 }
239323 }
240324
@@ -399,6 +483,73 @@ public void testParsePersistedConfigWithSecrets_DoesNotThrowWhenAnExtraKeyExists
399483 }
400484 }
401485
486+ public void testParsePersistedConfig_CreatesAIbmWatsonxEmbeddingsModelWhenChunkingSettingsNotProvided () throws IOException {
487+ try (var service = createIbmWatsonxService ()) {
488+ var persistedConfig = getPersistedConfigMap (
489+ new HashMap <>(
490+ Map .of (
491+ ServiceFields .MODEL_ID ,
492+ modelId ,
493+ IbmWatsonxServiceFields .PROJECT_ID ,
494+ projectId ,
495+ ServiceFields .URL ,
496+ url ,
497+ IbmWatsonxServiceFields .API_VERSION ,
498+ apiVersion
499+ )
500+ ),
501+ getTaskSettingsMapEmpty (),
502+ null
503+ );
504+
505+ var model = service .parsePersistedConfig ("id" , TaskType .TEXT_EMBEDDING , persistedConfig .config ());
506+
507+ assertThat (model , instanceOf (IbmWatsonxEmbeddingsModel .class ));
508+
509+ var embeddingsModel = (IbmWatsonxEmbeddingsModel ) model ;
510+ assertThat (embeddingsModel .getServiceSettings ().modelId (), is (modelId ));
511+ assertThat (embeddingsModel .getServiceSettings ().projectId (), is (projectId ));
512+ assertThat (embeddingsModel .getServiceSettings ().url (), is (URI .create (url )));
513+ assertThat (embeddingsModel .getServiceSettings ().apiVersion (), is (apiVersion ));
514+ assertThat (embeddingsModel .getTaskSettings (), is (EmptyTaskSettings .INSTANCE ));
515+ assertThat (embeddingsModel .getConfigurations ().getChunkingSettings (), instanceOf (ChunkingSettings .class ));
516+ }
517+ }
518+
519+ public void testParsePersistedConfig_CreatesAIbmWatsonxEmbeddingsModelWhenChunkingSettingsProvided () throws IOException {
520+ try (var service = createIbmWatsonxService ()) {
521+ var persistedConfig = getPersistedConfigMap (
522+ new HashMap <>(
523+ Map .of (
524+ ServiceFields .MODEL_ID ,
525+ modelId ,
526+ IbmWatsonxServiceFields .PROJECT_ID ,
527+ projectId ,
528+ ServiceFields .URL ,
529+ url ,
530+ IbmWatsonxServiceFields .API_VERSION ,
531+ apiVersion
532+ )
533+ ),
534+ getTaskSettingsMapEmpty (),
535+ createRandomChunkingSettingsMap (),
536+ null
537+ );
538+
539+ var model = service .parsePersistedConfig ("id" , TaskType .TEXT_EMBEDDING , persistedConfig .config ());
540+
541+ assertThat (model , instanceOf (IbmWatsonxEmbeddingsModel .class ));
542+
543+ var embeddingsModel = (IbmWatsonxEmbeddingsModel ) model ;
544+ assertThat (embeddingsModel .getServiceSettings ().modelId (), is (modelId ));
545+ assertThat (embeddingsModel .getServiceSettings ().projectId (), is (projectId ));
546+ assertThat (embeddingsModel .getServiceSettings ().url (), is (URI .create (url )));
547+ assertThat (embeddingsModel .getServiceSettings ().apiVersion (), is (apiVersion ));
548+ assertThat (embeddingsModel .getTaskSettings (), is (EmptyTaskSettings .INSTANCE ));
549+ assertThat (embeddingsModel .getConfigurations ().getChunkingSettings (), instanceOf (ChunkingSettings .class ));
550+ }
551+ }
552+
402553 public void testInfer_ThrowsErrorWhenModelIsNotIbmWatsonxModel () throws IOException {
403554 var sender = mock (Sender .class );
404555
@@ -488,7 +639,15 @@ public void testInfer_SendsEmbeddingsRequest() throws IOException {
488639 }
489640 }
490641
491- public void testChunkedInfer_Batches () throws IOException {
642+ public void testChunkedInfer_ChunkingSettingsNotSet () throws IOException {
643+ testChunkedInfer_Batches (null );
644+ }
645+
646+ public void testChunkedInfer_ChunkingSettingsSet () throws IOException {
647+ testChunkedInfer_Batches (createRandomChunkingSettings ());
648+ }
649+
650+ private void testChunkedInfer_Batches (ChunkingSettings chunkingSettings ) throws IOException {
492651 var input = List .of ("foo" , "bar" );
493652
494653 var senderFactory = HttpRequestSenderTests .createSenderFactory (threadPool , clientManager );
@@ -878,6 +1037,18 @@ private static ActionListener<Model> getModelListenerForException(Class<?> excep
8781037 });
8791038 }
8801039
1040+ private Map <String , Object > getRequestConfigMap (
1041+ Map <String , Object > serviceSettings ,
1042+ Map <String , Object > taskSettings ,
1043+ Map <String , Object > chunkingSettings ,
1044+ Map <String , Object > secretSettings
1045+ ) {
1046+ var requestConfigMap = getRequestConfigMap (serviceSettings , taskSettings , secretSettings );
1047+ requestConfigMap .put (ModelConfigurations .CHUNKING_SETTINGS , chunkingSettings );
1048+
1049+ return requestConfigMap ;
1050+ }
1051+
8811052 private Map <String , Object > getRequestConfigMap (
8821053 Map <String , Object > serviceSettings ,
8831054 Map <String , Object > taskSettings ,
0 commit comments