{
  "name": "cos5-pubmed-query",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "id": "b1c6f902-6179-4fda-ad8c-9a9ad197e758",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        380
      ],
      "webhookId": "6e79bf26-fedc-469f-8f40-dd8aeb3c55a3"
    },
    {
      "parameters": {
        "jsCode": "const q = $input.first().json.chatInput ?? '';\nconst encoded = encodeURIComponent(q);\nreturn [{ json: {\n  searchTerm: q,\n  esearchUrl: `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=${encoded}&retmax=5&retmode=json`\n}}];"
      },
      "id": "4f1f81fa-b2e2-44e8-886e-80721523c0bf",
      "name": "Build Search URL",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        380
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.esearchUrl }}",
        "options": {}
      },
      "id": "b11863a9-0a70-4281-becf-0b235b522cbd",
      "name": "Search PubMed",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        380
      ]
    },
    {
      "parameters": {
        "jsCode": "const r = $input.first().json;\nconst ids = r.esearchresult?.idlist ?? [];\nif (!ids.length) return [{ json: { ids: '', efetchUrl: '', count: 0 }}];\nconst efetchUrl = `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=${ids.join(',')}&rettype=abstract&retmode=text`;\nreturn [{ json: { ids: ids.join(','), efetchUrl, count: r.esearchresult.count } }];"
      },
      "id": "93d693a0-4b47-428c-9958-e3a18887faab",
      "name": "Extract IDs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        380
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.efetchUrl }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "text"
            }
          }
        }
      },
      "id": "c6083fd4-984e-440f-a59e-00834094babc",
      "name": "Fetch Abstracts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        380
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1f2f8ca7-24c2-40a9-8bf0-4421656bda5f",
              "name": "abstracts",
              "value": "={{ $json.data.substring(0, 8000) }}",
              "type": "string"
            },
            {
              "id": "3053bd35-0376-4bb1-a500-d2a0413cf8f9",
              "name": "question",
              "value": "={{ $('When chat message received').first().json.chatInput }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "04b09a0a-7d8d-4862-bb5e-6cf5ba0130c3",
      "name": "Prepare Context",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1340,
        380
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.question }}",
        "options": {
          "systemMessage": "You are a scientific research assistant. The user asked a question and you retrieved relevant PubMed abstracts.\n\nABSTRACTS:\n{{ $json.abstracts }}\n\nAnswer based on these abstracts. List key papers (title, year, key finding). If no abstracts were fetched, say so and suggest better search terms."
        }
      },
      "id": "12dd863d-1f05-4e5e-8121-47b99109a52d",
      "name": "AI Research Assistant",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1560,
        380
      ]
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.3
        }
      },
      "id": "c4deedde-13e6-4a6c-9c2c-a5a8e642c9fd",
      "name": "Groq",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "typeVersion": 1,
      "position": [
        1560,
        560
      ],
      "credentials": {
        "groqApi": {
          "id": "groq-shared-cred",
          "name": "Groq (shared, students)"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('When chat message received').first().json.sessionId }}",
        "contextWindowLength": 8
      },
      "id": "345b08b8-f86a-443e-9d21-3e9c19c79e0d",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1760,
        560
      ]
    },
    {
      "parameters": {
        "content": "## Session 05 — Pulling data from scientific databases\n\n**Two-step PubMed pattern:**\n1. `esearch` — converts your query to PubMed IDs (no abstracts yet)\n2. `efetch` — retrieves full plain-text abstracts for those IDs\n\nThe AI reads the abstracts and answers your question.\n\n**Try it:** 'Find papers on CRISPR base editing 2024'\n**Extend:** Change `retmax=5` in Build Search URL to get more results.\n**Other databases:** Swap `db=pubmed` for `db=protein` (UniProt) or `db=nucleotide` (GenBank).",
        "height": 220,
        "width": 420
      },
      "id": "563f7dcc-a644-445f-8361-902c00c113b3",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        100
      ]
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Build Search URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Search URL": {
      "main": [
        [
          {
            "node": "Search PubMed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search PubMed": {
      "main": [
        [
          {
            "node": "Extract IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract IDs": {
      "main": [
        [
          {
            "node": "Fetch Abstracts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Abstracts": {
      "main": [
        [
          {
            "node": "Prepare Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Context": {
      "main": [
        [
          {
            "node": "AI Research Assistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq": {
      "ai_languageModel": [
        [
          {
            "node": "AI Research Assistant",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Research Assistant",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}