Skip to content

feat: [ML] Support binary embeddings from Amazon Bedrock Titan (#125378) #126540

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

demoncoder-crypto
Copy link

[ML] Support binary embeddings from Amazon Bedrock Titan (#125378)

This change adds support for the binary embedding type offered by Amazon Titan text embedding models via the Amazon Bedrock connector in the Inference API.

Changes:

  • Added an optional embedding_type parameter to the Bedrock service configuration (service_settings). It accepts "float" (default) or "binary".
  • Modified the request payload generation for Titan embeddings to include "embeddingType": "binary" when the embedding_type setting is set to binary.
  • Updated the response parsing logic to handle both standard float arrays and Base64-encoded binary embeddings returned by Bedrock, based on the requested embedding_type. Returns TextEmbeddingFloatResults or TextEmbeddingBytesResults accordingly.
  • Added unit tests covering the new configuration option, request generation, and response parsing for both float and binary types.

This allows users to leverage the binary embedding format provided by Amazon Titan models through the Elasticsearch Inference API.

Copy link

cla-checker-service bot commented Apr 9, 2025

💚 CLA has been signed

@elasticsearchmachine elasticsearchmachine added v9.1.0 needs:triage Requires assignment of a team area label external-contributor Pull request authored by a developer outside the Elasticsearch team labels Apr 9, 2025
@davidkyle davidkyle added :ml Machine learning and removed needs:triage Requires assignment of a team area label labels Apr 11, 2025
@elasticsearchmachine elasticsearchmachine added the Team:ML Meta label for the ML team label Apr 11, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/ml-core (Team:ML)

@davidkyle davidkyle self-assigned this Apr 11, 2025
@davidkyle
Copy link
Member

@elasticmachine test this please

Copy link
Member

@davidkyle davidkyle left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @demoncoder-crypto

We have a very similar PR open for adding embedding type to AWS Bedrock Cohere embedding models #126565

@@ -11,8 +11,9 @@ public class AmazonBedrockConstants {
public static final String ACCESS_KEY_FIELD = "access_key";
public static final String SECRET_KEY_FIELD = "secret_key";
public static final String REGION_FIELD = "region";
public static final String MODEL_FIELD = "model";
public static final String MODEL_FIELD = "model_id";
Copy link
Member

Choose a reason for hiding this comment

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

Changing the value of this field will break existing configurations.

) {}

protected AmazonBedrockServiceSettings(StreamInput in) throws IOException {
this.region = in.readString();
this.model = in.readString();
this.provider = in.readEnum(AmazonBedrockProvider.class);
this.rateLimitSettings = new RateLimitSettings(in);
if (in.getTransportVersion().onOrAfter(TransportVersions.V_9_0_0)) { // Version set for BWC
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +186 to +188
if (embeddingType != DEFAULT_EMBEDDING_TYPE) {
builder.field(EMBEDDING_TYPE_FIELD, embeddingType.toString());
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (embeddingType != DEFAULT_EMBEDDING_TYPE) {
builder.field(EMBEDDING_TYPE_FIELD, embeddingType.toString());
}
builder.field(EMBEDDING_TYPE_FIELD, embeddingType.toString());

We prefer show the default values explicitly rather than hiding them.

}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(INPUT_TEXT_FIELD, inputText);
if (embeddingType == AmazonBedrockEmbeddingType.BINARY) {
Copy link
Member

Choose a reason for hiding this comment

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

Always be explicit rather than depending on default values set in a 3rd party service. If Bedrock changes the default embedding then it will break any integrations that rely on the default value being float. Also of a new value is added to the AmazonBedrockEmbeddingType enum it won't be set in this request.

The G1 models do not support binary embeddings only the V2 models

https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-embed-text.html

When an user creates

@davidkyle
Copy link
Member

@elasticmachine test this please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement external-contributor Pull request authored by a developer outside the Elasticsearch team :ml Machine learning Team:ML Meta label for the ML team v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants