-
Notifications
You must be signed in to change notification settings - Fork 104
Implement Optimized embedding generation in text and image embedding processor #1249
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.neuralsearch.processor.optimization; | ||
|
|
||
| import lombok.extern.log4j.Log4j2; | ||
| import org.opensearch.ingest.IngestDocument; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * TextImageEmbeddingInferenceFilter optimizes text/image embedding inference by selectively processing text/image data. | ||
| * This class provides efficient text/image embedding processing by comparing text/image between existing and new documents. | ||
| * If both text and image are identical, the corresponding embeddings are copied over, avoiding redundant inference calls and improving performance. | ||
| */ | ||
| @Log4j2 | ||
| public class TextImageEmbeddingInferenceFilter { | ||
|
|
||
| public TextImageEmbeddingInferenceFilter() {} | ||
|
|
||
| /** | ||
| * Filters the given knnMap by checking if the values for both text and image are identical in the existing and new document. | ||
| * If both values for text and image match, the corresponding embedding is copied, and empty map is returned, indicating no further | ||
| * processing is required. If any of the two do not match or embedding does not exist, the given knnMap is returned to be processed | ||
| * | ||
| * @return empty Map if embeddings are reused; the original knnMap otherwise. | ||
| */ | ||
| public Map<String, String> filterAndCopyExistingEmbeddings( | ||
| IngestDocument ingestDocument, | ||
| Map<String, Object> existingDocument, | ||
| Map<String, String> knnMap, | ||
| String embeddingField | ||
| ) { | ||
| // knnMap can only contain two keys: one for text field and another for image field. | ||
| // If either of the two does not match, knnMap cannot be filtered | ||
| for (Map.Entry<String, String> entry : knnMap.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| if (existingDocument.containsKey(key) == false || existingDocument.get(key).equals(value) == false) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to compare both the text and image here and this can be done by just checking one key?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. knnMap contains two keys: one for text and one for value. For each entry, it will be compared with text and image values of the existing document
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Synced offline that this code will work because currently we only allow user to define one image and one text field. So the knnMap only contains the text and image fields and both of them should be the same to reuse the existing embedding. We should add a comment to call it out. Besides also thinking we may want to allow users to define multiple text and image fields in the processor. Probably we can create a RFC to see if there is a user need.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return knnMap; | ||
| } | ||
| } | ||
| Object embeddings = existingDocument.get(embeddingField); | ||
| if (Objects.isNull(embeddings)) { | ||
| return knnMap; | ||
| } | ||
| ingestDocument.setFieldValue(embeddingField, existingDocument.get(embeddingField)); | ||
| return Collections.emptyMap(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.