CreateAnomalyDetectorTool

Introduced 2.16

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 CreateAnomalyDetectorTool helps create anomaly detectors based on your provided index. This tool retrieves index mappings and enables a large language model (LLM) to recommend category fields, aggregation fields, and their corresponding aggregation methods, which are required by the Create Anomaly Detector API.

For comprehensive information about anomaly detectors, see Anomaly detection.

Step 1: Register a flow agent that runs the CreateAnomalyDetectorTool

A flow agent runs a sequence of tools in order, returning the output of the last tool. To create a flow agent, send the following register agent request:

  1. POST /_plugins/_ml/agents/_register
  2. {
  3. "name": "Test_Agent_For_Create_Anomaly_Detector_Tool",
  4. "type": "flow",
  5. "description": "this is a test agent for the CreateAnomalyDetectorTool",
  6. "memory": {
  7. "type": "demo"
  8. },
  9. "tools": [
  10. {
  11. "type": "CreateAnomalyDetectorTool",
  12. "name": "DemoCreateAnomalyDetectorTool",
  13. "parameters": {
  14. "model_id": "<the model id of LLM>"
  15. }
  16. }
  17. ]
  18. }

copy

OpenSearch responds with an agent ID, for example, as follows:

  1. {
  2. "agent_id": "EuJYYo0B9RaBCvhuy1q8"
  3. }

copy

Step 2: Run the agent

Run the agent by sending the following request:

  1. POST /_plugins/_ml/agents/EuJYYo0B9RaBCvhuy1q8/_execute
  2. {
  3. "parameters": {
  4. "index": "sample_weblogs_test"
  5. }
  6. }

copy

OpenSearch responds with a JSON string containing all of the recommended parameters for creating an anomaly detector, such as the string shown in the following example repsonse:

  1. {
  2. "inference_results": [
  3. {
  4. "output": [
  5. {
  6. "name": "response",
  7. "result":"""{"index":"sample_weblogs_test","categoryField":"ip.keyword","aggregationField":"bytes,response,responseLatency","aggregationMethod":"sum,avg,avg","dateFields":"utc_time,timestamp"}"""
  8. }
  9. ]
  10. }
  11. ]
  12. }

copy

You can then create an anomaly detector containing the recommended parameters by sending a request similar to the following:

  1. POST _plugins/_anomaly_detection/detectors
  2. {
  3. "name": "test-detector",
  4. "description": "Test detector",
  5. "time_field": "timestamp",
  6. "indices": [
  7. "sample_weblogs_test"
  8. ],
  9. "feature_attributes": [
  10. {
  11. "feature_name": "feature_bytes",
  12. "feature_enabled": true,
  13. "aggregation_query": {
  14. "agg1": {
  15. "sum": {
  16. "field": "bytes"
  17. }
  18. }
  19. }
  20. },
  21. {
  22. "feature_name": "feature_response",
  23. "feature_enabled": true,
  24. "aggregation_query": {
  25. "agg2": {
  26. "avg": {
  27. "field": "response"
  28. }
  29. }
  30. }
  31. },
  32. {
  33. "feature_name": "feature_responseLatency",
  34. "feature_enabled": true,
  35. "aggregation_query": {
  36. "agg3": {
  37. "avg": {
  38. "field": "responseLatency"
  39. }
  40. }
  41. }
  42. }
  43. ],
  44. "detection_interval": {
  45. "period": {
  46. "interval": 1,
  47. "unit": "Minutes"
  48. }
  49. },
  50. "window_delay": {
  51. "period": {
  52. "interval": 1,
  53. "unit": "Minutes"
  54. }
  55. }
  56. }

copy

Register parameters

The following table lists the available tool parameters for agent registration.

ParameterTypeRequired/OptionalDescription
model_idStringRequiredThe LLM model ID used for suggesting required Create Anomaly Detector API parameters.
model_typeStringOptionalThe model type. Valid values are CLAUDE (Anthropic Claude models) and OPENAI (OpenAI models).

Execute parameters

The following table lists the available tool parameters for running the agent.

ParameterTypeRequired/OptionalDescription
indexStringRequiredThe index name. Supports wildcards (for example, weblogs-*). If wildcards are used, then the tool fetches mappings from the first resolved index and sends them to the LLM.