14
14
import java .util .function .Consumer ;
15
15
import java .util .stream .Collectors ;
16
16
17
+ import lombok .Builder ;
18
+ import lombok .Getter ;
19
+ import lombok .Singular ;
17
20
import org .opensearch .common .CheckedConsumer ;
18
21
import org .opensearch .common .cache .Cache ;
19
22
import org .opensearch .common .cache .CacheBuilder ;
47
50
@ Log4j2
48
51
public class MLCommonsClientAccessor {
49
52
53
+ public static final int MAXIMUM_CACHE_ENTRIES = 10_000 ;
54
+
50
55
/**
51
56
* Inference parameters for calls to the MLCommons client.
52
57
*/
58
+ @ Getter
59
+ @ Builder
53
60
public static class InferenceRequest {
54
61
55
62
private static final List <String > DEFAULT_TARGET_RESPONSE_FILTERS = List .of ("sentence_embedding" );
56
63
57
- private final String modelId ;
58
- private final List <String > inputTexts ;
59
- private final MLAlgoParams mlAlgoParams ;
60
- private final List <String > targetResponseFilters ;
61
- private final Map <String , String > inputObjects ;
62
- private final String queryText ;
64
+ private final String modelId ; // required
65
+ @ Singular
66
+ private List <String > inputTexts ;
67
+ private MLAlgoParams mlAlgoParams ;
68
+ private List <String > targetResponseFilters ;
69
+ private Map <String , String > inputObjects ;
70
+ private String queryText ;
63
71
64
72
public InferenceRequest (
65
73
@ NonNull String modelId ,
@@ -76,124 +84,12 @@ public InferenceRequest(
76
84
this .inputObjects = inputObjects ;
77
85
this .queryText = queryText ;
78
86
}
79
-
80
- public String getModelId () {
81
- return modelId ;
82
- }
83
-
84
- public List <String > getInputTexts () {
85
- return inputTexts ;
86
- }
87
-
88
- public MLAlgoParams getMlAlgoParams () {
89
- return mlAlgoParams ;
90
- }
91
-
92
- public List <String > getTargetResponseFilters () {
93
- return targetResponseFilters ;
94
- }
95
-
96
- public Map <String , String > getInputObjects () {
97
- return inputObjects ;
98
- }
99
-
100
- public String getQueryText () {
101
- return queryText ;
102
- }
103
-
104
- /**
105
- * Builder for {@link InferenceRequest}. Supports fluent construction of the request object.
106
- */
107
- public static class Builder {
108
-
109
- private final String modelId ;
110
- private List <String > inputTexts ;
111
- private MLAlgoParams mlAlgoParams ;
112
- private List <String > targetResponseFilters ;
113
- private Map <String , String > inputObjects ;
114
- private String queryText ;
115
-
116
- /**
117
- * @param modelId the model id to use for inference
118
- */
119
- public Builder (String modelId ) {
120
- this .modelId = modelId ;
121
- }
122
-
123
- /**
124
- * @param inputTexts a {@link List} of input texts to use for inference
125
- * @return this builder
126
- */
127
- public Builder inputTexts (List <String > inputTexts ) {
128
- this .inputTexts = inputTexts ;
129
- return this ;
130
- }
131
-
132
- /**
133
- * @param inputText an input text to add to the list of input texts. Repeated calls will add
134
- * more input texts.
135
- * @return this builder
136
- */
137
- public Builder inputText (String inputText ) {
138
- if (this .inputTexts != null ) {
139
- this .inputTexts .add (inputText );
140
- } else {
141
- this .inputTexts = new ArrayList <>();
142
- this .inputTexts .add (inputText );
143
- }
144
- return this ;
145
- }
146
-
147
- /**
148
- * @param mlAlgoParams the {@link MLAlgoParams} to use for inference.
149
- * @return this builder
150
- */
151
- public Builder mlAlgoParams (MLAlgoParams mlAlgoParams ) {
152
- this .mlAlgoParams = mlAlgoParams ;
153
- return this ;
154
- }
155
-
156
- /**
157
- * @param targetResponseFilters a {@link List} of target response filters to use for
158
- * inference
159
- * @return this builder
160
- */
161
- public Builder targetResponseFilters (List <String > targetResponseFilters ) {
162
- this .targetResponseFilters = targetResponseFilters ;
163
- return this ;
164
- }
165
-
166
- /**
167
- * @param inputObjects {@link Map} of {@link String}, {@link String} on which inference needs
168
- * to happen
169
- * @return this builder
170
- */
171
- public Builder inputObjects (Map <String , String > inputObjects ) {
172
- this .inputObjects = inputObjects ;
173
- return this ;
174
- }
175
-
176
- /**
177
- * @param queryText the query text to use for similarity inference
178
- * @return this builder
179
- */
180
- public Builder queryText (String queryText ) {
181
- this .queryText = queryText ;
182
- return this ;
183
- }
184
-
185
- /**
186
- * @return a new {@link InferenceRequest} object with the parameters set in this builder
187
- */
188
- public InferenceRequest build () {
189
- return new InferenceRequest (modelId , inputTexts , mlAlgoParams , targetResponseFilters , inputObjects , queryText );
190
- }
191
-
192
- }
193
87
}
194
88
195
89
private final MachineLearningNodeClient mlClient ;
196
- private final Cache <String , Boolean > modelAsymmetryCache = CacheBuilder .<String , Boolean >builder ().setMaximumWeight (10_000 ).build ();
90
+ private final Cache <String , Boolean > modelAsymmetryCache = CacheBuilder .<String , Boolean >builder ()
91
+ .setMaximumWeight (MAXIMUM_CACHE_ENTRIES )
92
+ .build ();
197
93
198
94
/**
199
95
* Wrapper around {@link #inferenceSentencesMap} that expects a single input text and produces a
0 commit comments