Search in sources :

Example 11 with MLTask

use of org.opensearch.ml.common.parameter.MLTask in project ml-commons by opensearch-project.

the class MLTrainAndPredictTaskRunner method startTrainAndPredictionTask.

/**
 * Start prediction task
 * @param request MLPredictionTaskRequest
 * @param listener Action listener
 */
public void startTrainAndPredictionTask(MLTrainingTaskRequest request, ActionListener<MLTaskResponse> listener) {
    MLInputDataType inputDataType = request.getMlInput().getInputDataset().getInputDataType();
    Instant now = Instant.now();
    MLTask mlTask = MLTask.builder().taskId(UUID.randomUUID().toString()).taskType(MLTaskType.TRAINING_AND_PREDICTION).inputType(inputDataType).functionName(request.getMlInput().getFunctionName()).state(MLTaskState.CREATED).workerNode(clusterService.localNode().getId()).createTime(now).lastUpdateTime(now).async(false).build();
    MLInput mlInput = request.getMlInput();
    if (mlInput.getInputDataset().getInputDataType().equals(MLInputDataType.SEARCH_QUERY)) {
        ActionListener<DataFrame> dataFrameActionListener = ActionListener.wrap(dataFrame -> {
            trainAndPredict(mlTask, dataFrame, request, listener);
        }, e -> {
            log.error("Failed to generate DataFrame from search query", e);
            handlePredictFailure(mlTask, listener, e, false);
        });
        mlInputDatasetHandler.parseSearchQueryInput(mlInput.getInputDataset(), new ThreadedActionListener<>(log, threadPool, TASK_THREAD_POOL, dataFrameActionListener, false));
    } else {
        DataFrame inputDataFrame = mlInputDatasetHandler.parseDataFrameInput(mlInput.getInputDataset());
        threadPool.executor(TASK_THREAD_POOL).execute(() -> {
            trainAndPredict(mlTask, inputDataFrame, request, listener);
        });
    }
}
Also used : MLInput(org.opensearch.ml.common.parameter.MLInput) Instant(java.time.Instant) MLInputDataType(org.opensearch.ml.common.dataset.MLInputDataType) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) MLTask(org.opensearch.ml.common.parameter.MLTask)

Aggregations

MLTask (org.opensearch.ml.common.parameter.MLTask)11 Instant (java.time.Instant)5 Log4j2 (lombok.extern.log4j.Log4j2)4 ActionListener (org.opensearch.action.ActionListener)4 Client (org.opensearch.client.Client)4 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 DataFrame (org.opensearch.ml.common.dataframe.DataFrame)4 MLInputDataType (org.opensearch.ml.common.dataset.MLInputDataType)4 MLInput (org.opensearch.ml.common.parameter.MLInput)4 IndexRequest (org.opensearch.action.index.IndexRequest)3 IndexResponse (org.opensearch.action.index.IndexResponse)3 WriteRequest (org.opensearch.action.support.WriteRequest)3 ToXContent (org.opensearch.common.xcontent.ToXContent)3 MLTaskState (org.opensearch.ml.common.parameter.MLTaskState)3 MLIndicesHandler (org.opensearch.ml.indices.MLIndicesHandler)3 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ActionListenerResponseHandler (org.opensearch.action.ActionListenerResponseHandler)2