Register a model

All versions of a particular model are held in a model group. You can either register a model group before registering a model to the group or register a first version of a model, thereby creating the group. Each model group name in the cluster must be globally unique.

If you are registering the first version of a model without first registering the model group, a new model group is created automatically with the following name and access level:

  • Name: The new model group will have the same name as the model. Because the model group name must be unique, ensure that your model name does not have the same name as any model groups in the cluster.
  • Access level: The access level for the new model group is determined using the access_mode, backend_roles, and add_all_backend_roles parameters that you pass in the request. If you provide none of the three parameters, the new model group will be private if model access control is enabled on your cluster and public if model access control is disabled. The newly registered model is the first model version assigned to that model group.

Once a model group is created, provide its model_group_id to register a new model version to the model group. In this case, the model name does not need to be unique.

If you’re using pretrained models provided by OpenSearch, we recommend that you first register a model group with a unique name for these models. Then register the pretrained models as versions to that model group. This ensures that every model group has a globally unique model group name.

For information about user access for this API, see Model access control considerations.

If the model is more than 10 MB in size, ML Commons splits it into smaller chunks and saves those chunks in the model’s index.

Path and HTTP methods

  1. POST /_plugins/_ml/models/_register

copy

Register an OpenSearch-provided pretrained model

OpenSearch provides several pretrained models. For more information, see OpenSearch-provided pretrained models.

Register a pretrained text embedding model

To register a pretrained text embedding model, the only required parameters are name, version, and model_format.

Request fields

The following table lists the available request fields.

FieldData typeRequired/OptionalDescription
nameStringRequiredThe model name.
versionStringRequiredThe model version.
model_formatStringRequiredThe portable format of the model file. Valid values are TORCH_SCRIPT and ONNX.
descriptionStringOptionalThe model description.
model_group_idStringOptionalThe model group ID of the model group to register this model to.

Example request: OpenSearch-provided text embedding model

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "huggingface/sentence-transformers/msmarco-distilbert-base-tas-b",
  4. "version": "1.0.1",
  5. "model_group_id": "Z1eQf4oB5Vm0Tdw8EIP2",
  6. "model_format": "TORCH_SCRIPT"
  7. }

copy

Register a pretrained sparse encoding model

To register a pretrained sparse encoding model, you must set the function name to SPARSE_ENCODING or SPARSE_TOKENIZE.

Request fields

The following table lists the available request fields.

FieldData typeRequired/OptionalDescription
nameStringRequiredThe model name.
versionStringRequiredThe model version.
model_formatStringRequiredThe portable format of the model file. Valid values are TORCH_SCRIPT and ONNX.
function_nameStringRequiredSet this parameter to SPARSE_ENCODING or SPARSE_TOKENIZE.
model_content_hash_valueStringRequiredThe model content hash generated using the SHA-256 hashing algorithm.
urlStringRequiredThe URL that contains the model.
descriptionStringOptionalThe model description.
model_group_idStringOptionalThe model group ID of the model group to register this model to.

Example request: OpenSearch-provided sparse encoding model

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "amazon/neural-sparse/opensearch-neural-sparse-encoding-doc-v1",
  4. "version": "1.0.0",
  5. "model_group_id": "Z1eQf4oB5Vm0Tdw8EIP2",
  6. "description": "This is a neural sparse encoding model: It transfers text into sparse vector, and then extract nonzero index and value to entry and weights. It serves only in ingestion and customer should use tokenizer model in query.",
  7. "model_format": "TORCH_SCRIPT",
  8. "function_name": "SPARSE_ENCODING",
  9. "model_content_hash_value": "9a41adb6c13cf49a7e3eff91aef62ed5035487a6eca99c996156d25be2800a9a",
  10. "url": "https://artifacts.opensearch.org/models/ml-models/amazon/neural-sparse/opensearch-neural-sparse-encoding-doc-v1/1.0.0/torch_script/opensearch-neural-sparse-encoding-doc-v1-1.0.0-torch_script.zip"
  11. }

copy

Register a custom model

To use a custom model locally within the OpenSearch cluster, you need to provide a URL and a config object for that model. For more information, see Custom local models.

Request fields

The following table lists the available request fields.

FieldData typeRequired/OptionalDescription
nameStringRequiredThe model name.
versionStringRequiredThe model version.
model_formatStringRequiredThe portable format of the model file. Valid values are TORCH_SCRIPT and ONNX.
function_nameStringRequiredSet this parameter to SPARSE_ENCODING or SPARSE_TOKENIZE.
model_content_hash_valueStringRequiredThe model content hash generated using the SHA-256 hashing algorithm.
model_configObjectRequiredThe model’s configuration, including the model_type, embedding_dimension, and framework_type. all_config is an optional JSON string that contains all model configurations.
urlStringRequiredThe URL that contains the model.
descriptionStringOptionalThe model description.
model_group_idStringOptionalThe model group ID of the model group to register this model to.

The model_config object

FieldData typeDescription
model_typeStringThe model type, such as bert. For a Hugging Face model, the model type is specified in config.json. For an example, see the all-MiniLM-L6-v2 Hugging Face model config.json. Required.
embedding_dimensionIntegerThe dimension of the model-generated dense vector. For a Hugging Face model, the dimension is specified in the model card. For example, in the all-MiniLM-L6-v2 Hugging Face model card, the statement 384 dimensional dense vector space specifies 384 as the embedding dimension. Required.
framework_typeStringThe framework the model is using. Currently, OpenSearch supports sentence_transformers and huggingface_transformers frameworks. The sentence_transformers model outputs text embeddings directly, so ML Commons does not perform any post processing. For huggingface_transformers, ML Commons performs post processing by applying mean pooling to get text embeddings. See the example all-MiniLM-L6-v2 Hugging Face model for more details. Required.
all_configStringThis field is used for reference purposes. You can specify all model configurations in this field. For example, if you are using a Hugging Face model, you can minify the config.json file to one line and save its contents in the all_config field. Once the model is uploaded, you can use the get model API operation to get all model configurations stored in this field. Optional.

You can further customize a pretrained sentence transformer model’s post-processing logic with the following optional fields in the model_config object.

FieldData typeDescription
pooling_modeStringThe post-process model output, either mean, mean_sqrt_len, max, weightedmean, or cls.
normalize_resultBooleanWhen set to true, normalizes the model output in order to scale to a standard range for the model.

Example request: Custom model

The following example request registers a version 1.0.0 of an NLP sentence transformation model named all-MiniLM-L6-v2.

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "all-MiniLM-L6-v2",
  4. "version": "1.0.0",
  5. "description": "test model",
  6. "model_format": "TORCH_SCRIPT",
  7. "model_group_id": "FTNlQ4gBYW0Qyy5ZoxfR",
  8. "model_content_hash_value": "c15f0d2e62d872be5b5bc6c84d2e0f4921541e29fefbef51d59cc10a8ae30e0f",
  9. "model_config": {
  10. "model_type": "bert",
  11. "embedding_dimension": 384,
  12. "framework_type": "sentence_transformers",
  13. "all_config": "{\"_name_or_path\":\"nreimers/MiniLM-L6-H384-uncased\",\"architectures\":[\"BertModel\"],\"attention_probs_dropout_prob\":0.1,\"gradient_checkpointing\":false,\"hidden_act\":\"gelu\",\"hidden_dropout_prob\":0.1,\"hidden_size\":384,\"initializer_range\":0.02,\"intermediate_size\":1536,\"layer_norm_eps\":1e-12,\"max_position_embeddings\":512,\"model_type\":\"bert\",\"num_attention_heads\":12,\"num_hidden_layers\":6,\"pad_token_id\":0,\"position_embedding_type\":\"absolute\",\"transformers_version\":\"4.8.2\",\"type_vocab_size\":2,\"use_cache\":true,\"vocab_size\":30522}"
  14. },
  15. "url": "https://artifacts.opensearch.org/models/ml-models/huggingface/sentence-transformers/all-MiniLM-L6-v2/1.0.1/torch_script/sentence-transformers_all-MiniLM-L6-v2-1.0.1-torch_script.zip"
  16. }

copy

Register a model hosted on a third-party platform

To register a model hosted on a third-party platform, you can either first create a standalone connector and provide the ID of that connector or specify an internal connector for the model. For more information, see Creating connectors for third-party ML platforms.

Request fields

The following table lists the available request fields.

FieldData typeRequired/OptionalDescription
nameStringRequiredThe model name.
function_nameStringRequiredSet this parameter to SPARSE_ENCODING or SPARSE_TOKENIZE.
connector_idOptionalRequiredThe connector ID of a standalone connector for a model hosted on a third-party platform. For more information, see Standalone connector. You must provide either connector_id or connector.
connectorObjectRequiredContains specifications for a connector for a model hosted on a third-party platform. For more information, see Creating a connector for a specific model. You must provide either connector_id or connector.
descriptionStringOptionalThe model description.
model_group_idStringOptionalThe model group ID of the model group to register this model to.

Example request: Remote model with a standalone connector

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "openAI-gpt-3.5-turbo",
  4. "function_name": "remote",
  5. "model_group_id": "1jriBYsBq7EKuKzZX131",
  6. "description": "test model",
  7. "connector_id": "a1eMb4kBJ1eYAeTMAljY"
  8. }

copy

Example request: Remote model with a connector specified as part of the model

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "openAI-GPT-3.5: internal connector",
  4. "function_name": "remote",
  5. "model_group_id": "lEFGL4kB4ubqQRzegPo2",
  6. "description": "test model",
  7. "connector": {
  8. "name": "OpenAI Connector",
  9. "description": "The connector to public OpenAI model service for GPT 3.5",
  10. "version": 1,
  11. "protocol": "http",
  12. "parameters": {
  13. "endpoint": "api.openai.com",
  14. "max_tokens": 7,
  15. "temperature": 0,
  16. "model": "text-davinci-003"
  17. },
  18. "credential": {
  19. "openAI_key": "..."
  20. },
  21. "actions": [
  22. {
  23. "action_type": "predict",
  24. "method": "POST",
  25. "url": "https://${parameters.endpoint}/v1/completions",
  26. "headers": {
  27. "Authorization": "Bearer ${credential.openAI_key}"
  28. },
  29. "request_body": "{ \"model\": \"${parameters.model}\", \"prompt\": \"${parameters.prompt}\", \"max_tokens\": ${parameters.max_tokens}, \"temperature\": ${parameters.temperature} }"
  30. }
  31. ]
  32. }
  33. }

copy

Example response

OpenSearch responds with the task_id and task status.

  1. {
  2. "task_id" : "ew8I44MBhyWuIwnfvDIH",
  3. "status" : "CREATED"
  4. }

Check the status of model registration

To see the status of your model registration and retrieve the model ID created for the new model version, pass the task_id as a path parameter to the Tasks API:

  1. GET /_plugins/_ml/tasks/<task_id>

copy

The response contains the model ID of the model version:

  1. {
  2. "model_id": "Qr1YbogBYOqeeqR7sI9L",
  3. "task_type": "DEPLOY_MODEL",
  4. "function_name": "TEXT_EMBEDDING",
  5. "state": "COMPLETED",
  6. "worker_node": [
  7. "N77RInqjTSq_UaLh1k0BUg"
  8. ],
  9. "create_time": 1685478486057,
  10. "last_update_time": 1685478491090,
  11. "is_async": true
  12. }