{
  "id": "0tZbdRF64nAiQHSF",
  "tags": [],
  "nodes": [
    {
      "id": "92d4affa-acbe-4114-a8f7-3ff2ba472061",
      "name": "Send Request to Google Gemini",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3720,
        -140
      ],
      "parameters": {
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-thinking-exp-1219:generateContent",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"contents\": [\n    {\n      \"role\": \"user\",\n      \"parts\": [\n        {\n          \"text\": \"{{ $json.query.input }}\"\n        }\n      ]\n    }\n  ],\n  \"generationConfig\": {\n    \"temperature\": 1,\n    \"topK\": 64,\n    \"topP\": 0.95,\n    \"maxOutputTokens\": 8192,\n    \"responseMimeType\": \"text/plain\"\n  }\n} ",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googlePalmApi"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "e2550819-5719-4ba2-8844-71f3bdfb8f6a",
      "name": "Return Response to User",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        4500,
        -140
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={{ $json.response }}"
      },
      "typeVersion": 1.1
    },
    {
      "id": "51d3c73c-9077-4d22-9a3e-e14a460b9c3e",
      "name": "Receive User Input",
      "type": "n8n-nodes-base.webhook",
      "position": [
        3340,
        -140
      ],
      "parameters": {
        "path": "b767b67a-9fab-4f73-97f8-b17a55b8d2be",
        "options": {},
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "e8c643fc-c07a-4b38-a6eb-b6c8c60e09d5",
      "name": "Process API Response",
      "type": "n8n-nodes-base.code",
      "position": [
        4100,
        -140
      ],
      "parameters": {
        "jsCode": "// Retrieve all input data from the previous node\nconst items = $input.all();\n\n// Initialize an array to store the URIs\nlet uris = [];\nlet answer = \"\";\nlet reasoning = \"\";\n\n// Process each input item\nitems.forEach(item => {\n  // Navigate to the citationSources array within citationMetadata\n  const citationSources = item.json.candidates?.[0]?.citationMetadata?.citationSources;\n\n  // Check if citationSources exists and is an array\n  if (Array.isArray(citationSources)) {\n    // Extract the URI from each source and add it to the uris array\n    citationSources.forEach(source => {\n      if (source.uri) {\n        uris.push(source.uri);\n      }\n    });\n  }\n\n  // Extract reasoning and answer\n  reasoning = item.json.candidates?.[0]?.content?.parts?.[0]?.text || \"\";\n  answer = item.json.candidates?.[0]?.content?.parts?.[1]?.text || \"\";\n});\n\n// Wrap the URIs, reasoning, and answer inside a single response object\nreturn uris.length > 0 || reasoning || answer \n  ? [{ json: { response: { citation: uris, reasoning, answer } } }] \n  : [];\n"
      },
      "typeVersion": 2,
      "alwaysOutputData": true
    },
    {
      "id": "dcb63058-5fa5-417e-bce7-a1fc4aea2daa",
      "name": "Sticky Note - Receive User Input",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3220,
        -280
      ],
      "parameters": {
        "width": 340,
        "height": 380,
        "content": "This node receives user input via a webhook and acts as the starting point of the workflow. Make sure to properly configure the path to ensure requests are correctly routed."
      },
      "typeVersion": 1
    },
    {
      "id": "dcc76788-020e-45ff-86d3-4c126f93bf6c",
      "name": "Sticky Note - Send Request to Google Gemini",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3600,
        -280
      ],
      "parameters": {
        "width": 340,
        "height": 380,
        "content": "This node sends a request to the Google Gemini API to generate content based on the user's input. Properly configure parameters like temperature, topK, topP, and maxOutputTokens to customize the response."
      },
      "typeVersion": 1
    },
    {
      "id": "cbf0e447-bb05-410a-a635-c8aa7a5400fe",
      "name": "Sticky Note - Process API Response",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3980,
        -280
      ],
      "parameters": {
        "width": 340,
        "height": 380,
        "content": "This node processes and organizes the response obtained from the API to structure reasoning, answers, and citations, if available. Ensure the JavaScript code is correctly configured."
      },
      "typeVersion": 1
    },
    {
      "id": "1a249638-0cce-4370-978e-6a88923ff544",
      "name": "Sticky Note - Return Response to User",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        4380,
        -280
      ],
      "parameters": {
        "width": 340,
        "height": 380,
        "content": "This node finalizes the workflow by sending the processed response back to the user. Verify that the response format meets client expectations."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6e06959e-0dcd-4c47-adc8-30e5633167e3",
  "connections": {
    "Receive User Input": {
      "main": [
        [
          {
            "node": "Send Request to Google Gemini",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process API Response": {
      "main": [
        [
          {
            "node": "Return Response to User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Request to Google Gemini": {
      "main": [
        [
          {
            "node": "Process API Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}