7
7
8
8
package org .elasticsearch .xpack .inference .services .elasticsearch ;
9
9
10
+ import org .elasticsearch .TransportVersion ;
10
11
import org .elasticsearch .common .ValidationException ;
11
12
import org .elasticsearch .common .io .stream .StreamInput ;
13
+ import org .elasticsearch .common .io .stream .StreamOutput ;
14
+ import org .elasticsearch .inference .ModelConfigurations ;
15
+ import org .elasticsearch .xcontent .XContentBuilder ;
12
16
import org .elasticsearch .xpack .core .ml .inference .assignment .AdaptiveAllocationsSettings ;
13
17
14
18
import java .io .IOException ;
19
+ import java .util .EnumSet ;
20
+ import java .util .Locale ;
15
21
import java .util .Map ;
16
22
23
+ import static org .elasticsearch .xpack .inference .services .ServiceUtils .extractOptionalEnum ;
24
+ import static org .elasticsearch .xpack .inference .services .ServiceUtils .extractOptionalPositiveInteger ;
25
+ import static org .elasticsearch .xpack .inference .services .elasticsearch .ElasticsearchInternalService .ELASTIC_RERANKER_CHUNKING ;
17
26
import static org .elasticsearch .xpack .inference .services .elasticsearch .ElasticsearchInternalService .RERANKER_ID ;
18
27
19
28
public class ElasticRerankerServiceSettings extends ElasticsearchInternalServiceSettings {
20
29
21
30
public static final String NAME = "elastic_reranker_service_settings" ;
22
31
32
+ public static final String LONG_DOCUMENT_STRATEGY = "long_document_strategy" ;
33
+ public static final String MAX_CHUNKS_PER_DOC = "max_chunks_per_doc" ;
34
+
35
+ private static final TransportVersion ELASTIC_RERANKER_CHUNKING_CONFIGURATION = TransportVersion .fromName (
36
+ "elastic_reranker_chunking_configuration"
37
+ );
38
+
39
+ private final LongDocumentStrategy longDocumentStrategy ;
40
+ private final Integer maxChunksPerDoc ;
41
+
23
42
public static ElasticRerankerServiceSettings defaultEndpointSettings () {
24
43
return new ElasticRerankerServiceSettings (null , 1 , RERANKER_ID , new AdaptiveAllocationsSettings (Boolean .TRUE , 0 , 32 ));
25
44
}
26
45
27
- public ElasticRerankerServiceSettings (ElasticsearchInternalServiceSettings other ) {
46
+ public ElasticRerankerServiceSettings (
47
+ ElasticsearchInternalServiceSettings other ,
48
+ LongDocumentStrategy longDocumentStrategy ,
49
+ Integer maxChunksPerDoc
50
+ ) {
28
51
super (other );
52
+ this .longDocumentStrategy = longDocumentStrategy ;
53
+ this .maxChunksPerDoc = maxChunksPerDoc ;
54
+
29
55
}
30
56
31
57
private ElasticRerankerServiceSettings (
@@ -35,10 +61,32 @@ private ElasticRerankerServiceSettings(
35
61
AdaptiveAllocationsSettings adaptiveAllocationsSettings
36
62
) {
37
63
super (numAllocations , numThreads , modelId , adaptiveAllocationsSettings , null );
64
+ this .longDocumentStrategy = null ;
65
+ this .maxChunksPerDoc = null ;
66
+ }
67
+
68
+ protected ElasticRerankerServiceSettings (
69
+ Integer numAllocations ,
70
+ int numThreads ,
71
+ String modelId ,
72
+ AdaptiveAllocationsSettings adaptiveAllocationsSettings ,
73
+ LongDocumentStrategy longDocumentStrategy ,
74
+ Integer maxChunksPerDoc
75
+ ) {
76
+ super (numAllocations , numThreads , modelId , adaptiveAllocationsSettings , null );
77
+ this .longDocumentStrategy = longDocumentStrategy ;
78
+ this .maxChunksPerDoc = maxChunksPerDoc ;
38
79
}
39
80
40
81
public ElasticRerankerServiceSettings (StreamInput in ) throws IOException {
41
82
super (in );
83
+ if (in .getTransportVersion ().supports (ELASTIC_RERANKER_CHUNKING_CONFIGURATION )) {
84
+ this .longDocumentStrategy = in .readOptionalEnum (LongDocumentStrategy .class );
85
+ this .maxChunksPerDoc = in .readOptionalInt ();
86
+ } else {
87
+ this .longDocumentStrategy = null ;
88
+ this .maxChunksPerDoc = null ;
89
+ }
42
90
}
43
91
44
92
/**
@@ -48,21 +96,93 @@ public ElasticRerankerServiceSettings(StreamInput in) throws IOException {
48
96
* {@link ValidationException} is thrown.
49
97
*
50
98
* @param map Source map containing the config
51
- * @return The builder
99
+ * @return Parsed and validated service settings
52
100
*/
53
- public static Builder fromRequestMap (Map <String , Object > map ) {
101
+ public static ElasticRerankerServiceSettings fromMap (Map <String , Object > map ) {
54
102
ValidationException validationException = new ValidationException ();
55
103
var baseSettings = ElasticsearchInternalServiceSettings .fromMap (map , validationException );
56
104
105
+ LongDocumentStrategy longDocumentStrategy = null ;
106
+ Integer maxChunksPerDoc = null ;
107
+ if (ELASTIC_RERANKER_CHUNKING .isEnabled ()) {
108
+ longDocumentStrategy = extractOptionalEnum (
109
+ map ,
110
+ LONG_DOCUMENT_STRATEGY ,
111
+ ModelConfigurations .SERVICE_SETTINGS ,
112
+ LongDocumentStrategy ::fromString ,
113
+ EnumSet .allOf (LongDocumentStrategy .class ),
114
+ validationException
115
+ );
116
+
117
+ maxChunksPerDoc = extractOptionalPositiveInteger (
118
+ map ,
119
+ MAX_CHUNKS_PER_DOC ,
120
+ ModelConfigurations .SERVICE_SETTINGS ,
121
+ validationException
122
+ );
123
+
124
+ if (maxChunksPerDoc != null && (longDocumentStrategy == null || longDocumentStrategy == LongDocumentStrategy .TRUNCATE )) {
125
+ validationException .addValidationError (
126
+ "The [" + MAX_CHUNKS_PER_DOC + "] setting requires [" + LONG_DOCUMENT_STRATEGY + "] to be set to [chunk]"
127
+ );
128
+ }
129
+ }
130
+
57
131
if (validationException .validationErrors ().isEmpty () == false ) {
58
132
throw validationException ;
59
133
}
60
134
61
- return baseSettings ;
135
+ return new ElasticRerankerServiceSettings (baseSettings .build (), longDocumentStrategy , maxChunksPerDoc );
136
+ }
137
+
138
+ public LongDocumentStrategy getLongDocumentStrategy () {
139
+ return longDocumentStrategy ;
140
+ }
141
+
142
+ public Integer getMaxChunksPerDoc () {
143
+ return maxChunksPerDoc ;
144
+ }
145
+
146
+ @ Override
147
+ public void writeTo (StreamOutput out ) throws IOException {
148
+ super .writeTo (out );
149
+ if (out .getTransportVersion ().supports (ELASTIC_RERANKER_CHUNKING_CONFIGURATION )) {
150
+ out .writeOptionalEnum (longDocumentStrategy );
151
+ out .writeOptionalInt (maxChunksPerDoc );
152
+ }
153
+ }
154
+
155
+ @ Override
156
+ public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
157
+ builder .startObject ();
158
+ addInternalSettingsToXContent (builder , params );
159
+ if (longDocumentStrategy != null ) {
160
+ builder .field (LONG_DOCUMENT_STRATEGY , longDocumentStrategy .strategyName );
161
+ }
162
+ if (maxChunksPerDoc != null ) {
163
+ builder .field (MAX_CHUNKS_PER_DOC , maxChunksPerDoc );
164
+ }
165
+ builder .endObject ();
166
+ return builder ;
62
167
}
63
168
64
169
@ Override
65
170
public String getWriteableName () {
66
171
return ElasticRerankerServiceSettings .NAME ;
67
172
}
173
+
174
+ public enum LongDocumentStrategy {
175
+ CHUNK ("chunk" ),
176
+ TRUNCATE ("truncate" );
177
+
178
+ public final String strategyName ;
179
+
180
+ LongDocumentStrategy (String strategyName ) {
181
+ this .strategyName = strategyName ;
182
+ }
183
+
184
+ public static LongDocumentStrategy fromString (String name ) {
185
+ return valueOf (name .trim ().toUpperCase (Locale .ROOT ));
186
+ }
187
+ }
68
188
}
0 commit comments