|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.model; |
| 7 | + |
| 8 | +import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; |
| 9 | +import static org.opensearch.ml.common.CommonValue.VERSION_3_1_0; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +import org.opensearch.common.xcontent.XContentHelper; |
| 17 | +import org.opensearch.common.xcontent.XContentType; |
| 18 | +import org.opensearch.core.ParseField; |
| 19 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 20 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 21 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 22 | +import org.opensearch.core.xcontent.ToXContent; |
| 23 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 24 | +import org.opensearch.core.xcontent.XContentParser; |
| 25 | + |
| 26 | +import lombok.Builder; |
| 27 | +import lombok.Getter; |
| 28 | +import lombok.Setter; |
| 29 | + |
| 30 | +/** |
| 31 | + * Base configuration class for ML local models. This class handles |
| 32 | + * the basic configuration parameters that every local model can support. |
| 33 | + */ |
| 34 | +@Setter |
| 35 | +@Getter |
| 36 | +public class BaseModelConfig extends MLModelConfig { |
| 37 | + public static final String PARSE_FIELD_NAME = "base"; |
| 38 | + public static final NamedXContentRegistry.Entry XCONTENT_REGISTRY = new NamedXContentRegistry.Entry( |
| 39 | + BaseModelConfig.class, |
| 40 | + new ParseField(PARSE_FIELD_NAME), |
| 41 | + it -> parse(it) |
| 42 | + ); |
| 43 | + |
| 44 | + public static final String ADDITIONAL_CONFIG_FIELD = "additional_config"; |
| 45 | + protected Map<String, Object> additionalConfig; |
| 46 | + |
| 47 | + @Builder(builderMethodName = "baseModelConfigBuilder") |
| 48 | + public BaseModelConfig(String modelType, String allConfig, Map<String, Object> additionalConfig) { |
| 49 | + super(modelType, allConfig); |
| 50 | + this.additionalConfig = additionalConfig; |
| 51 | + validateNoDuplicateKeys(allConfig, additionalConfig); |
| 52 | + } |
| 53 | + |
| 54 | + public static BaseModelConfig parse(XContentParser parser) throws IOException { |
| 55 | + String modelType = null; |
| 56 | + String allConfig = null; |
| 57 | + Map<String, Object> additionalConfig = null; |
| 58 | + |
| 59 | + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); |
| 60 | + while (parser.nextToken() != XContentParser.Token.END_OBJECT) { |
| 61 | + String fieldName = parser.currentName(); |
| 62 | + parser.nextToken(); |
| 63 | + |
| 64 | + switch (fieldName) { |
| 65 | + case MODEL_TYPE_FIELD: |
| 66 | + modelType = parser.text(); |
| 67 | + break; |
| 68 | + case ALL_CONFIG_FIELD: |
| 69 | + allConfig = parser.text(); |
| 70 | + break; |
| 71 | + case ADDITIONAL_CONFIG_FIELD: |
| 72 | + additionalConfig = parser.map(); |
| 73 | + break; |
| 74 | + default: |
| 75 | + parser.skipChildren(); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + return new BaseModelConfig(modelType, allConfig, additionalConfig); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String getWriteableName() { |
| 84 | + return PARSE_FIELD_NAME; |
| 85 | + } |
| 86 | + |
| 87 | + public BaseModelConfig(StreamInput in) throws IOException { |
| 88 | + super(in); |
| 89 | + if (in.getVersion().onOrAfter(VERSION_3_1_0)) { |
| 90 | + this.additionalConfig = in.readMap(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void writeTo(StreamOutput out) throws IOException { |
| 96 | + super.writeTo(out); |
| 97 | + if (out.getVersion().onOrAfter(VERSION_3_1_0)) { |
| 98 | + out.writeMap(additionalConfig); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 104 | + builder.startObject(); |
| 105 | + if (modelType != null) { |
| 106 | + builder.field(MODEL_TYPE_FIELD, modelType); |
| 107 | + } |
| 108 | + if (allConfig != null) { |
| 109 | + builder.field(ALL_CONFIG_FIELD, allConfig); |
| 110 | + } |
| 111 | + if (additionalConfig != null) { |
| 112 | + builder.field(ADDITIONAL_CONFIG_FIELD, additionalConfig); |
| 113 | + } |
| 114 | + builder.endObject(); |
| 115 | + return builder; |
| 116 | + } |
| 117 | + |
| 118 | + protected void validateNoDuplicateKeys(String allConfig, Map<String, Object> additionalConfig) { |
| 119 | + if (allConfig == null || additionalConfig == null || additionalConfig.isEmpty()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + Map<String, Object> allConfigMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), allConfig, false); |
| 124 | + Set<String> duplicateKeys = allConfigMap.keySet().stream().filter(additionalConfig::containsKey).collect(Collectors.toSet()); |
| 125 | + if (!duplicateKeys.isEmpty()) { |
| 126 | + throw new IllegalArgumentException( |
| 127 | + "Duplicate keys found in both all_config and additional_config: " + String.join(", ", duplicateKeys) |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public Map<String, Object> getAdditionalConfig() { |
| 133 | + return this.additionalConfig; |
| 134 | + } |
| 135 | +} |
0 commit comments