{
  "name": "cos7-research-assistant",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "id": "652a5882-af61-48ba-a73e-0b126c982419",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        380
      ],
      "webhookId": "2a38dcb0-6b80-4bf2-91e7-fabe5f8aba1e"
    },
    {
      "parameters": {
        "jsCode": "const q = $input.first().json.chatInput ?? '';\nconst encoded = encodeURIComponent(q);\nreturn [{ json: {\n  question: q,\n  esearchUrl: `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=${encoded}&retmax=5&retmode=json`\n}}];"
      },
      "id": "040ecae3-fa9f-4515-94b9-b3d02f23eacd",
      "name": "Plan Search",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        380
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.esearchUrl }}",
        "options": {}
      },
      "id": "aa038c09-9060-480d-9141-93452a3042e8",
      "name": "Tool — PubMed Search",
      "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: { paperList: 'No papers found. Try different search terms.', question: $('Plan Search').first().json.question } }];\nconst esummaryUrl = `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=${ids.join(',')}&retmode=json`;\nreturn [{ json: { ids: ids.join(','), esummaryUrl, question: $('Plan Search').first().json.question } }];"
      },
      "id": "db370336-b06c-45df-bd3a-97273b17dbb9",
      "name": "Extract IDs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        380
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.esummaryUrl }}",
        "options": {}
      },
      "id": "ab547ab8-d243-455a-856b-eb2777f6168b",
      "name": "Tool — Paper Details",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        380
      ]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst question = $('Extract IDs').first().json.question;\nconst ids = Object.keys(data.result || {}).filter(k => k !== 'uids');\nconst papers = ids.map(id => {\n  const p = data.result[id];\n  const authors = p.authors?.map(a => a.name).slice(0,3).join(', ') || 'Unknown';\n  return `PMID ${id}: \"${p.title}\" — ${authors} (${p.pubdate?.split(' ')[0] || 'n.d.'}) [${p.source || ''}]`;\n}).join('\\n');\nreturn [{ json: { paperList: papers || 'No details available.', question } }];"
      },
      "id": "0210a96e-8819-4621-8232-f65706bef9b6",
      "name": "Format Paper List",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        380
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.question }}",
        "options": {
          "systemMessage": "You are a scientific research assistant. Using the tool results below, answer the user's research question.\n\nPAPERS FOUND:\n{{ $json.paperList }}\n\nFor each relevant paper, explain what it contributes to the user's question. Identify patterns, gaps, or conflicts across the papers. Always cite PMIDs when referencing a specific paper. If no papers were found, suggest alternative search strategies."
        }
      },
      "id": "1cf0f6ef-1854-4084-ace8-63e59e10c2a0",
      "name": "AI Research Synthesizer",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1560,
        380
      ]
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.3
        }
      },
      "id": "a9d70698-60f3-4571-a5d3-eea312d8660b",
      "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": "892f16c1-83fc-44e9-9c5d-b84ba74ded1f",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1760,
        560
      ]
    },
    {
      "parameters": {
        "content": "## Session 07 — Research Assistant\n\n**n8n's tool-use model:**\nInstead of an autonomous agent deciding WHEN to call tools, n8n makes the tool calls deterministic:\n1. **PubMed Search** (esearch) = search tool — finds relevant PMIDs\n2. **Paper Details** (esummary) = details tool — gets title/author/date\n3. **AI Synthesizer** = reasoning layer — answers using tool results\n\nThis is the same as an agent ReAct loop, but explicit and inspectable at every step.\n\n**Try it:** 'What are the latest approaches to CRISPR base editing?'\n**Try follow-up:** 'Which of those papers uses clinical trial data?'\n\n**Extend:** Add a third HTTP node for Semantic Scholar to get citation counts.",
        "height": 280,
        "width": 460
      },
      "id": "19511a73-8290-4bde-b43d-774fb03deea6",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        80
      ]
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Plan Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Plan Search": {
      "main": [
        [
          {
            "node": "Tool — PubMed Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool — PubMed Search": {
      "main": [
        [
          {
            "node": "Extract IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract IDs": {
      "main": [
        [
          {
            "node": "Tool — Paper Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool — Paper Details": {
      "main": [
        [
          {
            "node": "Format Paper List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Paper List": {
      "main": [
        [
          {
            "node": "AI Research Synthesizer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq": {
      "ai_languageModel": [
        [
          {
            "node": "AI Research Synthesizer",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Research Synthesizer",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}