forked from opensearch-project/skills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAGToolTests.java
510 lines (441 loc) · 25.7 KB
/
RAGToolTests.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.agent.tools;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.opensearch.agent.tools.AbstractRetrieverTool.*;
import static org.opensearch.agent.tools.AbstractRetrieverToolTests.*;
import static org.opensearch.agent.tools.VectorDBTool.DEFAULT_K;
import static org.opensearch.agent.tools.utils.CommonConstants.COMMON_MODEL_ID_FIELD;
import static org.opensearch.ml.common.utils.StringUtils.gson;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.ParseField;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.index.query.QueryBuilder;
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.spi.tools.Parser;
import org.opensearch.ml.common.transport.MLTaskResponse;
import org.opensearch.ml.common.transport.prediction.MLPredictionTaskAction;
import org.opensearch.ml.repackage.com.google.common.collect.ImmutableMap;
import org.opensearch.transport.client.Client;
import lombok.SneakyThrows;
public class RAGToolTests {
public static final String TEST_QUERY_TEXT = "hello?";
public static final String TEST_EMBEDDING_FIELD = "test_embedding";
public static final String TEST_EMBEDDING_MODEL_ID = "1234";
public static final String TEST_INFERENCE_MODEL_ID = "1234";
public static final String TEST_NEURAL_QUERY_TYPE = "neural";
public static final String TEST_NEURAL_SPARSE_QUERY_TYPE = "neural_sparse";
public static final String TEST_NESTED_PATH = "nested_path";
static public final NamedXContentRegistry TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY = getQueryNamedXContentRegistry();
private RAGTool ragTool;
private String mockedSearchResponseString;
private String mockedEmptySearchResponseString;
private String mockedNeuralSparseSearchResponseString;
@Mock
private Parser mockOutputParser;
@Mock
private Client client;
@Mock
private ActionListener<ModelTensorOutput> listener;
private Map<String, Object> params;
@Before
@SneakyThrows
public void setup() {
try (InputStream searchResponseIns = AbstractRetrieverTool.class.getResourceAsStream("retrieval_tool_search_response.json")) {
if (searchResponseIns != null) {
mockedSearchResponseString = new String(searchResponseIns.readAllBytes(), StandardCharsets.UTF_8);
}
}
try (InputStream searchResponseIns = AbstractRetrieverTool.class.getResourceAsStream("retrieval_tool_empty_search_response.json")) {
if (searchResponseIns != null) {
mockedEmptySearchResponseString = new String(searchResponseIns.readAllBytes(), StandardCharsets.UTF_8);
}
}
try (InputStream searchResponseIns = AbstractRetrieverTool.class.getResourceAsStream("neural_sparse_tool_search_response.json")) {
if (searchResponseIns != null) {
mockedNeuralSparseSearchResponseString = new String(searchResponseIns.readAllBytes(), StandardCharsets.UTF_8);
}
}
client = mock(Client.class);
listener = mock(ActionListener.class);
RAGTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
VectorDBTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
NeuralSparseSearchTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
params = new HashMap<>();
params.put(RAGTool.INDEX_FIELD, TEST_INDEX);
params.put(RAGTool.EMBEDDING_FIELD, TEST_EMBEDDING_FIELD);
params.put(RAGTool.SOURCE_FIELD, gson.toJson(TEST_SOURCE_FIELDS));
params.put(RAGTool.EMBEDDING_MODEL_ID_FIELD, TEST_EMBEDDING_MODEL_ID);
params.put(RAGTool.INFERENCE_MODEL_ID_FIELD, TEST_INFERENCE_MODEL_ID);
params.put(RAGTool.DOC_SIZE_FIELD, AbstractRetrieverToolTests.TEST_DOC_SIZE.toString());
params.put(RAGTool.K_FIELD, DEFAULT_K.toString());
params.put(RAGTool.QUERY_TYPE, TEST_NEURAL_QUERY_TYPE);
params.put(RAGTool.CONTENT_GENERATION_FIELD, "true");
ragTool = RAGTool.Factory.getInstance().create(params);
}
@Test
public void testValidate() {
assertTrue(ragTool.validate(Map.of(AbstractRetrieverTool.INPUT_FIELD, "hi")));
assertFalse(ragTool.validate(Map.of(AbstractRetrieverTool.INPUT_FIELD, "")));
assertFalse(ragTool.validate(Map.of(AbstractRetrieverTool.INPUT_FIELD, " ")));
assertFalse(ragTool.validate(Map.of("test", " ")));
assertFalse(ragTool.validate(new HashMap<>()));
assertFalse(ragTool.validate(null));
}
@Test
public void testGetAttributes() {
assertEquals(ragTool.getVersion(), null);
assertEquals(ragTool.getType(), RAGTool.TYPE);
assertEquals(ragTool.getInferenceModelId(), TEST_INFERENCE_MODEL_ID);
}
@Test
public void testSetName() {
assertEquals(ragTool.getName(), RAGTool.TYPE);
ragTool.setName("test-tool");
assertEquals(ragTool.getName(), "test-tool");
}
@Test
public void testOutputParser() throws IOException {
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
ragTool.setXContentRegistry(mockNamedXContentRegistry);
ModelTensorOutput mlModelTensorOutput = getMlModelTensorOutput();
SearchResponse mockedSearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedSearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedSearchResponse);
return null;
}).when(client).search(any(), any());
doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build());
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
ragTool.setOutputParser(mockOutputParser);
ragTool.run(Map.of(INPUT_FIELD, TEST_QUERY_TEXT), listener);
verify(client).search(any(), any());
verify(client).execute(any(), any(), any());
}
@Test
public void testRunWithEmptySearchResponse() throws IOException {
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
ragTool.setXContentRegistry(mockNamedXContentRegistry);
ModelTensorOutput mlModelTensorOutput = getMlModelTensorOutput();
SearchResponse mockedEmptySearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedEmptySearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedEmptySearchResponse);
return null;
}).when(client).search(any(), any());
doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build());
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
ragTool.run(Map.of(INPUT_FIELD, TEST_QUERY_TEXT), listener);
verify(client).search(any(), any());
verify(client).execute(any(), any(), any());
}
@Test
public void testRunWithNeuralSparseQueryType() throws IOException {
Map<String, Object> paramsWithNeuralSparse = new HashMap<>(params);
paramsWithNeuralSparse.put(RAGTool.QUERY_TYPE, TEST_NEURAL_SPARSE_QUERY_TYPE);
RAGTool rAGtoolWithNeuralSparseQuery = RAGTool.Factory.getInstance().create(paramsWithNeuralSparse);
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
rAGtoolWithNeuralSparseQuery.setXContentRegistry(mockNamedXContentRegistry);
ModelTensorOutput mlModelTensorOutput = getMlModelTensorOutput();
SearchResponse mockedNeuralSparseSearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
mockedNeuralSparseSearchResponseString
)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedNeuralSparseSearchResponse);
return null;
}).when(client).search(any(), any());
doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build());
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
rAGtoolWithNeuralSparseQuery.run(Map.of(INPUT_FIELD, TEST_QUERY_TEXT), listener);
verify(client).search(any(), any());
verify(client).execute(any(), any(), any());
}
@Test
public void testRunWithInvalidQueryType() throws IOException {
RAGTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
Map<String, Object> paramsWithInvalidQueryType = new HashMap<>(params);
paramsWithInvalidQueryType.put(RAGTool.QUERY_TYPE, "sparse");
try {
RAGTool rAGtoolWithInvalidQueryType = RAGTool.Factory.getInstance().create(paramsWithInvalidQueryType);
} catch (IllegalArgumentException e) {
assertEquals("Failed to read queryType, please input neural_sparse or neural.", e.getMessage());
}
}
@Test
public void testRunWithQuestionJson() throws IOException {
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
ragTool.setXContentRegistry(mockNamedXContentRegistry);
ModelTensorOutput mlModelTensorOutput = getMlModelTensorOutput();
SearchResponse mockedEmptySearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedEmptySearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedEmptySearchResponse);
return null;
}).when(client).search(any(), any());
doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build());
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
ragTool.run(Map.of(INPUT_FIELD, "{question:'what is the population in seattle?'}"), listener);
verify(client).search(any(), any());
verify(client).execute(any(), any(), any());
}
@Test
public void testRunEmptyResponseWithNotEnableContentGeneration() throws IOException {
ActionListener<String> mockListener = mock(ActionListener.class);
Map<String, Object> paramsWithNotEnableContentGeneration = new HashMap<>(params);
paramsWithNotEnableContentGeneration.put(RAGTool.CONTENT_GENERATION_FIELD, "false");
RAGTool rAGtoolWithNotEnableContentGeneration = RAGTool.Factory.getInstance().create(paramsWithNotEnableContentGeneration);
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
rAGtoolWithNotEnableContentGeneration.setXContentRegistry(mockNamedXContentRegistry);
SearchResponse mockedEmptySearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedEmptySearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedEmptySearchResponse);
return null;
}).when(client).search(any(), any());
rAGtoolWithNotEnableContentGeneration.run(Map.of(INPUT_FIELD, "{question:'what is the population in seattle?'}"), mockListener);
verify(client).search(any(), any());
ArgumentCaptor<String> responseCaptor = ArgumentCaptor.forClass(String.class);
verify(mockListener).onResponse(responseCaptor.capture());
assertEquals("Can not get any match from search result.", responseCaptor.getValue());
}
@Test
public void testRunResponseWithNotEnableContentGeneration() throws IOException {
ActionListener<String> mockListener = mock(ActionListener.class);
Map<String, Object> paramsWithNotEnableContentGeneration = new HashMap<>(params);
paramsWithNotEnableContentGeneration.put(RAGTool.CONTENT_GENERATION_FIELD, "false");
RAGTool rAGtoolWithNotEnableContentGeneration = RAGTool.Factory.getInstance().create(paramsWithNotEnableContentGeneration);
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
rAGtoolWithNotEnableContentGeneration.setXContentRegistry(mockNamedXContentRegistry);
SearchResponse mockedNeuralSparseSearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.IGNORE_DEPRECATIONS,
mockedNeuralSparseSearchResponseString
)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedNeuralSparseSearchResponse);
return null;
}).when(client).search(any(), any());
rAGtoolWithNotEnableContentGeneration.run(Map.of(INPUT_FIELD, "{question:'what is the population in seattle?'}"), mockListener);
verify(client).search(any(), any());
ArgumentCaptor<String> responseCaptor = ArgumentCaptor.forClass(String.class);
verify(mockListener).onResponse(responseCaptor.capture());
assertEquals(
"{\"_index\":\"my-nlp-index\",\"_source\":{\"passage_text\":\"Hello world\",\"passage_embedding\":{\"!\":0.8708904,\"door\":0.8587369,\"hi\":2.3929274,\"worlds\":2.7839446,\"yes\":0.75845814,\"##world\":2.5432441,\"born\":0.2682308,\"nothing\":0.8625516,\"goodbye\":0.17146169,\"greeting\":0.96817183,\"birth\":1.2788506,\"come\":0.1623208,\"global\":0.4371151,\"it\":0.42951578,\"life\":1.5750692,\"thanks\":0.26481047,\"world\":4.7300377,\"tiny\":0.5462298,\"earth\":2.6555297,\"universe\":2.0308156,\"worldwide\":1.3903781,\"hello\":6.696973,\"so\":0.20279501,\"?\":0.67785245},\"id\":\"s1\"},\"_id\":\"1\",\"_score\":30.0029}\n"
+ "{\"_index\":\"my-nlp-index\",\"_source\":{\"passage_text\":\"Hi planet\",\"passage_embedding\":{\"hi\":4.338913,\"planets\":2.7755864,\"planet\":5.0969057,\"mars\":1.7405145,\"earth\":2.6087382,\"hello\":3.3210192},\"id\":\"s2\"},\"_id\":\"2\",\"_score\":16.480486}\n",
responseCaptor.getValue()
);
}
@Test
@SneakyThrows
public void testRunWithRuntimeExceptionDuringSearch() {
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
ragTool.setXContentRegistry(mockNamedXContentRegistry);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> actionListener = invocation.getArgument(1);
actionListener.onFailure(new RuntimeException("Failed to search index"));
return null;
}).when(client).search(any(), any());
ragTool.run(Map.of(INPUT_FIELD, TEST_QUERY_TEXT), listener);
verify(listener).onFailure(any(RuntimeException.class));
ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class);
verify(listener).onFailure(argumentCaptor.capture());
assertEquals("Failed to search index", argumentCaptor.getValue().getMessage());
}
@Test
@SneakyThrows
public void testRunWithRuntimeExceptionDuringExecute() {
NamedXContentRegistry mockNamedXContentRegistry = getQueryNamedXContentRegistry();
ragTool.setXContentRegistry(mockNamedXContentRegistry);
SearchResponse mockedSearchResponse = SearchResponse
.fromXContent(
JsonXContent.jsonXContent
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, mockedSearchResponseString)
);
doAnswer(invocation -> {
SearchRequest searchRequest = invocation.getArgument(0);
assertEquals((long) TEST_DOC_SIZE, (long) searchRequest.source().size());
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(mockedSearchResponse);
return null;
}).when(client).search(any(), any());
doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
actionListener.onFailure(new RuntimeException("Failed to run model " + TEST_INFERENCE_MODEL_ID));
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
ragTool.run(Map.of(INPUT_FIELD, TEST_QUERY_TEXT), listener);
verify(listener).onFailure(any(RuntimeException.class));
ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class);
verify(listener).onFailure(argumentCaptor.capture());
assertEquals("Failed to run model " + TEST_INFERENCE_MODEL_ID, argumentCaptor.getValue().getMessage());
}
@Test(expected = IllegalArgumentException.class)
public void testRunWithEmptyInput() {
ActionListener listener = mock(ActionListener.class);
ragTool.run(Map.of(INPUT_FIELD, ""), listener);
}
@Test
public void testFactoryNeuralQuery() {
RAGTool.Factory factoryMock = new RAGTool.Factory();
RAGTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
factoryMock.init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
String defaultDescription = factoryMock.getDefaultDescription();
assertEquals(RAGTool.DEFAULT_DESCRIPTION, defaultDescription);
assertEquals(factoryMock.getDefaultType(), RAGTool.TYPE);
assertEquals(factoryMock.getDefaultVersion(), null);
assertNotNull(RAGTool.Factory.getInstance());
params.put(VectorDBTool.NESTED_PATH_FIELD, TEST_NESTED_PATH);
RAGTool rAGtool1 = factoryMock.create(params);
VectorDBTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
params.put(COMMON_MODEL_ID_FIELD, TEST_EMBEDDING_MODEL_ID);
VectorDBTool queryTool = VectorDBTool.Factory.getInstance().create(params);
RAGTool rAGtool2 = new RAGTool(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY, TEST_INFERENCE_MODEL_ID, true, queryTool);
assertEquals(rAGtool1.getClient(), rAGtool2.getClient());
assertEquals(rAGtool1.getInferenceModelId(), rAGtool2.getInferenceModelId());
assertEquals(rAGtool1.getName(), rAGtool2.getName());
assertEquals(rAGtool1.getQueryTool().getDocSize(), rAGtool2.getQueryTool().getDocSize());
assertEquals(rAGtool1.getQueryTool().getIndex(), rAGtool2.getQueryTool().getIndex());
assertEquals(rAGtool1.getQueryTool().getSourceFields(), rAGtool2.getQueryTool().getSourceFields());
assertEquals(rAGtool1.getXContentRegistry(), rAGtool2.getXContentRegistry());
assertEquals(rAGtool1.getQueryType(), rAGtool2.getQueryType());
assertEquals(((VectorDBTool) rAGtool1.getQueryTool()).getNestedPath(), ((VectorDBTool) rAGtool2.getQueryTool()).getNestedPath());
}
@Test
public void testFactoryNeuralSparseQuery() {
RAGTool.Factory factoryMock = new RAGTool.Factory();
RAGTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
factoryMock.init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
String defaultDescription = factoryMock.getDefaultDescription();
assertEquals(RAGTool.DEFAULT_DESCRIPTION, defaultDescription);
assertNotNull(RAGTool.Factory.getInstance());
assertEquals(factoryMock.getDefaultType(), RAGTool.TYPE);
assertEquals(factoryMock.getDefaultVersion(), null);
params.put(NeuralSparseSearchTool.NESTED_PATH_FIELD, TEST_NESTED_PATH);
params.put("query_type", "neural_sparse");
RAGTool rAGtool1 = factoryMock.create(params);
NeuralSparseSearchTool.Factory.getInstance().init(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY);
NeuralSparseSearchTool queryTool = NeuralSparseSearchTool.Factory.getInstance().create(params);
RAGTool rAGtool2 = new RAGTool(client, TEST_XCONTENT_REGISTRY_FOR_NEURAL_QUERY, TEST_INFERENCE_MODEL_ID, true, queryTool);
assertEquals(rAGtool1.getClient(), rAGtool2.getClient());
assertEquals(rAGtool1.getInferenceModelId(), rAGtool2.getInferenceModelId());
assertEquals(rAGtool1.getName(), rAGtool2.getName());
assertEquals(rAGtool1.getQueryTool().getDocSize(), rAGtool2.getQueryTool().getDocSize());
assertEquals(rAGtool1.getQueryTool().getIndex(), rAGtool2.getQueryTool().getIndex());
assertEquals(rAGtool1.getQueryTool().getSourceFields(), rAGtool2.getQueryTool().getSourceFields());
assertEquals(rAGtool1.getXContentRegistry(), rAGtool2.getXContentRegistry());
assertEquals(rAGtool1.getQueryType(), rAGtool2.getQueryType());
assertEquals(
((NeuralSparseSearchTool) rAGtool1.getQueryTool()).getNestedPath(),
((NeuralSparseSearchTool) rAGtool2.getQueryTool()).getNestedPath()
);
}
private static NamedXContentRegistry getQueryNamedXContentRegistry() {
QueryBuilder matchAllQueryBuilder = new MatchAllQueryBuilder();
List<NamedXContentRegistry.Entry> entries = new ArrayList<>();
NamedXContentRegistry.Entry neural_query_entry = new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField("neural"),
(p, c) -> {
p.map();
return matchAllQueryBuilder;
}
);
entries.add(neural_query_entry);
NamedXContentRegistry.Entry neural_sparse_query_entry = new NamedXContentRegistry.Entry(
QueryBuilder.class,
new ParseField("neural_sparse"),
(p, c) -> {
p.map();
return matchAllQueryBuilder;
}
);
entries.add(neural_sparse_query_entry);
NamedXContentRegistry mockNamedXContentRegistry = new NamedXContentRegistry(entries);
return mockNamedXContentRegistry;
}
private static ModelTensorOutput getMlModelTensorOutput() {
ModelTensor modelTensor = ModelTensor.builder().dataAsMap(ImmutableMap.of("thought", "thought 1", "action", "action1")).build();
ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build();
ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build();
return mlModelTensorOutput;
}
}