ML Model tool

plugins.ml_commons.rag_pipeline_feature_enabled: true

The MLModelTool runs a machine learning (ML) model and returns inference results.

Step 1: Create a connector for a model

The following example request creates a connector for a model hosted on Amazon SageMaker:

  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "sagemaker model",
  4. "description": "Test connector for Sagemaker model",
  5. "version": 1,
  6. "protocol": "aws_sigv4",
  7. "credential": {
  8. "access_key": "<YOUR ACCESS KEY>",
  9. "secret_key": "<YOUR SECRET KEY>"
  10. },
  11. "parameters": {
  12. "region": "us-east-1",
  13. "service_name": "sagemaker"
  14. },
  15. "actions": [
  16. {
  17. "action_type": "predict",
  18. "method": "POST",
  19. "headers": {
  20. "content-type": "application/json"
  21. },
  22. "url": "<YOUR SAGEMAKER ENDPOINT>",
  23. "request_body": """{"prompt":"${parameters.prompt}"}"""
  24. }
  25. ]
  26. }

copy

OpenSearch responds with a connector ID:

  1. {
  2. "connector_id": "eJATWo0BkIylWTeYToTn"
  3. }

Step 2: Register and deploy the model

To register and deploy the model to OpenSearch, send the following request, providing the connector ID from the previous step:

  1. POST /_plugins/_ml/models/_register?deploy=true
  2. {
  3. "name": "remote-inferene",
  4. "function_name": "remote",
  5. "description": "test model",
  6. "connector_id": "eJATWo0BkIylWTeYToTn"
  7. }

copy

OpenSearch responds with a model ID:

  1. {
  2. "task_id": "7X7pWI0Bpc3sThaJ4I8R",
  3. "status": "CREATED",
  4. "model_id": "h5AUWo0BkIylWTeYT4SU"
  5. }

Step 3: Register a flow agent that will run the MLModelTool

A flow agent runs a sequence of tools in order and returns the last tool’s output. To create a flow agent, send the following register agent request, providing the model ID in the model_id parameter:

  1. POST /_plugins/_ml/agents/_register
  2. {
  3. "name": "Test agent for embedding model",
  4. "type": "flow",
  5. "description": "this is a test agent",
  6. "tools": [
  7. {
  8. "type": "MLModelTool",
  9. "description": "A general tool to answer any question",
  10. "parameters": {
  11. "model_id": "h5AUWo0BkIylWTeYT4SU",
  12. "prompt": "\n\nHuman:You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say don't know. \n\nHuman:${parameters.question}\n\nAssistant:"
  13. }
  14. }
  15. ]
  16. }

copy

For parameter descriptions, see Register parameters.

OpenSearch responds with an agent ID:

  1. {
  2. "agent_id": "9X7xWI0Bpc3sThaJdY9i"
  3. }

Step 4: Run the agent

Run the agent by sending the following request:

  1. POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
  2. {
  3. "parameters": {
  4. "question": "what's the population increase of Seattle from 2021 to 2023"
  5. }
  6. }

copy

OpenSearch returns the inference results:

  1. {
  2. "inference_results": [
  3. {
  4. "output": [
  5. {
  6. "name": "response",
  7. "result": " I do not have direct data on the population increase of Seattle from 2021 to 2023 in the context provided. As a data analyst, I would need to research population statistics from credible sources like the US Census Bureau to analyze population trends and make an informed estimate. Without looking up actual data, I don't have enough information to provide a specific answer to the question."
  8. }
  9. ]
  10. }
  11. ]
  12. }

Register parameters

The following table lists all tool parameters that are available when registering an agent.

ParameterTypeRequired/OptionalDescription
model_idStringRequiredThe model ID of the large language model (LLM) to use for generating the response.
promptStringOptionalThe prompt to provide to the LLM.
response_fieldStringOptionalThe name of the response field. Default is response.

Execute parameters

The following table lists all tool parameters that are available when running the agent.

ParameterTypeRequired/OptionalDescription
questionStringRequiredThe natural language question to send to the LLM.