Skip to content

Commit ab0d138

Browse files
Refactor code
1 parent 5f39dcf commit ab0d138

File tree

5 files changed

+26
-263
lines changed

5 files changed

+26
-263
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static TransportVersion def(int id) {
170170
public static final TransportVersion RESOURCE_DEPRECATION_CHECKS = def(8_836_00_0);
171171
public static final TransportVersion LINEAR_RETRIEVER_SUPPORT = def(8_837_00_0);
172172
public static final TransportVersion TIMEOUT_GET_PARAM_FOR_RESOLVE_CLUSTER = def(8_838_00_0);
173+
public static final TransportVersion ML_INFERENCE_IBM_WATSONX_RERANK_ADDED = def(8_839_00_0);
173174

174175
/*
175176
* STOP! READ THIS FIRST! No, really,

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/sender/IbmWatsonxRerankRequestManager.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.xpack.inference.external.response.ibmwatsonx.IbmWatsonxRankedResponseEntity;
2020
import org.elasticsearch.xpack.inference.services.ibmwatsonx.rerank.IbmWatsonxRerankModel;
2121

22+
import java.util.List;
2223
import java.util.Objects;
2324
import java.util.function.Supplier;
2425

@@ -40,7 +41,7 @@ public static IbmWatsonxRerankRequestManager of(IbmWatsonxRerankModel model, Thr
4041

4142
private final IbmWatsonxRerankModel model;
4243

43-
private IbmWatsonxRerankRequestManager(IbmWatsonxRerankModel model, ThreadPool threadPool) {
44+
public IbmWatsonxRerankRequestManager(IbmWatsonxRerankModel model, ThreadPool threadPool) {
4445
super(threadPool, model);
4546
this.model = model;
4647
}
@@ -54,8 +55,19 @@ public void execute(
5455
) {
5556
var rerankInput = QueryAndDocsInputs.of(inferenceInputs);
5657

57-
IbmWatsonxRerankRequest request = new IbmWatsonxRerankRequest(rerankInput.getQuery(), rerankInput.getChunks(), model);
58+
execute(new ExecutableInferenceRequest(requestSender,
59+
logger,
60+
getRerankRequest(rerankInput.getQuery(), rerankInput.getChunks(), model),
61+
HANDLER,
62+
hasRequestCompletedFunction,
63+
listener));
64+
}
5865

59-
execute(new ExecutableInferenceRequest(requestSender, logger, request, HANDLER, hasRequestCompletedFunction, listener));
66+
protected IbmWatsonxRerankRequest getRerankRequest(
67+
String query,
68+
List<String> chunks,
69+
IbmWatsonxRerankModel model
70+
) {
71+
return new IbmWatsonxRerankRequest(query, chunks, model);
6072
}
6173
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/request/ibmwatsonx/IbmWatsonxRerankRequest.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
import org.apache.http.HttpHeaders;
1111
import org.apache.http.client.methods.HttpPost;
12-
import org.apache.http.client.utils.URIBuilder;
1312
import org.apache.http.entity.ByteArrayEntity;
1413
import org.elasticsearch.common.Strings;
1514
import org.elasticsearch.xcontent.XContentType;
1615
import org.elasticsearch.xpack.inference.external.request.HttpRequest;
1716
import org.elasticsearch.xpack.inference.external.request.Request;
18-
import org.elasticsearch.xpack.inference.external.request.cohere.CohereUtils;
1917
import org.elasticsearch.xpack.inference.services.ibmwatsonx.rerank.IbmWatsonxRerankModel;
2018
import org.elasticsearch.xpack.inference.services.ibmwatsonx.rerank.IbmWatsonxRerankTaskSettings;
2119

@@ -31,7 +29,6 @@ public class IbmWatsonxRerankRequest implements IbmWatsonxRequest {
3129
private final List<String> input;
3230
private final IbmWatsonxRerankTaskSettings taskSettings;
3331
private final IbmWatsonxRerankModel model;
34-
private final String inferenceEntityId;
3532

3633
public IbmWatsonxRerankRequest(String query, List<String> input, IbmWatsonxRerankModel model) {
3734
Objects.requireNonNull(model);
@@ -40,7 +37,6 @@ public IbmWatsonxRerankRequest(String query, List<String> input, IbmWatsonxReran
4037
this.query = Objects.requireNonNull(query);
4138
taskSettings = model.getTaskSettings();
4239
this.model = model;
43-
inferenceEntityId = model.getInferenceEntityId();
4440
}
4541

4642
@Override
@@ -81,7 +77,7 @@ public void decorateWithAuth(HttpPost httpPost) {
8177

8278
@Override
8379
public String getInferenceEntityId() {
84-
return inferenceEntityId;
80+
return model.getInferenceEntityId();
8581
}
8682

8783
@Override
@@ -111,10 +107,4 @@ public boolean[] getTruncationInfo() {
111107
return null;
112108
}
113109

114-
public static URI buildDefaultUri() throws URISyntaxException {
115-
return new URIBuilder().setScheme("https")
116-
.setHost(CohereUtils.HOST)
117-
.setPathSegments(CohereUtils.VERSION_1, CohereUtils.RERANK_PATH)
118-
.build();
119-
}
120110
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/ibmwatsonx/IbmWatsonxServiceSettings.java

Lines changed: 0 additions & 242 deletions
This file was deleted.

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/ibmwatsonx/rerank/IbmWatsonxRerankServiceSettings.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@
4040
import static org.elasticsearch.xpack.inference.services.ServiceUtils.removeAsType;
4141
import static org.elasticsearch.xpack.inference.services.ibmwatsonx.IbmWatsonxServiceFields.API_VERSION;
4242
import static org.elasticsearch.xpack.inference.services.ibmwatsonx.IbmWatsonxServiceFields.PROJECT_ID;
43-
import static org.elasticsearch.xpack.inference.services.ibmwatsonx.IbmWatsonxServiceSettings.DEFAULT_RATE_LIMIT_SETTINGS;
44-
import static org.elasticsearch.xpack.inference.services.ibmwatsonx.IbmWatsonxServiceSettings.MODEL_ID;
43+
import static org.elasticsearch.xpack.inference.services.ServiceFields.MODEL_ID;
4544

4645
public class IbmWatsonxRerankServiceSettings extends FilteredXContentObject implements ServiceSettings, IbmWatsonxRateLimitServiceSettings {
4746
public static final String NAME = "ibm_watsonx_rerank_service_settings";
4847

49-
private static final Logger logger = LogManager.getLogger(IbmWatsonxRerankServiceSettings.class);
48+
/**
49+
* Rate limits are defined at
50+
* <a href="https://www.ibm.com/docs/en/watsonx/saas?topic=learning-watson-machine-plans">Watson Machine Learning plans</a>.
51+
* For Lite plan, you've 120 requests per minute.
52+
*/
53+
private static final RateLimitSettings DEFAULT_RATE_LIMIT_SETTINGS = new RateLimitSettings(120);
5054

5155
public static IbmWatsonxRerankServiceSettings fromMap(Map<String, Object> map, ConfigurationParseContext context) {
5256
ValidationException validationException = new ValidationException();
@@ -177,7 +181,7 @@ protected XContentBuilder toXContentFragmentOfExposedFields(XContentBuilder buil
177181

178182
@Override
179183
public TransportVersion getMinimalSupportedVersion() {
180-
return TransportVersions.V_8_14_0;
184+
return TransportVersions.ML_INFERENCE_IBM_WATSONX_RERANK_ADDED;
181185
}
182186

183187
@Override
@@ -189,9 +193,7 @@ public void writeTo(StreamOutput out) throws IOException {
189193
out.writeOptionalString(modelId);
190194
out.writeOptionalString(projectId);
191195

192-
if (out.getTransportVersion().onOrAfter(TransportVersions.V_8_15_0)) {
193-
rateLimitSettings.writeTo(out);
194-
}
196+
rateLimitSettings.writeTo(out);
195197
}
196198

197199
@Override

0 commit comments

Comments
 (0)