Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ public static <T> T singleValue(Collection<T> collection) {
return collection.iterator().next();
}

public static Attribute getAttributeByName(Collection<Attribute> attributes, String name) {
return attributes.stream().filter(attr -> attr.name().equals(name)).findAny().orElse(null);
}

public static Map<String, Object> jsonEntityToMap(HttpEntity entity) throws IOException {
return entityToMap(entity, XContentType.JSON);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.elasticsearch.xpack.esql.plan.logical.Rename;
import org.elasticsearch.xpack.esql.plan.logical.RrfScoreEval;
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
import org.elasticsearch.xpack.esql.plan.logical.inference.Completion;
import org.elasticsearch.xpack.esql.plan.logical.inference.InferencePlan;
import org.elasticsearch.xpack.esql.plan.logical.inference.Rerank;
import org.elasticsearch.xpack.esql.plan.logical.join.Join;
Expand Down Expand Up @@ -488,6 +489,10 @@ protected LogicalPlan rule(LogicalPlan plan, AnalyzerContext context) {
return resolveAggregate(aggregate, childrenOutput);
}

if (plan instanceof Completion c) {
return resolveCompletion(c, childrenOutput);
}

if (plan instanceof Drop d) {
return resolveDrop(d, childrenOutput);
}
Expand Down Expand Up @@ -598,6 +603,34 @@ private Aggregate resolveAggregate(Aggregate aggregate, List<Attribute> children
return aggregate;
}

private LogicalPlan resolveCompletion(Completion p, List<Attribute> childrenOutput) {
Attribute targetField = p.targetField();
Expression prompt = p.prompt();

if (targetField instanceof UnresolvedAttribute ua) {
targetField = new ReferenceAttribute(ua.source(), ua.name(), TEXT);
}

if (prompt.resolved() == false) {
prompt = prompt.transformUp(UnresolvedAttribute.class, ua -> maybeResolveAttribute(ua, childrenOutput));
}

if (prompt.resolved() && DataType.isString(prompt.dataType()) == false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ We support only text as prompt.

prompt = new UnresolvedAttribute(
prompt.source(),
"unsupported",
LoggerMessageFormat.format(
null,
"prompt must be of type [{}] but is [{}]",
TEXT.typeName(),
prompt.dataType().typeName()
)
);
}

return new Completion(p.source(), p.child(), p.inferenceId(), prompt, targetField);
}

private LogicalPlan resolveMvExpand(MvExpand p, List<Attribute> childrenOutput) {
if (p.target() instanceof UnresolvedAttribute ua) {
Attribute resolved = maybeResolveAttribute(ua, childrenOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
import org.elasticsearch.xpack.esql.expression.function.fulltext.MatchOperator;
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Concat;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Substring;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.GreaterThan;
Expand All @@ -71,6 +72,7 @@
import org.elasticsearch.xpack.esql.plan.logical.Row;
import org.elasticsearch.xpack.esql.plan.logical.RrfScoreEval;
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
import org.elasticsearch.xpack.esql.plan.logical.inference.Completion;
import org.elasticsearch.xpack.esql.plan.logical.inference.Rerank;
import org.elasticsearch.xpack.esql.plan.logical.local.EsqlProject;
import org.elasticsearch.xpack.esql.plugin.EsqlPlugin;
Expand All @@ -92,9 +94,11 @@
import static org.elasticsearch.xpack.esql.EsqlTestUtils.as;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.configuration;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.emptyInferenceResolution;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.getAttributeByName;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.paramAsConstant;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.paramAsIdentifier;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.paramAsPattern;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.referenceAttribute;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.withDefaultLimitWarning;
import static org.elasticsearch.xpack.esql.analysis.Analyzer.NO_FIELDS;
import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.analyze;
Expand Down Expand Up @@ -3530,16 +3534,13 @@ public void testResolveRerankFields() {
Filter filter = as(drop.child(), Filter.class);
EsRelation relation = as(filter.child(), EsRelation.class);

Attribute titleAttribute = relation.output().stream().filter(attribute -> attribute.name().equals("title")).findFirst().get();
assertThat(titleAttribute, notNullValue());
Attribute titleAttribute = getAttributeByName(relation.output(), "title");
assertThat(getAttributeByName(relation.output(), "title"), notNullValue());

assertThat(rerank.queryText(), equalTo(string("italian food recipe")));
assertThat(rerank.inferenceId(), equalTo(string("reranking-inference-id")));
assertThat(rerank.rerankFields(), equalTo(List.of(alias("title", titleAttribute))));
assertThat(
rerank.scoreAttribute(),
equalTo(relation.output().stream().filter(attr -> attr.name().equals(MetadataAttribute.SCORE)).findFirst().get())
);
assertThat(rerank.scoreAttribute(), equalTo(getAttributeByName(relation.output(), MetadataAttribute.SCORE)));
}

{
Expand All @@ -3559,15 +3560,11 @@ public void testResolveRerankFields() {
assertThat(rerank.inferenceId(), equalTo(string("reranking-inference-id")));

assertThat(rerank.rerankFields(), hasSize(3));
Attribute titleAttribute = relation.output().stream().filter(attribute -> attribute.name().equals("title")).findFirst().get();
Attribute titleAttribute = getAttributeByName(relation.output(), "title");
assertThat(titleAttribute, notNullValue());
assertThat(rerank.rerankFields().get(0), equalTo(alias("title", titleAttribute)));

Attribute descriptionAttribute = relation.output()
.stream()
.filter(attribute -> attribute.name().equals("description"))
.findFirst()
.get();
Attribute descriptionAttribute = getAttributeByName(relation.output(), "description");
assertThat(descriptionAttribute, notNullValue());
Alias descriptionAlias = rerank.rerankFields().get(1);
assertThat(descriptionAlias.name(), equalTo("description"));
Expand All @@ -3576,13 +3573,11 @@ public void testResolveRerankFields() {
equalTo(List.of(descriptionAttribute, literal(0), literal(100)))
);

Attribute yearAttribute = relation.output().stream().filter(attribute -> attribute.name().equals("year")).findFirst().get();
Attribute yearAttribute = getAttributeByName(relation.output(), "year");
assertThat(yearAttribute, notNullValue());
assertThat(rerank.rerankFields().get(2), equalTo(alias("yearRenamed", yearAttribute)));
assertThat(
rerank.scoreAttribute(),
equalTo(relation.output().stream().filter(attr -> attr.name().equals(MetadataAttribute.SCORE)).findFirst().get())
);

assertThat(rerank.scoreAttribute(), equalTo(getAttributeByName(relation.output(), MetadataAttribute.SCORE)));
}

{
Expand Down Expand Up @@ -3614,11 +3609,7 @@ public void testResolveRerankScoreField() {
Filter filter = as(rerank.child(), Filter.class);
EsRelation relation = as(filter.child(), EsRelation.class);

Attribute metadataScoreAttribute = relation.output()
.stream()
.filter(attr -> attr.name().equals(MetadataAttribute.SCORE))
.findFirst()
.get();
Attribute metadataScoreAttribute = getAttributeByName(relation.output(), MetadataAttribute.SCORE);
assertThat(rerank.scoreAttribute(), equalTo(metadataScoreAttribute));
assertThat(rerank.output(), hasItem(metadataScoreAttribute));
}
Expand All @@ -3642,6 +3633,117 @@ public void testResolveRerankScoreField() {
}
}

public void testResolveCompletionInferenceId() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ The code that does the inference resolution is from the #123074 PR.

assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

LogicalPlan plan = analyze("""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `completion-inference-id`
""", "mapping-books.json");
Completion completion = as(as(plan, Limit.class).child(), Completion.class);
assertThat(completion.inferenceId(), equalTo(string("completion-inference-id")));
}

public void testResolveCompletionInferenceIdInvalidTaskType() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

assertError(
"""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `reranking-inference-id`
""",
"mapping-books.json",
new QueryParams(),
"cannot use inference endpoint [reranking-inference-id] with task type [rerank] within a Completion command."
+ " Only inference endpoints with the task type [completion] are supported"
);
}

public void testResolveCompletionInferenceMissingInferenceId() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

assertError("""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `unknown-inference-id`
""", "mapping-books.json", new QueryParams(), "unresolved inference [unknown-inference-id]");
}

public void testResolveCompletionInferenceIdResolutionError() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

assertError("""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `error-inference-id`
""", "mapping-books.json", new QueryParams(), "error with inference resolution");
}

public void testResolveCompletionTargetField() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

LogicalPlan plan = analyze("""
FROM books METADATA _score
| COMPLETION CONCAT(
"Translate the following text in French\\n", description) WITH `completion-inference-id` AS translation
""", "mapping-books.json");

Completion completion = as(as(plan, Limit.class).child(), Completion.class);
assertThat(completion.targetField(), equalTo(referenceAttribute("translation", DataType.TEXT)));
}

public void testResolveCompletionDefaultTargetField() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

LogicalPlan plan = analyze("""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `completion-inference-id`
""", "mapping-books.json");

Completion completion = as(as(plan, Limit.class).child(), Completion.class);
assertThat(completion.targetField(), equalTo(referenceAttribute("completion", DataType.TEXT)));
}

public void testResolveCompletionPrompt() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

LogicalPlan plan = analyze("""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `completion-inference-id`
""", "mapping-books.json");

Completion completion = as(as(plan, Limit.class).child(), Completion.class);
EsRelation esRelation = as(completion.child(), EsRelation.class);

assertThat(
as(completion.prompt(), Concat.class).children(),
equalTo(List.of(string("Translate the following text in French\n"), getAttributeByName(esRelation.output(), "description")))
);
}

public void testResolveCompletionPromptInvalidType() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

assertError("""
FROM books METADATA _score
| COMPLETION LENGTH(description) WITH `completion-inference-id`
""", "mapping-books.json", new QueryParams(), "prompt must be of type [text] but is [integer]");
}

public void testResolveCompletionOutputField() {
assumeTrue("Requires COMPLETION command", EsqlCapabilities.Cap.COMPLETION.isEnabled());

LogicalPlan plan = analyze("""
FROM books METADATA _score
| COMPLETION CONCAT("Translate the following text in French\\n", description) WITH `completion-inference-id` AS description
""", "mapping-books.json");

Completion completion = as(as(plan, Limit.class).child(), Completion.class);
assertThat(completion.targetField(), equalTo(referenceAttribute("description", DataType.TEXT)));

EsRelation esRelation = as(completion.child(), EsRelation.class);
assertThat(getAttributeByName(completion.output(), "description"), equalTo(completion.targetField()));
assertThat(getAttributeByName(esRelation.output(), "description"), not(equalTo(completion.targetField())));
}

@Override
protected IndexAnalyzers createDefaultIndexAnalyzers() {
return super.createDefaultIndexAnalyzers();
Expand Down