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

Query parameters

The following table lists the available query parameters. All query parameters are optional.

ParameterData typeDescription
deployBooleanWhether to deploy the model after registering it. The deploy operation is performed by calling the Deploy Model API. Default is false.

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 ID of the model group to which to register the model.

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_nameStringRequiredFor text embedding models, set this parameter to TEXT_EMBEDDING. For sparse encoding models, set this parameter to SPARSE_ENCODING or SPARSE_TOKENIZE. For cross-encoder models, set this parameter to TEXT_SIMILARITY. For question answering models, set this parameter to QUESTION_ANSWERING.
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 ID of the model group to which to register this model.

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.1",
  5. "model_group_id": "Z1eQf4oB5Vm0Tdw8EIP2",
  6. "model_format": "TORCH_SCRIPT"
  7. }

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.
is_enabledBooleanOptionalSpecifies whether the model is enabled. Disabling the model makes it unavailable for Predict API requests, regardless of the model’s deployment status. Default is true.
rate_limiterObjectOptionalLimits the number of times that any user can call the Predict API on the model. For more information, see Rate limiting inference calls.
interfaceObjectOptionalThe interface for the model. For more information, see Interface.

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.
is_enabledBooleanOptionalSpecifies whether the model is enabled. Disabling the model makes it unavailable for Predict API requests, regardless of the model’s deployment status. Default is true.
rate_limiterObjectOptionalLimits the number of times that any user can call the Predict API on the model. For more information, see Rate limiting inference calls.
guardrailsObjectOptionalThe guardrails for the model input. For more information, see Guardrails.
interfaceObjectOptionalThe interface for the model. For more information, see Interface.

Example request: Externally hosted 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: Externally hosted 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, task status, and model_id:

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

The guardrails parameter

Guardrails are safety measures for large language models (LLMs). They provide a set of rules and boundaries that control how an LLM behaves and what kind of output it generates.

To register an externally hosted model with guardrails, provide the guardrails parameter, which supports the following fields. All fields are optional.

FieldData typeDescription
typeStringThe guardrail type. Valid values are local_regex and model. Using local_regex, you can specify a regular expression or stop words. Using model, you can specify a guardrail model. For more information, see Guardrails.
input_guardrailObjectThe guardrail for the model input.
output_guardrailObjectThe guardrail for the model output.
stop_wordsObjectThe list of indexes containing stopwords used for model input/output validation. If the model prompt/response contains a stopword contained in any of the indexes, then the predict request on the model is rejected.
index_nameObjectThe name of the index storing the stopwords.
source_fieldsObjectThe name of the field storing the stopwords.
regexObjectA regular expression used for input/output validation. If the model prompt/response matches the regular expression, then the predict request on the model is rejected.
model_idStringThe guardrail model used to validate user input and LLM output.
response_filterStringThe dot path of the field containing the guardrail model response.
response_validation_regexStringThe regular expression used to validate the guardrail model response.

Examples

The following examples configure an externally hosted model with guardrails.

Example request: Regex and stopword validation

The following example uses a regular expression and a set of stopwords to validate the LLM response:

  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. "guardrails": {
  9. "type": "local_regex",
  10. "input_guardrail": {
  11. "stop_words": [
  12. {
  13. "index_name": "stop_words_input",
  14. "source_fields": ["title"]
  15. }
  16. ],
  17. "regex": ["regex1", "regex2"]
  18. },
  19. "output_guardrail": {
  20. "stop_words": [
  21. {
  22. "index_name": "stop_words_output",
  23. "source_fields": ["title"]
  24. }
  25. ],
  26. "regex": ["regex1", "regex2"]
  27. }
  28. }
  29. }

copy

For a complete example, see Validating input/output using stopwords and regex.

Example request: Guardrail model validation

The following example uses a guardrail model to validate the LLM response:

  1. POST /_plugins/_ml/models/_register?deploy=true
  2. {
  3. "name": "Bedrock Claude V2 model with guardrails model",
  4. "function_name": "remote",
  5. "model_group_id": "ppSmpo8Bi-GZ0tf1i7cD",
  6. "description": "Bedrock Claude V2 model with guardrails model",
  7. "connector_id": "xnJjDZABNFJeYR3IPvTO",
  8. "guardrails": {
  9. "input_guardrail": {
  10. "model_id": "o3JaDZABNFJeYR3I2fRV",
  11. "response_validation_regex": "^\\s*\"[Aa]ccept\"\\s*$"
  12. },
  13. "output_guardrail": {
  14. "model_id": "o3JaDZABNFJeYR3I2fRV",
  15. "response_validation_regex": "^\\s*\"[Aa]ccept\"\\s*$"
  16. },
  17. "type": "model"
  18. }
  19. }

copy

For a complete example, see Validating input/output using a guardrail model.

Example response

OpenSearch responds with the task_id, task status, and model_id:

  1. {
  2. "task_id": "tsqvDY4BChVAiNVEuo8F",
  3. "status": "CREATED",
  4. "model_id": "t8qvDY4BChVAiNVEuo8q"
  5. }

The interface parameter

The model interface provides a highly flexible way to add arbitrary metadata annotations to all local deep learning models and externally hosted models in a JSON schema syntax. This annotation initiates a validation check on the input and output fields of the model during the model’s invocation. The validation check ensures that the input and output fields are in the correct format both before and after the model performs inference.

To register a model with a model interface, provide the interface parameter, which supports the following fields.

FieldData typeDescription
inputObjectThe JSON schema for the model input.
outputObjectThe JSON schema for the model output.

The input and output fields are evaluated against the provided JSON schema. You do not need to provide both fields simultaneously.

Connector model interfaces

To simplify your workflow, you can register an externally hosted model using a connector in one of the connector blueprint formats. If you do so, a predefined model interface for this connector is generated automatically during model registration. The predefined model interface is generated based on the connector blueprint and the model’s metadata, so you must strictly follow the blueprint when creating the connector in order to avoid errors.

The following connector blueprints currently support creating predefined model interfaces:

To learn more about connector blueprints, see Connector blueprints.

Example request: Externally hosted model with an interface

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "openAI-gpt-3.5-turbo",
  4. "function_name": "remote",
  5. "description": "test model",
  6. "connector_id": "A-j7K48BZzNMh1sWVdJu",
  7. "interface": {
  8. "input": {
  9. "properties": {
  10. "parameters": {
  11. "properties": {
  12. "messages": {
  13. "type": "string",
  14. "description": "This is a test description field"
  15. }
  16. }
  17. }
  18. }
  19. },
  20. "output": {
  21. "properties": {
  22. "inference_results": {
  23. "type": "array",
  24. "items": {
  25. "type": "object",
  26. "properties": {
  27. "output": {
  28. "type": "array",
  29. "items": {
  30. "properties": {
  31. "name": {
  32. "type": "string",
  33. "description": "This is a test description field"
  34. },
  35. "dataAsMap": {
  36. "type": "object",
  37. "description": "This is a test description field"
  38. }
  39. }
  40. },
  41. "description": "This is a test description field"
  42. },
  43. "status_code": {
  44. "type": "integer",
  45. "description": "This is a test description field"
  46. }
  47. }
  48. },
  49. "description": "This is a test description field"
  50. }
  51. }
  52. }
  53. }
  54. }

copy

Example response

OpenSearch responds with the task_id, task status, and model_id:

  1. {
  2. "task_id": "tsqvDY4BChVAiNVEuo8F",
  3. "status": "CREATED",
  4. "model_id": "t8qvDY4BChVAiNVEuo8q"
  5. }

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. }