forked from opensearch-project/skills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPLToolTests.java
597 lines (514 loc) · 26.4 KB
/
PPLToolTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.agent.tools;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.opensearch.ml.common.CommonValue.ML_CONNECTOR_INDEX;
import static org.opensearch.ml.common.utils.StringUtils.gson;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.search.TotalHits;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.ml.common.output.model.MLResultDataType;
import org.opensearch.ml.common.output.model.ModelTensor;
import org.opensearch.ml.common.output.model.ModelTensorOutput;
import org.opensearch.ml.common.output.model.ModelTensors;
import org.opensearch.ml.common.transport.MLTaskResponse;
import org.opensearch.ml.common.transport.prediction.MLPredictionTaskAction;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.sql.plugin.transport.PPLQueryAction;
import org.opensearch.sql.plugin.transport.TransportPPLQueryResponse;
import org.opensearch.transport.client.AdminClient;
import org.opensearch.transport.client.Client;
import org.opensearch.transport.client.IndicesAdminClient;
import com.google.common.collect.ImmutableMap;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class PPLToolTests {
@Mock
private Client client;
@Mock
private AdminClient adminClient;
@Mock
private IndicesAdminClient indicesAdminClient;
@Mock
private GetMappingsResponse getMappingsResponse;
@Mock
private MappingMetadata mappingMetadata;
private Map<String, MappingMetadata> mockedMappings;
private Map<String, Object> indexMappings;
private SearchHits searchHits;
private SearchHit hit;
@Mock
private SearchResponse searchResponse;
private Map<String, Object> sampleMapping;
@Mock
private MLTaskResponse mlTaskResponse;
@Mock
private ModelTensorOutput modelTensorOutput;
@Mock
private ModelTensors modelTensors;
private ModelTensor modelTensor;
private Map<String, ?> pplReturns;
@Mock
private TransportPPLQueryResponse transportPPLQueryResponse;
private String mockedIndexName = "demo";
private String pplResult = "ppl result";
@Before
public void setup() {
MockitoAnnotations.openMocks(this);
createMappings();
// get mapping
when(mappingMetadata.getSourceAsMap()).thenReturn(indexMappings);
when(getMappingsResponse.getMappings()).thenReturn(mockedMappings);
when(client.admin()).thenReturn(adminClient);
when(adminClient.indices()).thenReturn(indicesAdminClient);
doAnswer(invocation -> {
ActionListener<GetMappingsResponse> listener = (ActionListener<GetMappingsResponse>) invocation.getArguments()[1];
listener.onResponse(getMappingsResponse);
return null;
}).when(indicesAdminClient).getMappings(any(), any());
// mockedMappings (index name, mappingmetadata)
// search result
when(searchResponse.getHits()).thenReturn(searchHits);
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) invocation.getArguments()[1];
listener.onResponse(searchResponse);
return null;
}).when(client).search(any(), any());
initMLTensors();
when(transportPPLQueryResponse.getResult()).thenReturn(pplResult);
doAnswer(invocation -> {
ActionListener<TransportPPLQueryResponse> listener = (ActionListener<TransportPPLQueryResponse>) invocation.getArguments()[2];
listener.onResponse(transportPPLQueryResponse);
return null;
}).when(client).execute(eq(PPLQueryAction.INSTANCE), any(), any());
PPLTool.Factory.getInstance().init(client);
}
@Test
public void testTool_WithoutModelId() {
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> PPLTool.Factory.getInstance().create(ImmutableMap.of("prompt", "contextPrompt"))
);
assertEquals("PPL tool needs non blank model id.", exception.getMessage());
}
@Test
public void testTool_WithBlankModelId() {
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", " "))
);
assertEquals("PPL tool needs non blank model id.", exception.getMessage());
}
@Test
public void testTool_WithNonIntegerHead() {
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "demo", "head", "11.5"))
);
assertEquals("PPL tool parameter head must be integer.", exception.getMessage());
}
@Test
public void testTool_WithNonBooleanExecute() {
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "demo", "execute", "hello"))
);
assertEquals("PPL tool parameter execute must be false or true", exception.getMessage());
}
@Test
public void testTool() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "100"));
assertEquals(PPLTool.TYPE, tool.getName());
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_ForSparkInputWithWrongSchema() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "100"));
assertEquals(PPLTool.TYPE, tool.getName());
List<Object> samples = List.of(Map.of("headers", List.of(Map.of("name", "X-Forwarded-For", "value", "34.210.155.133"))));
String wrongSchema = "array<structss<name:string,value:string>>";
Map<String, Object> schema = Map.of("headers", Map.of("col_name", "headers", "data_type", wrongSchema));
Exception exception = assertThrows(
IllegalStateException.class,
() -> tool
.run(
ImmutableMap
.of(
"index",
"demo",
"question",
"demo",
"samples",
gson.toJson(samples),
"schema",
gson.toJson(schema),
"type",
"s3"
),
ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { throw new IllegalStateException(e.getMessage()); })
)
);
assertEquals("Unable to extract field types from schema " + wrongSchema, exception.getMessage());
}
@Test
public void testTool_ForSparkInputWithArrayInput() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "100"));
assertEquals(PPLTool.TYPE, tool.getName());
List<Object> samples = List.of(Map.of("headers", List.of(Map.of("name", "X-Forwarded-For", "value", "34.210.155.133"))));
Map<String, Object> schema = Map
.of("headers", Map.of("col_name", "headers", "data_type", "array<struct<name:string,value:string>>"));
tool
.run(
ImmutableMap
.of("index", "demo", "question", "demo", "samples", gson.toJson(samples), "schema", gson.toJson(schema), "type", "s3"),
ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); })
);
}
@Test
public void testTool_ForSparkInputWithArrayString() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "100"));
assertEquals(PPLTool.TYPE, tool.getName());
List<Object> samples = List.of(Map.of("headers", List.of(Map.of("name", "X-Forwarded-For", "value", "34.210.155.133"))));
Map<String, Object> schema = Map.of("headers", Map.of("col_name", "headers", "data_type", "array<string>"));
tool
.run(
ImmutableMap
.of("index", "demo", "question", "demo", "samples", gson.toJson(samples), "schema", gson.toJson(schema), "type", "s3"),
ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); })
);
}
@Test
public void testTool_ForSparkInputWithStructInput() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "100"));
assertEquals(PPLTool.TYPE, tool.getName());
List<Object> samples = List
.of(
Map
.of(
"httpMethod",
"POST",
"httpRequest",
Map.of("headers", List.of(Map.of("name", "X-Forwarded-For", "value", "34.210.155.133")))
)
);
Map<String, Object> schema = Map
.of(
"httpRequest",
Map.of("col_name", "httpRequest", "data_type", "struct<headers:array<struct<name:string,value:string>>,httpMethod:string>"),
"httpMethod",
Map.of("col_name", "httpMethod", "data_type", "string")
);
tool
.run(
ImmutableMap
.of("index", "demo", "question", "demo", "samples", gson.toJson(samples), "schema", gson.toJson(schema), "type", "s3"),
ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); })
);
}
@Test
public void testTool_withPreviousInput() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "previous_tool_name", "previousTool", "head", "-5"));
assertEquals(PPLTool.TYPE, tool.getName());
tool.run(ImmutableMap.of("previousTool.output", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_withHEADButIgnore() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "5"));
assertEquals(PPLTool.TYPE, tool.getName());
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_withHEAD() {
pplReturns = Collections.singletonMap("response", "source=demo");
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
initMLTensors();
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "5"));
assertEquals(PPLTool.TYPE, tool.getName());
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo | head 5", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_with_WithoutExecution() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "model_type", "claude", "execute", "false"));
assertEquals(PPLTool.TYPE, tool.getName());
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> ret = gson.fromJson(executePPLResult, Map.class);
assertEquals("source=demo| head 1", ret.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_with_DefaultPrompt() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "model_type", "claude"));
assertEquals(PPLTool.TYPE, tool.getName());
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_withPPLTag() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
pplReturns = Collections.singletonMap("response", "<ppl>source=demo\n|\n\rhead 1</ppl>");
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
initMLTensors();
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo|head 1", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_withWrongEndpointInference() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
pplReturns = Collections.singletonMap("code", "424");
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
initMLTensors();
Exception exception = assertThrows(
IllegalStateException.class,
() -> tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(ppl -> {
assertEquals(pplResult, "ppl result");
}, e -> { throw new IllegalStateException(e.getMessage()); }))
);
assertEquals("Remote endpoint fails to inference.", exception.getMessage());
}
@Test
public void testTool_withWrongEndpointInferenceWithNullResponse() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
pplReturns = Collections.singletonMap("response", null);
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
initMLTensors();
Exception exception = assertThrows(
IllegalStateException.class,
() -> tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(ppl -> {
assertEquals(pplResult, "ppl result");
}, e -> { throw new IllegalStateException(e.getMessage()); }))
);
assertEquals("Remote endpoint fails to inference.", exception.getMessage());
}
@Test
public void testTool_withDescribeStartPPL() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
pplReturns = Collections.singletonMap("response", "describe demo");
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
initMLTensors();
tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("describe demo", returnResults.get("ppl"));
}, e -> { log.info(e); }));
}
@Test
public void testTool_querySystemIndex() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> tool.run(ImmutableMap.of("index", ML_CONNECTOR_INDEX, "question", "demo"), ActionListener.<String>wrap(ppl -> {
assertEquals(pplResult, "ppl result");
}, e -> { assertEquals("We cannot search system indices " + ML_CONNECTOR_INDEX, e.getMessage()); }))
);
assertEquals(
"PPLTool doesn't support searching indices starting with '.' since it could be system index, current searching index name: "
+ ML_CONNECTOR_INDEX,
exception.getMessage()
);
}
@Test
public void testTool_queryEmptyIndex() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> tool.run(ImmutableMap.of("question", "demo"), ActionListener.<String>wrap(ppl -> {
assertEquals(pplResult, "ppl result");
}, e -> { assertEquals("We cannot search system indices " + ML_CONNECTOR_INDEX, e.getMessage()); }))
);
assertEquals(
"Return this final answer to human directly and do not use other tools: 'Please provide index name'. Please try to directly send this message to human to ask for index name",
exception.getMessage()
);
}
@Test
public void testTool_WrongModelType() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "model_type", "wrong_model_type"));
assertEquals(PPLTool.PPLModelType.CLAUDE, tool.getPplModelType());
}
@Test
public void testTool_getMappingFailure() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
Exception exception = new Exception("get mapping error");
doAnswer(invocation -> {
ActionListener<GetMappingsResponse> listener = (ActionListener<GetMappingsResponse>) invocation.getArguments()[1];
listener.onFailure(exception);
return null;
}).when(indicesAdminClient).getMappings(any(), any());
tool
.run(
ImmutableMap.of("index", "demo", "question", "demo"),
ActionListener.<String>wrap(ppl -> { assertEquals(pplResult, "ppl result"); }, e -> {
assertEquals("get mapping error", e.getMessage());
})
);
}
@Test
public void testTool_predictModelFailure() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
Exception exception = new Exception("predict model error");
doAnswer(invocation -> {
ActionListener<MLTaskResponse> listener = (ActionListener<MLTaskResponse>) invocation.getArguments()[2];
listener.onFailure(exception);
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
tool
.run(
ImmutableMap.of("index", "demo", "question", "demo"),
ActionListener.<String>wrap(ppl -> { assertEquals(pplResult, "ppl result"); }, e -> {
assertEquals("predict model error", e.getMessage());
})
);
}
@Test
public void testTool_searchFailure() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
Exception exception = new Exception("search error");
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) invocation.getArguments()[1];
listener.onFailure(exception);
return null;
}).when(client).search(any(), any());
tool
.run(
ImmutableMap.of("index", "demo", "question", "demo"),
ActionListener.<String>wrap(ppl -> { assertEquals(pplResult, "ppl result"); }, e -> {
assertEquals("search error", e.getMessage());
})
);
}
@Test
public void testTool_executePPLFailure() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt"));
assertEquals(PPLTool.TYPE, tool.getName());
Exception exception = new Exception("execute ppl error");
doAnswer(invocation -> {
ActionListener<TransportPPLQueryResponse> listener = (ActionListener<TransportPPLQueryResponse>) invocation.getArguments()[2];
listener.onFailure(exception);
return null;
}).when(client).execute(eq(PPLQueryAction.INSTANCE), any(), any());
tool
.run(
ImmutableMap.of("index", "demo", "question", "demo"),
ActionListener.<String>wrap(ppl -> { assertEquals(pplResult, "ppl result"); }, e -> {
assertEquals("execute ppl:source=demo| head 1, get error: execute ppl error", e.getMessage());
})
);
}
private void createMappings() {
indexMappings = new HashMap<>();
indexMappings
.put(
"properties",
ImmutableMap
.of(
"demoFields",
ImmutableMap.of("type", "text"),
"demoNested",
ImmutableMap
.of(
"properties",
ImmutableMap.of("nest1", ImmutableMap.of("type", "text"), "nest2", ImmutableMap.of("type", "text"))
)
)
);
mockedMappings = new HashMap<>();
mockedMappings.put(mockedIndexName, mappingMetadata);
BytesReference bytesArray = new BytesArray("{\"demoFields\":\"111\", \"demoNested\": {\"nest1\": \"222\", \"nest2\": \"333\"}}");
hit = new SearchHit(1);
hit.sourceRef(bytesArray);
searchHits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f);
pplReturns = Collections.singletonMap("response", "source=demo| head 1");
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
}
private void initMLTensors(){
when(modelTensors.getMlModelTensors()).thenReturn(Collections.singletonList(modelTensor));
when(modelTensorOutput.getMlModelOutputs()).thenReturn(Collections.singletonList(modelTensors));
when(mlTaskResponse.getOutput()).thenReturn(modelTensorOutput);
// call model
doAnswer(invocation -> {
ActionListener<MLTaskResponse> listener = (ActionListener<MLTaskResponse>) invocation.getArguments()[2];
listener.onResponse(mlTaskResponse);
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
}
}