PPL tool

Introduced 2.12

This is an experimental feature and is not recommended for use in a production environment. For updates on the progress of the feature or if you want to leave feedback, see the associated GitHub issue.

The PPLTool translates natural language into a PPL query. The tool provides an execute flag to specify whether to run the query. If you set the flag to true, the PPLTool runs the query and returns the query and the results.

Prerequisite

To create a PPL tool, you need a fine-tuned model that translates natural language into PPL queries. Alternatively, you can use large language models for prompt-based translation. The PPL tool supports the Anthropic Claude and OpenAI models.

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: t2ppl",
  4. "description": "Test connector for Sagemaker t2ppl 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. }

For information about connecting to an Anthropic Claude model or OpenAI models, see Connectors.

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-inference",
  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 PPLTool

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. To run the generated query, set execute to true:

  1. POST /_plugins/_ml/agents/_register
  2. {
  3. "name": "Test_Agent_For_PPL",
  4. "type": "flow",
  5. "description": "this is a test agent",
  6. "memory": {
  7. "type": "demo"
  8. },
  9. "tools": [
  10. {
  11. "type": "PPLTool",
  12. "name": "TransferQuestionToPPLAndExecuteTool",
  13. "description": "Use this tool to transfer natural language to generate PPL and execute PPL to query inside. Use this tool after you know the index name, otherwise, call IndexRoutingTool first. The input parameters are: {index:IndexName, question:UserQuestion}",
  14. "parameters": {
  15. "model_id": "h5AUWo0BkIylWTeYT4SU",
  16. "model_type": "FINETUNE",
  17. "execute": true
  18. }
  19. }
  20. ]
  21. }

copy

For parameter descriptions, see Register parameters.

OpenSearch responds with an agent ID:

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

Step 4: Run the agent

Before you run the agent, make sure that you add the sample OpenSearch Dashboards Sample web logs dataset. To learn more, see Adding sample data.

Then, run the agent by sending the following request:

  1. POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
  2. {
  3. "parameters": {
  4. "verbose": true,
  5. "question": "what is the error rate yesterday",
  6. "index": "opensearch_dashboards_sample_data_logs"
  7. }
  8. }

copy

OpenSearch returns the PPL query and the query results:

  1. {
  2. "inference_results": [
  3. {
  4. "output": [
  5. {
  6. "name": "response",
  7. "result":"{\"ppl\":\"source\=opensearch_dashboards_sample_data_logs| where timestamp \> DATE_SUB(NOW(), INTERVAL 1 DAY) AND timestamp \< NOW() | eval is_error\=IF(response\=\'200\', 0, 1.0) | stats AVG(is_error) as error_rate\",\"executionResult\":\"{\\n \\\"schema\\\": [\\n {\\n \\\"name\\\": \\\"error_rate\\\",\\n \\\"type\\\": \\\"double\\\"\\n }\\n ],\\n \\\"datarows\\\": [\\n [\\n null\\n ]\\n ],\\n \\\"total\\\": 1,\\n \\\"size\\\": 1\\n}\"}"
  8. }
  9. ]
  10. }
  11. ]
  12. }

If you set execute to false, OpenSearch only returns the query but does not run it:

  1. {
  2. "inference_results": [
  3. {
  4. "output": [
  5. {
  6. "name": "response",
  7. "result": "source=opensearch_dashboards_sample_data_logs| where timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY) AND timestamp < NOW() | eval is_error=IF(response='200', 0, 1.0) | stats AVG(is_error) as error_rate"
  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 translating text into a PPL query.
model_typeStringOptionalThe model type. Valid values are CLAUDE (Anthropic Claude model), OPENAI (OpenAI models), and FINETUNE (custom fine-tuned model).
promptStringOptionalThe prompt to provide to the LLM.
executeBooleanOptionalSpecifies whether to run the PPL query. Default is true.
inputObjectOptionalContains two parameters that specify the index to search and the question for the LLM. For example, “input”: “{\”index\”: \”${parameters.index}\”, \”question\”: ${parameters.question} }”.
headIntegerOptionalLimits the number of returned execution results if execute is set to true. Default is -1 (no limit).

Execute parameters

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

ParameterTypeRequired/OptionalDescription
indexStringRequiredThe index on which to run the PPL query.
questionStringRequiredThe natural language question to send to the LLM.
verboseBooleanOptionalWhether to provide verbose output. Default is false.