{
  "id": "RwIJDmIumswgwEFS",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI Investment Scenario Simulator",
  "tags": [],
  "nodes": [
    {
      "id": "3ea0bd36-08dd-411b-80a3-f6b96c753f3c",
      "name": "Groq Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        1856,
        272
      ],
      "parameters": {
        "model": "openai/gpt-oss-120b",
        "options": {}
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5189c6ae-32a0-4e23-bf95-c117feafc380",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -608,
        -736
      ],
      "parameters": {
        "width": 576,
        "height": 576,
        "content": "## AI Investment Scenario Simulator\n\nThis workflow analyzes an investment scenario using user inputs and enhances it with AI-driven insights and real-time market context. It begins by receiving investment details such as amount, expected return, and time horizon through a webhook. The inputs are validated to ensure they meet logical constraints before further processing.\n\nAfter validation, the workflow calculates projected future value using compound growth logic and determines a risk profile (Low, Medium, High) based on expected return. It then dynamically fetches relevant market news using a risk-based RSS query, sorts and limits the results, and structures them for AI consumption.\n\nThe AI model processes all inputs, projections, risk level, and market context to generate a clear explanation along with three tailored investment suggestions across different risk levels. The final response is formatted into structured JSON and returned via webhook.\n\n### Setup Steps:\n1. Configure and activate the Webhook node\n2. Add Groq/OpenAI API credentials\n3. Optionally adjust RSS query keywords\n4. Test with sample input payload\n5. Connect to frontend or API client"
      },
      "typeVersion": 1
    },
    {
      "id": "977d8633-6f1c-4006-848f-248f96dd6858",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -16,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 608,
        "height": 736,
        "content": "## Input & Validation\n\nThis section receives investment inputs via webhook and validates them using conditional logic. If any value falls outside acceptable ranges, an error response is immediately returned, ensuring only valid data proceeds to further processing and analysis."
      },
      "typeVersion": 1
    },
    {
      "id": "4a42ccdc-80dc-4a07-b805-6b7c436c7907",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 352,
        "height": 736,
        "content": "## Data Processing & Risk\n\nThis section transforms validated input into structured variables and performs core calculations. It computes future investment value using compound growth and assigns a risk level based on expected return, forming the foundation for downstream analysis."
      },
      "typeVersion": 1
    },
    {
      "id": "f4796e25-a4af-4cb6-b837-3ba4985dc00a",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1760,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 352,
        "height": 736,
        "content": "## AI Analysis\n\nThis section sends structured investment data and curated market insights to the AI model. The model analyzes the scenario and generates a clear explanation along with three investment suggestions aligned to different risk levels in a structured format."
      },
      "typeVersion": 1
    },
    {
      "id": "b326db43-1078-44b5-b7c3-18f69034360c",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2128,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 448,
        "height": 736,
        "content": "## Output Handling\n\nThis section formats the AI-generated response into a structured JSON output that includes input details, projections, and analysis. The final result is returned via webhook, making it ready for integration with external applications or user interfaces."
      },
      "typeVersion": 1
    },
    {
      "id": "240d362d-bb55-4255-bc2f-7f1f17325cdb",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        976,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 752,
        "height": 736,
        "content": "## Market Insights\n\nThis section retrieves real-time market news using a dynamically generated RSS query based on risk level. The results are sorted, limited to the most relevant items, and then structured into a simplified format for efficient use in AI processing."
      },
      "typeVersion": 1
    },
    {
      "id": "147a5479-f873-4b3c-bccd-075d68b6b8ba",
      "name": "Receive Investment Input",
      "type": "n8n-nodes-base.webhook",
      "position": [
        0,
        -48
      ],
      "parameters": {
        "path": "investment-simulator",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2.1
    },
    {
      "id": "8d487a44-5c47-43fa-a11b-7aac2738272c",
      "name": "Validate User Input",
      "type": "n8n-nodes-base.if",
      "position": [
        176,
        -48
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "b98bf7ea-880c-4ddb-9581-67b610dff069",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ $json.body.principal }}",
              "rightValue": 1000
            },
            {
              "id": "ea7549e3-8788-4075-ae1e-7b451f94cf6e",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.body.rate < 0 || $json.body.rate > 50 }}",
              "rightValue": 0
            },
            {
              "id": "2996a07c-6e4b-4e40-9b4e-ad1d810bfd95",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ $json.body.time }}",
              "rightValue": 1
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "40e09367-535c-4ecc-97ba-18f949a41bee",
      "name": "Invalid Input Error",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        416,
        -176
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "{\n  \"success\": \"false\",\n  \"error\": \"Invalid input. Ensure amount > 0, return between 0\u201350%, and years > 0.\"\n}"
      },
      "typeVersion": 1.5
    },
    {
      "id": "e33eb4cf-e879-4bc0-b398-4d0d12485a34",
      "name": "Calculate Projection & Risk Profile",
      "type": "n8n-nodes-base.set",
      "position": [
        752,
        48
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "23cb1a55-84b9-4ade-8854-5476ddc3b927",
              "name": "amount",
              "type": "number",
              "value": "={{ $json.body.principal }}"
            },
            {
              "id": "12588aea-4a5e-498d-a4a7-b73773392f57",
              "name": "rate",
              "type": "number",
              "value": "={{ $json.body.rate / 100 }}"
            },
            {
              "id": "fd5d141e-c3d0-4479-979a-a0cb49a1414c",
              "name": "years",
              "type": "number",
              "value": "={{ $json.body.time }}"
            },
            {
              "id": "7d82c663-9a67-4dc2-9c20-55e2629215f5",
              "name": "future_value",
              "type": "number",
              "value": "=  {{ $json.body.principal * Math.pow(1 + ($json.body.rate / 100),$json.body.time) }}"
            },
            {
              "id": "c9c5bdd0-e890-4ca9-a889-4af5f8155bc8",
              "name": "profit",
              "type": "number",
              "value": "=  {{ $json.body.principal * Math.pow(1 + ($json.body.rate / 100),$json.body.time) \n-\n$json.body.principal\n}}  "
            },
            {
              "id": "7972be69-688b-4e50-ab05-1ecea29fc666",
              "name": "risk_level",
              "type": "string",
              "value": "={{ $json.body.rate < 8 ? \"Low Risk\" : ($json.body.rate < 15 ? \"Medium Risk\" : \"High Risk\") }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e31e3d33-49e1-4f15-9178-8f40abac84c6",
      "name": "Fetch Market News (Dynamic by Risk)",
      "type": "n8n-nodes-base.rssFeedRead",
      "position": [
        1008,
        48
      ],
      "parameters": {
        "url": "={{ \n  $json.risk_level === \"Low Risk\" \n    ? \"https://news.google.com/rss/search?q=inflation+interest+rates+economy\"\n    : $json.risk_level === \"Medium Risk\"\n    ? \"https://news.google.com/rss/search?q=stock+market+etf+earnings\"\n    : \"https://news.google.com/rss/search?q=crypto+bitcoin+startup+tech+stocks\"\n}}",
        "options": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "89c0d341-3a8a-49e4-a023-d9b4858c6eb3",
      "name": "Latest Market News First",
      "type": "n8n-nodes-base.sort",
      "position": [
        1216,
        48
      ],
      "parameters": {
        "options": {},
        "sortFieldsUi": {
          "sortField": [
            {
              "order": "descending",
              "fieldName": "isoDate"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "51e0dae8-4fba-49f6-b08f-7b30afd67f85",
      "name": "Top 5 Market News",
      "type": "n8n-nodes-base.limit",
      "position": [
        1408,
        48
      ],
      "parameters": {
        "maxItems": 5
      },
      "typeVersion": 1
    },
    {
      "id": "2409e882-1122-4d7d-990c-76326cc4a217",
      "name": "Structure Market News for AI",
      "type": "n8n-nodes-base.code",
      "position": [
        1600,
        48
      ],
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nconst market_data = [];\n\nfor (const item of $input.all()) {\n  market_data.push({\n    \"title\" : item.json.title,\n    \"link\" : item.json.link              \n  })\n}\n\nreturn {\n  json:{\n    market_data\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "07b9b8ad-997b-498c-b0e1-59eb28cbb5e8",
      "name": "AI - Investment Analysis & Suggestions",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1824,
        48
      ],
      "parameters": {
        "text": "=You are a professional financial advisor AI.\n\nAnalyze the following investment scenario and provide a clear, structured response.\n\n### User Input:\u20b9{{ $('Calculate Projection & Risk Profile').item.json.amount }}\n- Investment Amount: \n- Expected Annual Return: {{ $('Calculate Projection & Risk Profile').item.json.rate * 100 }}%\n- Time Horizon: {{ $('Calculate Projection & Risk Profile').item.json.years }} years\n- Calculated Future Value: \u20b9{{ $('Calculate Projection & Risk Profile').item.json.future_value }}\n- Estimated Profit:\u20b9{{ $('Calculate Projection & Risk Profile').item.json.profit }}\n- Risk Profile: {{ $('Calculate Projection & Risk Profile').item.json.risk_level }}\n\n### Market Context:\n{{ \n  $json.market_data\n    .map(item => `- ${item.title} (${item.link})`)\n    .join('\\n')\n}}\n---\n\n### Your Tasks:\n\n1. Explain the investment outcome in SIMPLE terms (2\u20133 sentences).\n2. Interpret whether this is realistic or aggressive.\n3. Provide EXACTLY 3 investment suggestions:\n   - Low Risk Option\n   - Medium Risk Option\n   - High Risk Option\n\nFor EACH suggestion include:\n- Name\n- Why it fits\n- Expected behavior\n- Confidence (Low/Medium/High)\n\n4. Keep response concise, structured, and beginner-friendly.\n\n---\n\n### Output Format (STRICT JSON):\n\n{\n  \"summary\": \"...\",\n  \"insight\": \"...\",\n  \"suggestions\": [\n    {\n      \"type\": \"Low Risk\",\n      \"name\": \"...\",\n      \"reason\": \"...\",\n      \"confidence\": \"...\"\n    },\n    {\n      \"type\": \"Medium Risk\",\n      \"name\": \"...\",\n      \"reason\": \"...\",\n      \"confidence\": \"...\"\n    },\n    {\n      \"type\": \"High Risk\",\n      \"name\": \"...\",\n      \"reason\": \"...\",\n      \"confidence\": \"...\"\n    }\n  ]\n}",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 3.1
    },
    {
      "id": "b7f4b2a9-7b2f-4f04-a19e-e07536f68be3",
      "name": "Format Final Response",
      "type": "n8n-nodes-base.set",
      "position": [
        2160,
        48
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={\n  \"success\" : \"true\",\n  \"response\" : {\n  \"input\" :  {\n    \"amount\" : {{ $('Validate User Input').item.json.body.principal }},\n    \"return\" : {{ $('Validate User Input').item.json.body.rate }},\n    \"years\"  : {{ $('Validate User Input').item.json.body.time }}\n  },\n  \"projection\" : {\n    \"future_value\" : {{ $('Calculate Projection & Risk Profile').item.json.future_value }},\n    \"profit\" : {{ $('Calculate Projection & Risk Profile').item.json.profit }},\n    \"risk_level\" : \"{{ $('Calculate Projection & Risk Profile').item.json.risk_level }}\"\n  },\n  \"ai_analysis\" : \n    {{ $json.output }}\n  }\n}\n"
      },
      "typeVersion": 3.4
    },
    {
      "id": "1e1b218c-b63e-403f-8848-0aa6e9ac50f3",
      "name": "Send Final Output",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        2352,
        48
      ],
      "parameters": {
        "options": {},
        "respondWith": "allIncomingItems"
      },
      "typeVersion": 1.5
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "3c25b1f9-b341-4bb0-a00b-4e14ea84eae6",
  "connections": {
    "Groq Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI - Investment Analysis & Suggestions",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Top 5 Market News": {
      "main": [
        [
          {
            "node": "Structure Market News for AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate User Input": {
      "main": [
        [
          {
            "node": "Invalid Input Error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Calculate Projection & Risk Profile",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Final Response": {
      "main": [
        [
          {
            "node": "Send Final Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Latest Market News First": {
      "main": [
        [
          {
            "node": "Top 5 Market News",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receive Investment Input": {
      "main": [
        [
          {
            "node": "Validate User Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structure Market News for AI": {
      "main": [
        [
          {
            "node": "AI - Investment Analysis & Suggestions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Projection & Risk Profile": {
      "main": [
        [
          {
            "node": "Fetch Market News (Dynamic by Risk)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Market News (Dynamic by Risk)": {
      "main": [
        [
          {
            "node": "Latest Market News First",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI - Investment Analysis & Suggestions": {
      "main": [
        [
          {
            "node": "Format Final Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}