メール履歴RAG

上級

これはInternal Wiki, AI RAG分野の自動化ワークフローで、23個のノードを含みます。主にCode, Gmail, GmailTrigger, ManualTrigger, SplitInBatchesなどのノードを使用。 Gmail、OpenAI、Qdrantベクトルデータベースを使ってメールの知識ベースを作成する

前提条件
  • Googleアカウント + Gmail API認証情報
  • OpenAI API Key
  • Qdrantサーバー接続情報
ワークフロープレビュー
ノード接続関係を可視化、ズームとパンをサポート
ワークフローをエクスポート
以下のJSON設定をn8nにインポートして、このワークフローを使用できます
{
  "id": "7QhxjMbqHv5tk9wM",
  "meta": {
    "instanceId": "b50483b2122f6dc268662b45507d575d939570ea8e1e086601ebb3b6273c1cfd",
    "templateCredsSetupCompleted": true
  },
  "name": "Email History RAG",
  "tags": [
    {
      "id": "PNmVCFjr9sq0xind",
      "name": "RAG",
      "createdAt": "2025-07-12T09:30:51.202Z",
      "updatedAt": "2025-07-12T09:30:51.202Z"
    }
  ],
  "nodes": [
    {
      "id": "dec780ab-9e05-4305-ba52-8a92dfc266e7",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -7040,
        800
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "K8T7HIhpd7gMJG20",
          "name": "OpenAi account - Remap"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "13f21017-5470-4e0e-8d13-aea6d98c4918",
      "name": "付箋",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -7440,
        448
      ],
      "parameters": {
        "width": 1162,
        "height": 796,
        "content": "# RAG AI Agent\n"
      },
      "typeVersion": 1
    },
    {
      "id": "b6615316-94c0-4091-b6e3-8cdf64cb0902",
      "name": "ワークフローをテストするとき",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -6048,
        1472
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "349336cd-9373-4553-aa13-9cf6a427a0c6",
      "name": "RAGエージェント",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -6944,
        576
      ],
      "parameters": {
        "options": {
          "systemMessage": "=You are a helpful assistant that will get data from RAG and send a good response to user.\n\nToday date is this if user ask for dated or latest data: {{ $now }}\n\nSo add date when send query to vector database because I am already setting the date field in embedding."
        }
      },
      "typeVersion": 1.7
    },
    {
      "id": "82da160a-a572-4cdf-a260-f7a0c51a30fa",
      "name": "Gmail Trigger1",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -6064,
        672
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "ufYEel3Kf0MiIlZB",
          "name": "Gmail account - chzainali6363@gmail.com"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "954a8ea4-9e00-43b9-9fe2-6d717b6dccd7",
      "name": "メールデータ取得1",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -5840,
        672
      ],
      "webhookId": "[REDACTED_WEBHOOK_ID]",
      "parameters": {
        "simple": false,
        "options": {},
        "messageId": "={{ $json.id }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "ufYEel3Kf0MiIlZB",
          "name": "Gmail account - chzainali6363@gmail.com"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "16acb16b-8d56-425a-b484-fa4ffc3f5616",
      "name": "Embeddings OpenAI5",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -5680,
        912
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "K8T7HIhpd7gMJG20",
          "name": "OpenAi account - Remap"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "73a26bce-0df2-4755-8175-8a0e367f7418",
      "name": "拡張デフォルトデータローダー3",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -5424,
        880
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "=data_source",
                "value": "=gmail"
              },
              {
                "name": "=created_at",
                "value": "={{ $json.date }}"
              }
            ]
          }
        },
        "jsonData": "=Email details:\nDate: {{ $json.headers.date }}\n\nFrom Email: {{ $json.from.value[0].address }}\nFrom Name:  {{ $json.from.value[0].name }}\n\nTo Email: {{ $json.to.value[0].address }}\n\nEmail Subject:\n{{ $json.subject }}\n\nEmail body:\n{{ $json.text }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "262344fa-8b26-4665-b0ff-e1354e6d84eb",
      "name": "付箋4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -6128,
        448
      ],
      "parameters": {
        "color": 4,
        "width": 1200,
        "height": 788,
        "content": "# Watch Trigger (Email) - New Email\n## Get new Email -> Extract the text -> Add to Vector Store"
      },
      "typeVersion": 1
    },
    {
      "id": "c70f74e1-75ab-4ce3-9c84-f92ff1d7a762",
      "name": "文字テキスト分割器",
      "type": "@n8n/n8n-nodes-langchain.textSplitterCharacterTextSplitter",
      "position": [
        -5344,
        1056
      ],
      "parameters": {
        "separator": "Email details:"
      },
      "typeVersion": 1
    },
    {
      "id": "f01ccf0e-caf4-44f1-a385-a6eb2b063551",
      "name": "コード",
      "type": "n8n-nodes-base.code",
      "position": [
        -5616,
        1472
      ],
      "parameters": {
        "jsCode": "const emails = $input.all().map((item) => item.json);\n\nconst extractedData = emails.map((email) => {\n  // Remove line breaks and excessive spaces from body\n  const cleanBody = email.text\n    ?.replace(/(\\r\\n|\\n|\\r)/gm, ' ')  // Replace newlines with space\n    ?.replace(/\\s+/g, ' ')           // Collapse multiple spaces\n    ?.trim();                        // Remove leading/trailing space\n\n  return {\n    to: email.to?.value?.[0]?.address || '',\n    from: email.from?.value?.[0]?.address || '',\n    fromName: email.from?.value?.[0]?.name || '',\n    date: email.date || '',\n    subject: email.subject || '',\n    body: cleanBody || '',\n    emailId: email.id || '',\n  };\n});\n\nreturn extractedData;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "69d042bf-b75a-414e-a2e2-a62632e6aef2",
      "name": "Embeddings OpenAI11",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -5280,
        1712
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "5200b258-2c96-4d38-9c92-b5353ada6957",
      "name": "デフォルトデータローダー",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -5104,
        1696
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "created_at",
                "value": "={{ $json.date }}"
              },
              {
                "name": "data_source",
                "value": "gmail"
              }
            ]
          }
        },
        "jsonData": "=Email details:\nDate: {{ $json.date }}\n\nFrom Email: {{ $json.from }}\nFrom Name:  {{ $json.fromName }}\n\nTo Email: {{ $json.to }}\n\nEmail Subject:\n{{ $json.subject }}\n\nEmail body:\n{{ $json.body }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "cd126bb7-60a6-450b-b31c-cfd9517e9441",
      "name": "付箋11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -6128,
        1328
      ],
      "parameters": {
        "width": 1596,
        "height": 740,
        "content": "# Get All Emails -> Store embedding in vector db\n"
      },
      "typeVersion": 1
    },
    {
      "id": "19fe59c6-6576-453a-8098-97e47c72c033",
      "name": "メール一括処理",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -5424,
        1472
      ],
      "parameters": {
        "options": {},
        "batchSize": 50
      },
      "typeVersion": 3
    },
    {
      "id": "512bb68a-94d7-4e0f-ab51-265f19d670bd",
      "name": "文字テキスト分割器1",
      "type": "@n8n/n8n-nodes-langchain.textSplitterCharacterTextSplitter",
      "position": [
        -5024,
        1888
      ],
      "parameters": {
        "separator": "Email details:"
      },
      "typeVersion": 1
    },
    {
      "id": "051c026e-0fd7-4182-a388-0ccd6e0c085e",
      "name": "Embeddings OpenAI3",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -6784,
        1072
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "K8T7HIhpd7gMJG20",
          "name": "OpenAi account - Remap"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "d7481948-2972-42e9-b2ef-900c55bc6465",
      "name": "Qdrantメールベクトルストア",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreQdrant",
      "position": [
        -6736,
        896
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "options": {},
        "toolName": "emails_vector_search",
        "toolDescription": "=Call this tool to perform a vector embeddings search in my e-mail database. For time-specific queries:\n1. ALWAYS include the time frame in your query (e.g., \"interviews scheduled after April 27, 2025\" or \"interviews for next week April 28-May 4, 2025\")\n2. For future events, explicitly mention \"future\" or \"upcoming\" in your query",
        "qdrantCollection": {
          "__rl": true,
          "mode": "list",
          "value": "emails_history",
          "cachedResultName": "emails_history"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "7cecd74a-bde6-4fcf-8622-81620c63b20f",
      "name": "Qdrantベクトルストア",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreQdrant",
      "position": [
        -5568,
        672
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "qdrantCollection": {
          "__rl": true,
          "mode": "list",
          "value": "emails_history",
          "cachedResultName": "emails_history"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "1e6d26d8-60e3-4128-82b4-a5e61d83cff1",
      "name": "Qdrantベクトルストア1",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreQdrant",
      "position": [
        -5104,
        1456
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "qdrantCollection": {
          "__rl": true,
          "mode": "list",
          "value": "emails_history",
          "cachedResultName": "emails_history"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "8b9845c3-a9e6-47a1-ae0e-8e66a15b5812",
      "name": "チャットメッセージ受信時",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -7248,
        576
      ],
      "webhookId": "61ffbc2d-0469-49ab-8541-694f021112e6",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "2c320869-b169-4961-a8fd-5403c7a1366e",
      "name": "シンプルメモリ",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -6864,
        800
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "6735edda-91ff-4872-8c13-bec68a8af925",
      "name": "複数メッセージ取得",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -5824,
        1472
      ],
      "webhookId": "778c704a-a717-45e1-adc7-f15a6426c415",
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "ufYEel3Kf0MiIlZB",
          "name": "Gmail account - chzainali6363@gmail.com"
        }
      },
      "typeVersion": 2.1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a81f41c7-8a71-483c-82bb-ff45081649cf",
  "connections": {
    "f01ccf0e-caf4-44f1-a385-a6eb2b063551": {
      "main": [
        [
          {
            "node": "19fe59c6-6576-453a-8098-97e47c72c033",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "349336cd-9373-4553-aa13-9cf6a427a0c6": {
      "main": [
        []
      ]
    },
    "19fe59c6-6576-453a-8098-97e47c72c033": {
      "main": [
        [],
        [
          {
            "node": "1e6d26d8-60e3-4128-82b4-a5e61d83cff1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2c320869-b169-4961-a8fd-5403c7a1366e": {
      "ai_memory": [
        [
          {
            "node": "349336cd-9373-4553-aa13-9cf6a427a0c6",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "954a8ea4-9e00-43b9-9fe2-6d717b6dccd7": {
      "main": [
        [
          {
            "node": "7cecd74a-bde6-4fcf-8622-81620c63b20f",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "82da160a-a572-4cdf-a260-f7a0c51a30fa": {
      "main": [
        [
          {
            "node": "954a8ea4-9e00-43b9-9fe2-6d717b6dccd7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "6735edda-91ff-4872-8c13-bec68a8af925": {
      "main": [
        [
          {
            "node": "f01ccf0e-caf4-44f1-a385-a6eb2b063551",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "dec780ab-9e05-4305-ba52-8a92dfc266e7": {
      "ai_languageModel": [
        [
          {
            "node": "349336cd-9373-4553-aa13-9cf6a427a0c6",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "051c026e-0fd7-4182-a388-0ccd6e0c085e": {
      "ai_embedding": [
        [
          {
            "node": "d7481948-2972-42e9-b2ef-900c55bc6465",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "16acb16b-8d56-425a-b484-fa4ffc3f5616": {
      "ai_embedding": [
        [
          {
            "node": "7cecd74a-bde6-4fcf-8622-81620c63b20f",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "5200b258-2c96-4d38-9c92-b5353ada6957": {
      "ai_document": [
        [
          {
            "node": "1e6d26d8-60e3-4128-82b4-a5e61d83cff1",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "69d042bf-b75a-414e-a2e2-a62632e6aef2": {
      "ai_embedding": [
        [
          {
            "node": "1e6d26d8-60e3-4128-82b4-a5e61d83cff1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "1e6d26d8-60e3-4128-82b4-a5e61d83cff1": {
      "main": [
        [
          {
            "node": "19fe59c6-6576-453a-8098-97e47c72c033",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "c70f74e1-75ab-4ce3-9c84-f92ff1d7a762": {
      "ai_textSplitter": [
        [
          {
            "node": "73a26bce-0df2-4755-8175-8a0e367f7418",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "512bb68a-94d7-4e0f-ab51-265f19d670bd": {
      "ai_textSplitter": [
        [
          {
            "node": "5200b258-2c96-4d38-9c92-b5353ada6957",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "d7481948-2972-42e9-b2ef-900c55bc6465": {
      "ai_tool": [
        [
          {
            "node": "349336cd-9373-4553-aa13-9cf6a427a0c6",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "8b9845c3-a9e6-47a1-ae0e-8e66a15b5812": {
      "main": [
        [
          {
            "node": "349336cd-9373-4553-aa13-9cf6a427a0c6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "73a26bce-0df2-4755-8175-8a0e367f7418": {
      "ai_document": [
        [
          {
            "node": "7cecd74a-bde6-4fcf-8622-81620c63b20f",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "b6615316-94c0-4091-b6e3-8cdf64cb0902": {
      "main": [
        [
          {
            "node": "6735edda-91ff-4872-8c13-bec68a8af925",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
よくある質問

このワークフローの使い方は?

上記のJSON設定コードをコピーし、n8nインスタンスで新しいワークフローを作成して「JSONからインポート」を選択、設定を貼り付けて認証情報を必要に応じて変更してください。

このワークフローはどんな場面に適していますか?

上級 - 内部Wiki, AI RAG検索拡張

有料ですか?

このワークフローは完全無料です。ただし、ワークフローで使用するサードパーティサービス(OpenAI APIなど)は別途料金が発生する場合があります。

ワークフロー情報
難易度
上級
ノード数23
カテゴリー2
ノードタイプ14
難易度説明

上級者向け、16ノード以上の複雑なワークフロー

作成者
Zain Ali

Zain Ali

@zain104

Software Engineer with over 3+ years of experience in full-stack development.

外部リンク
n8n.ioで表示

このワークフローを共有

カテゴリー

カテゴリー: 34