-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add text embedding processor #1007
Merged
reta
merged 5 commits into
opensearch-project:main
from
miguel-vila:add-text-embedding-processor
Jun 3, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
...-client/src/main/java/org/opensearch/client/opensearch/ingest/TextEmbeddingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.ingest; | ||
|
||
import jakarta.json.stream.JsonGenerator; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
import org.opensearch.client.json.JsonpDeserializable; | ||
import org.opensearch.client.json.JsonpDeserializer; | ||
import org.opensearch.client.json.JsonpMapper; | ||
import org.opensearch.client.json.ObjectBuilderDeserializer; | ||
import org.opensearch.client.json.ObjectDeserializer; | ||
import org.opensearch.client.util.ApiTypeHelper; | ||
import org.opensearch.client.util.ObjectBuilder; | ||
|
||
// typedef: ingest._types.TextEmbeddingProcessor | ||
|
||
@JsonpDeserializable | ||
public class TextEmbeddingProcessor extends ProcessorBase implements ProcessorVariant { | ||
private final String modelId; | ||
|
||
private final Map<String, String> fieldMap; | ||
|
||
@Nullable | ||
private final String description; | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
private TextEmbeddingProcessor(Builder builder) { | ||
super(builder); | ||
|
||
this.modelId = ApiTypeHelper.requireNonNull(builder.modelId, this, "modelId"); | ||
this.fieldMap = ApiTypeHelper.unmodifiableRequired(builder.fieldMap, this, "fieldMap"); | ||
this.description = builder.description; | ||
|
||
} | ||
|
||
public static TextEmbeddingProcessor of(Function<Builder, ObjectBuilder<TextEmbeddingProcessor>> fn) { | ||
return fn.apply(new Builder()).build(); | ||
} | ||
|
||
/** | ||
* Processor variant kind. | ||
*/ | ||
@Override | ||
public Processor.Kind _processorKind() { | ||
return Processor.Kind.Inference; | ||
} | ||
|
||
/** | ||
* Required - API name: {@code model_id} | ||
*/ | ||
public final String modelId() { | ||
return this.modelId; | ||
} | ||
|
||
/** | ||
* API name: {@code field_map} | ||
*/ | ||
public final Map<String, String> fieldMap() { | ||
return this.fieldMap; | ||
} | ||
|
||
/** | ||
* API name: {@code description} | ||
*/ | ||
@Nullable | ||
public final String description() { | ||
return this.description; | ||
} | ||
|
||
protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { | ||
|
||
super.serializeInternal(generator, mapper); | ||
generator.writeKey("model_id"); | ||
generator.write(this.modelId); | ||
|
||
if (ApiTypeHelper.isDefined(this.fieldMap)) { | ||
generator.writeKey("field_map"); | ||
generator.writeStartObject(); | ||
for (Map.Entry<String, String> item0 : this.fieldMap.entrySet()) { | ||
generator.writeKey(item0.getKey()); | ||
generator.write(item0.getValue()); | ||
} | ||
generator.writeEnd(); | ||
|
||
} | ||
if (this.description != null) { | ||
generator.writeKey("description"); | ||
generator.write(this.description); | ||
} | ||
|
||
} | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Builder for {@link TextEmbeddingProcessor}. | ||
*/ | ||
|
||
public static class Builder extends ProcessorBase.AbstractBuilder<Builder> implements ObjectBuilder<TextEmbeddingProcessor> { | ||
private String modelId; | ||
|
||
@Nullable | ||
private Map<String, String> fieldMap; | ||
|
||
@Nullable | ||
private String description; | ||
|
||
/** | ||
* Required - API name: {@code model_id} | ||
*/ | ||
public final Builder modelId(String value) { | ||
this.modelId = value; | ||
return this; | ||
} | ||
|
||
/** | ||
* API name: {@code field_map} | ||
* <p> | ||
* Adds all entries of <code>map</code> to <code>fieldMap</code>. | ||
*/ | ||
public final Builder fieldMap(Map<String, String> map) { | ||
this.fieldMap = _mapPutAll(this.fieldMap, map); | ||
return this; | ||
} | ||
|
||
/** | ||
* API name: {@code field_map} | ||
* <p> | ||
* Adds an entry to <code>fieldMap</code>. | ||
*/ | ||
public final Builder fieldMap(String key, String value) { | ||
this.fieldMap = _mapPut(this.fieldMap, key, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* API name: {@code description} | ||
*/ | ||
public final Builder description(@Nullable String value) { | ||
this.description = value; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected Builder self() { | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a {@link TextEmbeddingProcessor}. | ||
* | ||
* @throws NullPointerException | ||
* if some of the required fields are null. | ||
*/ | ||
public TextEmbeddingProcessor build() { | ||
_checkSingleUse(); | ||
|
||
return new TextEmbeddingProcessor(this); | ||
} | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Json deserializer for {@link TextEmbeddingProcessor} | ||
*/ | ||
public static final JsonpDeserializer<TextEmbeddingProcessor> _DESERIALIZER = ObjectBuilderDeserializer.lazy( | ||
Builder::new, | ||
TextEmbeddingProcessor::setupTextEmbeddingProcessorDeserializer | ||
); | ||
|
||
protected static void setupTextEmbeddingProcessorDeserializer(ObjectDeserializer<TextEmbeddingProcessor.Builder> op) { | ||
ProcessorBase.setupProcessorBaseDeserializer(op); | ||
op.add(Builder::modelId, JsonpDeserializer.stringDeserializer(), "model_id"); | ||
op.add(Builder::fieldMap, JsonpDeserializer.stringMapDeserializer(JsonpDeserializer.stringDeserializer()), "field_map"); | ||
op.add(Builder::description, JsonpDeserializer.stringDeserializer(), "description"); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...ent/src/test/java/org/opensearch/client/opensearch/ingest/TextEmbeddingProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.ingest; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
import org.opensearch.client.opensearch.model.ModelTestCase; | ||
|
||
public class TextEmbeddingProcessorTest extends ModelTestCase { | ||
|
||
private static final Map<String, String> baseFieldMap = new HashMap<>(); | ||
static { | ||
baseFieldMap.put("input_field", "vector_field"); | ||
} | ||
|
||
private static TextEmbeddingProcessor.Builder baseTextEmbeddingProcessor() { | ||
return new TextEmbeddingProcessor.Builder().modelId("modelId").fieldMap(baseFieldMap).tag("some-tag"); | ||
} | ||
|
||
@Test | ||
public void testJsonRoundtripWithDescription() { | ||
Processor processor = new Processor.Builder().textEmbedding( | ||
baseTextEmbeddingProcessor().description("processor-description").build() | ||
).build(); | ||
String json = | ||
"{\"text_embedding\":{\"tag\":\"some-tag\",\"model_id\":\"modelId\",\"field_map\":{\"input_field\":\"vector_field\"},\"description\":\"processor-description\"}}"; | ||
TextEmbeddingProcessor deserialized = checkJsonRoundtrip(processor, json).textEmbedding(); | ||
|
||
assertEquals("modelId", deserialized.modelId()); | ||
assertEquals(baseFieldMap, deserialized.fieldMap()); | ||
assertEquals("processor-description", deserialized.description()); | ||
assertEquals("some-tag", deserialized.tag()); | ||
} | ||
|
||
@Test | ||
public void testJsonRoundtripWithoutDescription() { | ||
Processor processor = new Processor.Builder().textEmbedding(baseTextEmbeddingProcessor().build()).build(); | ||
String json = | ||
"{\"text_embedding\":{\"tag\":\"some-tag\",\"model_id\":\"modelId\",\"field_map\":{\"input_field\":\"vector_field\"}}}"; | ||
TextEmbeddingProcessor deserialized = checkJsonRoundtrip(processor, json).textEmbedding(); | ||
|
||
assertEquals("modelId", deserialized.modelId()); | ||
assertEquals(baseFieldMap, deserialized.fieldMap()); | ||
assertNull(deserialized.description()); | ||
assertEquals("some-tag", deserialized.tag()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry was this class copied for Elasticsearch codebase? If not, we could only keep SPDX License
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, probably something I copied and pasted from another processor class, fixed at: 752df7b