-
Notifications
You must be signed in to change notification settings - Fork 25.2k
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
base: main
Are you sure you want to change the base?
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,8 +25,10 @@ | |||||||||
import java.util.Map; | ||||||||||
import java.util.Objects; | ||||||||||
|
||||||||||
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalEnum; | ||||||||||
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredEnum; | ||||||||||
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredString; | ||||||||||
import static org.elasticsearch.xpack.inference.services.amazonbedrock.AmazonBedrockConstants.EMBEDDING_TYPE_FIELD; | ||||||||||
import static org.elasticsearch.xpack.inference.services.amazonbedrock.AmazonBedrockConstants.MODEL_FIELD; | ||||||||||
import static org.elasticsearch.xpack.inference.services.amazonbedrock.AmazonBedrockConstants.PROVIDER_FIELD; | ||||||||||
import static org.elasticsearch.xpack.inference.services.amazonbedrock.AmazonBedrockConstants.REGION_FIELD; | ||||||||||
|
@@ -35,10 +37,31 @@ public abstract class AmazonBedrockServiceSettings extends FilteredXContentObjec | |||||||||
|
||||||||||
protected static final String AMAZON_BEDROCK_BASE_NAME = "amazon_bedrock"; | ||||||||||
|
||||||||||
public enum AmazonBedrockEmbeddingType { | ||||||||||
FLOAT, | ||||||||||
BINARY; | ||||||||||
|
||||||||||
public static AmazonBedrockEmbeddingType fromString(String value) { | ||||||||||
return switch (value.toLowerCase()) { | ||||||||||
case "float" -> FLOAT; | ||||||||||
case "binary" -> BINARY; | ||||||||||
default -> throw new IllegalArgumentException("unknown value for embedding type: " + value); | ||||||||||
}; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public String toString() { | ||||||||||
return name().toLowerCase(); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
protected static final AmazonBedrockEmbeddingType DEFAULT_EMBEDDING_TYPE = AmazonBedrockEmbeddingType.FLOAT; | ||||||||||
|
||||||||||
protected final String region; | ||||||||||
protected final String model; | ||||||||||
protected final AmazonBedrockProvider provider; | ||||||||||
protected final RateLimitSettings rateLimitSettings; | ||||||||||
protected final AmazonBedrockEmbeddingType embeddingType; | ||||||||||
|
||||||||||
// the default requests per minute are defined as per-model in the "Runtime quotas" on AWS | ||||||||||
// see: https://docs.aws.amazon.com/bedrock/latest/userguide/quotas.html | ||||||||||
|
@@ -69,34 +92,50 @@ protected static AmazonBedrockServiceSettings.BaseAmazonBedrockCommonSettings fr | |||||||||
AMAZON_BEDROCK_BASE_NAME, | ||||||||||
context | ||||||||||
); | ||||||||||
AmazonBedrockEmbeddingType embeddingType = extractOptionalEnum( | ||||||||||
map, | ||||||||||
EMBEDDING_TYPE_FIELD, | ||||||||||
ModelConfigurations.SERVICE_SETTINGS, | ||||||||||
AmazonBedrockEmbeddingType::fromString, | ||||||||||
EnumSet.allOf(AmazonBedrockEmbeddingType.class), | ||||||||||
validationException | ||||||||||
).orElse(DEFAULT_EMBEDDING_TYPE); | ||||||||||
|
||||||||||
return new BaseAmazonBedrockCommonSettings(region, model, provider, rateLimitSettings); | ||||||||||
return new BaseAmazonBedrockCommonSettings(region, model, provider, rateLimitSettings, embeddingType); | ||||||||||
} | ||||||||||
|
||||||||||
protected record BaseAmazonBedrockCommonSettings( | ||||||||||
String region, | ||||||||||
String model, | ||||||||||
AmazonBedrockProvider provider, | ||||||||||
@Nullable RateLimitSettings rateLimitSettings | ||||||||||
@Nullable RateLimitSettings rateLimitSettings, | ||||||||||
AmazonBedrockEmbeddingType embeddingType | ||||||||||
) {} | ||||||||||
|
||||||||||
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 | ||||||||||
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. Please create a new TransportVersion in https://github.com/elastic/elasticsearch/blob/main/server/src/main/java/org/elasticsearch/TransportVersions.java#L220 See this for an example: https://github.com/elastic/elasticsearch/pull/126565/files#diff-85e782e9e33a0f8ca8e99b41c17f9d04e3a7981d435abf44a3aa5d954a47cd8f We need 2 versions, one for this branch and another for the backport to 8.x |
||||||||||
this.embeddingType = in.readEnum(AmazonBedrockEmbeddingType.class); | ||||||||||
} else { | ||||||||||
this.embeddingType = DEFAULT_EMBEDDING_TYPE; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
protected AmazonBedrockServiceSettings( | ||||||||||
String region, | ||||||||||
String model, | ||||||||||
AmazonBedrockProvider provider, | ||||||||||
@Nullable RateLimitSettings rateLimitSettings | ||||||||||
@Nullable RateLimitSettings rateLimitSettings, | ||||||||||
AmazonBedrockEmbeddingType embeddingType | ||||||||||
) { | ||||||||||
this.region = Objects.requireNonNull(region); | ||||||||||
this.model = Objects.requireNonNull(model); | ||||||||||
this.provider = Objects.requireNonNull(provider); | ||||||||||
this.rateLimitSettings = Objects.requireNonNullElse(rateLimitSettings, DEFAULT_RATE_LIMIT_SETTINGS); | ||||||||||
this.embeddingType = Objects.requireNonNullElse(embeddingType, DEFAULT_EMBEDDING_TYPE); | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
|
@@ -121,12 +160,19 @@ public RateLimitSettings rateLimitSettings() { | |||||||||
return rateLimitSettings; | ||||||||||
} | ||||||||||
|
||||||||||
public AmazonBedrockEmbeddingType embeddingType() { | ||||||||||
return embeddingType; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public void writeTo(StreamOutput out) throws IOException { | ||||||||||
out.writeString(region); | ||||||||||
out.writeString(model); | ||||||||||
out.writeEnum(provider); | ||||||||||
rateLimitSettings.writeTo(out); | ||||||||||
if (out.getTransportVersion().onOrAfter(TransportVersions.V_9_0_0)) { // Version set for BWC | ||||||||||
out.writeEnum(embeddingType); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
public void addBaseXContent(XContentBuilder builder, Params params) throws IOException { | ||||||||||
|
@@ -137,6 +183,9 @@ protected void addXContentFragmentOfExposedFields(XContentBuilder builder, Param | |||||||||
builder.field(REGION_FIELD, region); | ||||||||||
builder.field(MODEL_FIELD, model); | ||||||||||
builder.field(PROVIDER_FIELD, provider.name()); | ||||||||||
if (embeddingType != DEFAULT_EMBEDDING_TYPE) { | ||||||||||
builder.field(EMBEDDING_TYPE_FIELD, embeddingType.toString()); | ||||||||||
} | ||||||||||
Comment on lines
+186
to
+188
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.
Suggested change
We prefer show the default values explicitly rather than hiding them. |
||||||||||
rateLimitSettings.toXContent(builder, params); | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,22 +9,31 @@ | |
|
||
import org.elasticsearch.xcontent.ToXContentObject; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.inference.services.amazonbedrock.AmazonBedrockServiceSettings.AmazonBedrockEmbeddingType; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public record AmazonBedrockTitanEmbeddingsRequestEntity(String inputText) implements ToXContentObject { | ||
import static org.elasticsearch.xpack.inference.services.amazonbedrock.AmazonBedrockConstants.EMBEDDING_TYPE_FIELD; | ||
|
||
public record AmazonBedrockTitanEmbeddingsRequestEntity(String inputText, AmazonBedrockEmbeddingType embeddingType) | ||
implements | ||
ToXContentObject { | ||
|
||
private static final String INPUT_TEXT_FIELD = "inputText"; | ||
|
||
public AmazonBedrockTitanEmbeddingsRequestEntity { | ||
Objects.requireNonNull(inputText); | ||
Objects.requireNonNull(embeddingType); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(INPUT_TEXT_FIELD, inputText); | ||
if (embeddingType == AmazonBedrockEmbeddingType.BINARY) { | ||
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. 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 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 |
||
builder.field(EMBEDDING_TYPE_FIELD, embeddingType.toString()); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
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.
Changing the value of this field will break existing configurations.