|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.ml.action; |
| 7 | + |
| 8 | +import org.elasticsearch.action.ActionRequest; |
| 9 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 10 | +import org.elasticsearch.action.ActionResponse; |
| 11 | +import org.elasticsearch.action.ActionType; |
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 14 | +import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; |
| 15 | +import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; |
| 16 | +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.InferenceParams; |
| 17 | +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +public class InferModelAction extends ActionType<InferModelAction.Response> { |
| 27 | + |
| 28 | + public static final InferModelAction INSTANCE = new InferModelAction(); |
| 29 | + public static final String NAME = "cluster:admin/xpack/ml/infer"; |
| 30 | + |
| 31 | + private InferModelAction() { |
| 32 | + super(NAME, Response::new); |
| 33 | + } |
| 34 | + |
| 35 | + public static class Request extends ActionRequest { |
| 36 | + |
| 37 | + private final String modelId; |
| 38 | + private final long modelVersion; |
| 39 | + private final List<Map<String, Object>> objectsToInfer; |
| 40 | + private final InferenceParams params; |
| 41 | + |
| 42 | + public Request(String modelId, long modelVersion) { |
| 43 | + this(modelId, modelVersion, Collections.emptyList(), InferenceParams.EMPTY_PARAMS); |
| 44 | + } |
| 45 | + |
| 46 | + public Request(String modelId, long modelVersion, List<Map<String, Object>> objectsToInfer, InferenceParams inferenceParams) { |
| 47 | + this.modelId = ExceptionsHelper.requireNonNull(modelId, TrainedModelConfig.MODEL_ID); |
| 48 | + this.modelVersion = modelVersion; |
| 49 | + this.objectsToInfer = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(objectsToInfer, "objects_to_infer")); |
| 50 | + this.params = inferenceParams == null ? InferenceParams.EMPTY_PARAMS : inferenceParams; |
| 51 | + } |
| 52 | + |
| 53 | + public Request(String modelId, long modelVersion, Map<String, Object> objectToInfer, InferenceParams params) { |
| 54 | + this(modelId, |
| 55 | + modelVersion, |
| 56 | + Arrays.asList(ExceptionsHelper.requireNonNull(objectToInfer, "objects_to_infer")), |
| 57 | + params); |
| 58 | + } |
| 59 | + |
| 60 | + public Request(StreamInput in) throws IOException { |
| 61 | + super(in); |
| 62 | + this.modelId = in.readString(); |
| 63 | + this.modelVersion = in.readVLong(); |
| 64 | + this.objectsToInfer = Collections.unmodifiableList(in.readList(StreamInput::readMap)); |
| 65 | + this.params = new InferenceParams(in); |
| 66 | + } |
| 67 | + |
| 68 | + public String getModelId() { |
| 69 | + return modelId; |
| 70 | + } |
| 71 | + |
| 72 | + public long getModelVersion() { |
| 73 | + return modelVersion; |
| 74 | + } |
| 75 | + |
| 76 | + public List<Map<String, Object>> getObjectsToInfer() { |
| 77 | + return objectsToInfer; |
| 78 | + } |
| 79 | + |
| 80 | + public InferenceParams getParams() { |
| 81 | + return params; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public ActionRequestValidationException validate() { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void writeTo(StreamOutput out) throws IOException { |
| 91 | + super.writeTo(out); |
| 92 | + out.writeString(modelId); |
| 93 | + out.writeVLong(modelVersion); |
| 94 | + out.writeCollection(objectsToInfer, StreamOutput::writeMap); |
| 95 | + params.writeTo(out); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean equals(Object o) { |
| 100 | + if (this == o) return true; |
| 101 | + if (o == null || getClass() != o.getClass()) return false; |
| 102 | + InferModelAction.Request that = (InferModelAction.Request) o; |
| 103 | + return Objects.equals(modelId, that.modelId) |
| 104 | + && Objects.equals(modelVersion, that.modelVersion) |
| 105 | + && Objects.equals(params, that.params) |
| 106 | + && Objects.equals(objectsToInfer, that.objectsToInfer); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int hashCode() { |
| 111 | + return Objects.hash(modelId, modelVersion, objectsToInfer, params); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + public static class Response extends ActionResponse { |
| 117 | + |
| 118 | + private final List<InferenceResults> inferenceResults; |
| 119 | + |
| 120 | + public Response(List<InferenceResults> inferenceResults) { |
| 121 | + super(); |
| 122 | + this.inferenceResults = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(inferenceResults, "inferenceResults")); |
| 123 | + } |
| 124 | + |
| 125 | + public Response(StreamInput in) throws IOException { |
| 126 | + super(in); |
| 127 | + this.inferenceResults = Collections.unmodifiableList(in.readNamedWriteableList(InferenceResults.class)); |
| 128 | + } |
| 129 | + |
| 130 | + public List<InferenceResults> getInferenceResults() { |
| 131 | + return inferenceResults; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void writeTo(StreamOutput out) throws IOException { |
| 136 | + out.writeNamedWriteableList(inferenceResults); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean equals(Object o) { |
| 141 | + if (this == o) return true; |
| 142 | + if (o == null || getClass() != o.getClass()) return false; |
| 143 | + InferModelAction.Response that = (InferModelAction.Response) o; |
| 144 | + return Objects.equals(inferenceResults, that.inferenceResults); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public int hashCode() { |
| 149 | + return Objects.hash(inferenceResults); |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | +} |
0 commit comments