forked from opensearch-project/skills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorDBTool.java
174 lines (153 loc) · 5.89 KB
/
VectorDBTool.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.agent.tools;
import static org.opensearch.agent.tools.utils.CommonConstants.COMMON_MODEL_ID_FIELD;
import static org.opensearch.ml.common.utils.StringUtils.gson;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.ml.common.spi.tools.ToolAnnotation;
import org.opensearch.ml.common.spi.tools.WithModelTool;
import org.opensearch.transport.client.Client;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
/**
* This tool supports neural search with embedding models and knn index.
*/
@Log4j2
@Getter
@Setter
@ToolAnnotation(VectorDBTool.TYPE)
public class VectorDBTool extends AbstractRetrieverTool implements WithModelTool {
public static final String TYPE = "VectorDBTool";
public static String DEFAULT_DESCRIPTION =
"Use this tool to performs knn-based dense retrieval. It takes 1 argument named input which is a string query for dense retrieval. The tool returns the dense retrieval results for the query.";
public static final String EMBEDDING_FIELD = "embedding_field";
public static final String K_FIELD = "k";
public static final Integer DEFAULT_K = 10;
public static final String NESTED_PATH_FIELD = "nested_path";
private String name = TYPE;
private String modelId;
private String embeddingField;
private Integer k;
private String nestedPath;
@Builder
public VectorDBTool(
Client client,
NamedXContentRegistry xContentRegistry,
String index,
String embeddingField,
String[] sourceFields,
Integer docSize,
String modelId,
Integer k,
String nestedPath
) {
super(client, xContentRegistry, index, sourceFields, docSize);
this.modelId = modelId;
this.embeddingField = embeddingField;
this.k = k;
this.nestedPath = nestedPath;
}
@Override
protected String getQueryBody(String queryText) {
if (StringUtils.isBlank(embeddingField) || StringUtils.isBlank(modelId)) {
throw new IllegalArgumentException(
"Parameter [" + EMBEDDING_FIELD + "] and [" + COMMON_MODEL_ID_FIELD + "] can not be null or empty."
);
}
Map<String, Object> queryBody;
if (StringUtils.isBlank(nestedPath)) {
queryBody = Map
.of("query", Map.of("neural", Map.of(embeddingField, Map.of("query_text", queryText, "model_id", modelId, "k", k))));
} else {
queryBody = Map
.of(
"query",
Map
.of(
"nested",
Map
.of(
"path",
nestedPath,
"score_mode",
"max",
"query",
Map.of("neural", Map.of(embeddingField, Map.of("query_text", queryText, "model_id", modelId, "k", k)))
)
)
);
}
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> gson.toJson(queryBody));
} catch (PrivilegedActionException e) {
throw new RuntimeException(e);
}
}
@Override
public String getType() {
return TYPE;
}
public static class Factory extends AbstractRetrieverTool.Factory<VectorDBTool> implements WithModelTool.Factory<VectorDBTool> {
private static VectorDBTool.Factory INSTANCE;
public static VectorDBTool.Factory getInstance() {
if (INSTANCE != null) {
return INSTANCE;
}
synchronized (VectorDBTool.class) {
if (INSTANCE != null) {
return INSTANCE;
}
INSTANCE = new VectorDBTool.Factory();
return INSTANCE;
}
}
@Override
public VectorDBTool create(Map<String, Object> params) {
String index = (String) params.get(INDEX_FIELD);
String embeddingField = (String) params.get(EMBEDDING_FIELD);
String[] sourceFields = gson.fromJson((String) params.get(SOURCE_FIELD), String[].class);
String modelId = (String) params.get(COMMON_MODEL_ID_FIELD);
Integer docSize = params.containsKey(DOC_SIZE_FIELD) ? Integer.parseInt((String) params.get(DOC_SIZE_FIELD)) : DEFAULT_DOC_SIZE;
Integer k = params.containsKey(K_FIELD) ? Integer.parseInt((String) params.get(K_FIELD)) : DEFAULT_K;
String nestedPath = (String) params.get(NESTED_PATH_FIELD);
return VectorDBTool
.builder()
.client(client)
.xContentRegistry(xContentRegistry)
.index(index)
.embeddingField(embeddingField)
.sourceFields(sourceFields)
.modelId(modelId)
.docSize(docSize)
.k(k)
.nestedPath(nestedPath)
.build();
}
@Override
public String getDefaultType() {
return TYPE;
}
@Override
public String getDefaultVersion() {
return null;
}
@Override
public String getDefaultDescription() {
return DEFAULT_DESCRIPTION;
}
@Override
public List<String> getAllModelKeys() {
return List.of(COMMON_MODEL_ID_FIELD);
}
}
}