|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.inference; |
| 9 | + |
| 10 | +import org.apache.lucene.util.BytesRef; |
| 11 | +import org.apache.lucene.util.BytesRefBuilder; |
| 12 | +import org.elasticsearch.compute.data.Block; |
| 13 | +import org.elasticsearch.compute.data.BlockFactory; |
| 14 | +import org.elasticsearch.compute.data.BytesRefBlock; |
| 15 | +import org.elasticsearch.compute.data.Page; |
| 16 | +import org.elasticsearch.compute.operator.DriverContext; |
| 17 | +import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator; |
| 18 | +import org.elasticsearch.compute.operator.Operator; |
| 19 | +import org.elasticsearch.core.Releasables; |
| 20 | +import org.elasticsearch.inference.TaskType; |
| 21 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 22 | +import org.elasticsearch.xpack.core.inference.results.ChatCompletionResults; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.NoSuchElementException; |
| 26 | + |
| 27 | +public class CompletionOperator extends InferenceOperator<ChatCompletionResults> { |
| 28 | + |
| 29 | + public record Factory(InferenceRunner inferenceRunner, String inferenceId, ExpressionEvaluator.Factory promptEvaluatorFactory) |
| 30 | + implements |
| 31 | + OperatorFactory { |
| 32 | + @Override |
| 33 | + public String describe() { |
| 34 | + return "Completion[inference_id=[" + inferenceId + "]]"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Operator get(DriverContext driverContext) { |
| 39 | + return new CompletionOperator(driverContext, inferenceRunner, inferenceId, promptEvaluatorFactory.get(driverContext)); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private final ExpressionEvaluator promptEvaluator; |
| 44 | + private final BlockFactory blockFactory; |
| 45 | + |
| 46 | + public CompletionOperator( |
| 47 | + DriverContext driverContext, |
| 48 | + InferenceRunner inferenceRunner, |
| 49 | + String inferenceId, |
| 50 | + ExpressionEvaluator promptEvaluator |
| 51 | + ) { |
| 52 | + super(driverContext, inferenceRunner, inferenceId); |
| 53 | + this.promptEvaluator = promptEvaluator; |
| 54 | + this.blockFactory = driverContext.blockFactory(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void doClose() { |
| 59 | + Releasables.closeExpectNoException(promptEvaluator); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String toString() { |
| 64 | + return "CompletionOperator[inference_id=[" + inferenceId() + "]]"; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected RequestIterator requests(Page inputPage) { |
| 69 | + return new InferenceOperator.RequestIterator() { |
| 70 | + private final BytesRefBlock promptBlock = (BytesRefBlock) promptEvaluator.eval(inputPage); |
| 71 | + private BytesRef readBuffer = new BytesRef(); |
| 72 | + private int currentPos = 0; |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean hasNext() { |
| 76 | + return currentPos < promptBlock.getPositionCount(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public InferenceAction.Request next() { |
| 81 | + if (hasNext() == false) { |
| 82 | + throw new NoSuchElementException(); |
| 83 | + } |
| 84 | + int pos = currentPos++; |
| 85 | + |
| 86 | + if (promptBlock.isNull(pos)) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + StringBuilder promptBuilder = new StringBuilder(); |
| 91 | + for (int valueIndex = 0; valueIndex < promptBlock.getValueCount(pos); valueIndex++) { |
| 92 | + readBuffer = promptBlock.getBytesRef(promptBlock.getFirstValueIndex(pos) + valueIndex, readBuffer); |
| 93 | + promptBuilder.append(readBuffer.utf8ToString()).append("\n"); |
| 94 | + } |
| 95 | + |
| 96 | + return inferenceRequest(promptBuilder.toString()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void close() { |
| 101 | + promptBlock.allowPassingToDifferentDriver(); |
| 102 | + Releasables.closeExpectNoException(promptBlock); |
| 103 | + } |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected OutputBuilder<ChatCompletionResults> outputBuilder(Page inputPage) { |
| 109 | + return new InferenceOperator.OutputBuilder<>() { |
| 110 | + private final BytesRefBlock.Builder outputBlockBuilder = blockFactory.newBytesRefBlockBuilder(inputPage.getPositionCount()); |
| 111 | + private final BytesRefBuilder bytesRefBuilder = new BytesRefBuilder(); |
| 112 | + |
| 113 | + @Override |
| 114 | + public void close() { |
| 115 | + Releasables.closeExpectNoException(outputBlockBuilder); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onInferenceResults(ChatCompletionResults completionResults) { |
| 120 | + if (completionResults == null || completionResults.getResults().isEmpty()) { |
| 121 | + outputBlockBuilder.appendNull(); |
| 122 | + } else { |
| 123 | + outputBlockBuilder.beginPositionEntry(); |
| 124 | + for (ChatCompletionResults.Result rankedDocsResult : completionResults.getResults()) { |
| 125 | + bytesRefBuilder.copyChars(rankedDocsResult.content()); |
| 126 | + outputBlockBuilder.appendBytesRef(bytesRefBuilder.get()); |
| 127 | + bytesRefBuilder.clear(); |
| 128 | + } |
| 129 | + outputBlockBuilder.endPositionEntry(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + protected Class<ChatCompletionResults> inferenceResultsClass() { |
| 135 | + return ChatCompletionResults.class; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public Page buildOutput() { |
| 140 | + Block outputBlock = outputBlockBuilder.build(); |
| 141 | + assert outputBlock.getPositionCount() == inputPage.getPositionCount(); |
| 142 | + return inputPage.appendBlock(outputBlock); |
| 143 | + } |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + private InferenceAction.Request inferenceRequest(String prompt) { |
| 148 | + return InferenceAction.Request.builder(inferenceId(), TaskType.COMPLETION).setInput(List.of(prompt)).build(); |
| 149 | + } |
| 150 | +} |
0 commit comments