8
n8n 中文网amn8n.com

[3/3] 异常检测工具(农作物数据集)

高级

这是一个AI, SecOps领域的自动化工作流,包含 17 个节点。主要使用 Set, Code, HttpRequest, ExecuteWorkflowTrigger 等节点,结合人工智能技术实现智能自动化。 异常(图像)检测工具 [3/3 - 异常]

前置要求
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "G8jRDBvwsMkkMiLN",
  "meta": {
    "instanceId": "205b3bc06c96f2dc835b4f00e1cbf9a937a74eeb3b47c99d0c30b0586dbf85aa"
  },
  "name": "[3/3] 异常检测工具(农作物数据集)",
  "tags": [
    {
      "id": "spMntyrlE9ydvWFA",
      "name": "anomaly-detection",
      "createdAt": "2024-12-08T22:05:15.945Z",
      "updatedAt": "2024-12-09T12:50:19.287Z"
    }
  ],
  "nodes": [
    {
      "id": "e01bafec-eb24-44c7-b3c4-a60f91666350",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1200,
        180
      ],
      "parameters": {
        "color": 6,
        "width": 400,
        "height": 740,
        "content": "我们在此使用农作物数据集:"
      },
      "typeVersion": 1
    },
    {
      "id": "b9943781-de1f-4129-9b81-ed836e9ebb11",
      "name": "嵌入图像",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        680,
        60
      ],
      "parameters": {
        "url": "https://api.voyageai.com/v1/multimodalembeddings",
        "method": "POST",
        "options": {},
        "jsonBody": "={{\n{\n  \"inputs\": [\n    {\n      \"content\": [\n        {\n          \"type\": \"image_url\",\n          \"image_url\": $('Image URL hardcode').first().json.imageURL\n        }\n      ]\n    }\n  ],\n  \"model\": \"voyage-multimodal-3\",\n  \"input_type\": \"document\"\n}\n}}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "Vb0RNVDnIHmgnZOP",
          "name": "Voyage API"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "47b72bc2-4817-48c6-b517-c1328e402468",
      "name": "获取聚类中心相似度",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        940,
        60
      ],
      "parameters": {
        "url": "={{ $('Variables for medoids').first().json.qdrantCloudURL }}/collections/{{ $('Variables for medoids').first().json.collectionName }}/points/query",
        "method": "POST",
        "options": {},
        "jsonBody": "={{\n{\n  \"query\": $json.data[0].embedding,\n  \"using\": \"voyage\",\n  \"limit\": $('Info About Crop Labeled Clusters').first().json.cropsNumber,\n  \"with_payload\": true,\n  \"filter\": {\n      \"must\": [\n          {      \n          \"key\": $('Variables for medoids').first().json.clusterCenterType,\n          \"match\": {\n              \"value\": true\n              }\n          }\n      ]\n  }\n}\n}}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "qdrantApi"
      },
      "credentials": {
        "qdrantApi": {
          "id": "it3j3hP9FICqhgX6",
          "name": "QdrantApi account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "42d7eb27-ec38-4406-b5c4-27eb45358e93",
      "name": "比较分数",
      "type": "n8n-nodes-base.code",
      "position": [
        1140,
        60
      ],
      "parameters": {
        "language": "python",
        "pythonCode": "points = _input.first()['json']['result']['points']\nthreshold_type = _('Variables for medoids').first()['json']['clusterThresholdCenterType']\n\nmax_score = -1\ncrop_with_max_score = None\nundefined = True\n\nfor center in points:\n    if center['score'] >= center['payload'][threshold_type]:\n        undefined = False\n        if center['score'] > max_score:\n            max_score = center['score']\n            crop_with_max_score = center['payload']['crop_name']\n\nif undefined:\n    result_message = \"ALERT, we might have a new undefined crop!\"\nelse:\n    result_message = f\"Looks similar to {crop_with_max_score}\"\n\nreturn [{\n    \"json\": {\n        \"result\": result_message\n    }\n}]\n"
      },
      "typeVersion": 2
    },
    {
      "id": "23aa604a-ff0b-4948-bcd5-af39300198c0",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1200,
        -220
      ],
      "parameters": {
        "width": 400,
        "height": 380,
        "content": "## 农作物异常检测工具"
      },
      "typeVersion": 1
    },
    {
      "id": "3a79eca2-44f9-4aee-8a0d-9c7ca2f9149d",
      "name": "聚类中心变量",
      "type": "n8n-nodes-base.set",
      "position": [
        -200,
        60
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "dbbc1e7b-c63e-4ff1-9524-8ef3e9f6cd48",
              "name": "clusterCenterType",
              "type": "string",
              "value": "is_medoid"
            },
            {
              "id": "a994ce37-2530-4030-acfb-ec777a7ddb05",
              "name": "qdrantCloudURL",
              "type": "string",
              "value": "https://152bc6e2-832a-415c-a1aa-fb529f8baf8d.eu-central-1-0.aws.cloud.qdrant.io"
            },
            {
              "id": "12f0a9e6-686d-416e-a61b-72d034ec21ba",
              "name": "collectionName",
              "type": "string",
              "value": "=agricultural-crops"
            },
            {
              "id": "4c88a617-d44f-4776-b457-8a1dffb1d03c",
              "name": "clusterThresholdCenterType",
              "type": "string",
              "value": "is_medoid_cluster_threshold"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "13b25434-bd66-4293-93f1-26c67b9ec7dd",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -340,
        260
      ],
      "parameters": {
        "color": 6,
        "width": 360,
        "height": 200,
        "content": "**clusterCenterType** - 可选值:"
      },
      "typeVersion": 1
    },
    {
      "id": "869b0962-6cae-487d-8230-539a0cc4c14c",
      "name": "农作物标记聚类信息",
      "type": "n8n-nodes-base.set",
      "position": [
        440,
        60
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5327b254-b703-4a34-a398-f82edb1d6d6b",
              "name": "=cropsNumber",
              "type": "number",
              "value": "={{ $json.result.hits.length }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "5d3956f8-f43b-439e-b176-a594a21d8011",
      "name": "集合中的总点数",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        40,
        60
      ],
      "parameters": {
        "url": "={{ $json.qdrantCloudURL }}/collections/{{ $json.collectionName }}/points/count",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"exact\": true\n}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "qdrantApi"
      },
      "credentials": {
        "qdrantApi": {
          "id": "it3j3hP9FICqhgX6",
          "name": "QdrantApi account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "14ba3db9-3965-4b20-b333-145616d45c3a",
      "name": "各类农作物数量统计",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        240,
        60
      ],
      "parameters": {
        "url": "={{ $('Variables for medoids').first().json.qdrantCloudURL }}/collections/{{ $('Variables for medoids').first().json.collectionName }}/facet",
        "method": "POST",
        "options": {},
        "jsonBody": "={{\n{\n  \"key\": \"crop_name\",\n  \"limit\": $json.result.count,\n  \"exact\": true\n}\n}}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "qdrantApi"
      },
      "credentials": {
        "qdrantApi": {
          "id": "it3j3hP9FICqhgX6",
          "name": "QdrantApi account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "e37c6758-0556-4a56-ab14-d4df663cb53a",
      "name": "硬编码图像URL",
      "type": "n8n-nodes-base.set",
      "position": [
        -480,
        60
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "46ceba40-fb25-450c-8550-d43d8b8aa94c",
              "name": "imageURL",
              "type": "string",
              "value": "={{ $json.query.imageURL }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "b24ad1a7-0cf8-4acc-9c18-6fe9d58b10f2",
      "name": "执行工作流触发器",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -720,
        60
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "50424f2b-6831-41bf-8de4-81f69d901ce1",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -240,
        -80
      ],
      "parameters": {
        "width": 180,
        "height": 120,
        "content": "用于访问我们在前两个流程中上传并准备用于异常检测的Qdrant集合的变量"
      },
      "typeVersion": 1
    },
    {
      "id": "2e8ed3ca-1bba-4214-b34b-376a237842ff",
      "name": "便利贴5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        -120
      ],
      "parameters": {
        "width": 560,
        "height": 140,
        "content": "这三个节点仅用于计算Qdrant集合中有多少不同的类别(农作物):**cropsNumber**(在*\"获取聚类中心相似度\"*节点中需要)。"
      },
      "typeVersion": 1
    },
    {
      "id": "e2fa5763-6e97-4ff5-8919-1cb85a3c6968",
      "name": "便签 6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        620,
        240
      ],
      "parameters": {
        "height": 120,
        "content": "这里,我们使用Voyage嵌入模型对传递给此工作流工具的图像进行嵌入,以将该图像与数据库中的所有农作物图像进行比较。"
      },
      "typeVersion": 1
    },
    {
      "id": "cdb6b8d3-f7f4-4d66-850f-ce16c8ed98b9",
      "name": "便签 7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        920,
        220
      ],
      "parameters": {
        "width": 400,
        "height": 180,
        "content": "检查图像与所有聚类中心(农作物)的相似程度。"
      },
      "typeVersion": 1
    },
    {
      "id": "03b4699f-ba43-4f5f-ad69-6f81deea2641",
      "name": "便签22",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -620,
        580
      ],
      "parameters": {
        "color": 4,
        "width": 540,
        "height": 300,
        "content": "### 关于异常检测"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {
    "Execute Workflow Trigger": [
      {
        "json": {
          "query": {
            "imageURL": "https://storage.googleapis.com/n8n-qdrant-demo/agricultural-crops%2Fcotton%2Fimage%20(36).jpg"
          }
        }
      }
    ]
  },
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f67b764b-9e1a-4db0-b9f2-490077a62f74",
  "connections": {
    "Embed image": {
      "main": [
        [
          {
            "node": "Get similarity of medoids",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Each Crop Counts": {
      "main": [
        [
          {
            "node": "Info About Crop Labeled Clusters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Image URL hardcode": {
      "main": [
        [
          {
            "node": "Variables for medoids",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Variables for medoids": {
      "main": [
        [
          {
            "node": "Total Points in Collection",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow Trigger": {
      "main": [
        [
          {
            "node": "Image URL hardcode",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get similarity of medoids": {
      "main": [
        [
          {
            "node": "Compare scores",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Total Points in Collection": {
      "main": [
        [
          {
            "node": "Each Crop Counts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Info About Crop Labeled Clusters": {
      "main": [
        [
          {
            "node": "Embed image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 人工智能, 安全运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量17
分类2
节点类型5
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者

Qdrant DevRel, ML/NLP/math nerd with yapping skills

外部链接
在 n8n.io 查看

分享此工作流