{
  "name": "Build a Free Local AI Personal Assistant with Ollama",
  "nodes": [
    {
      "id": "b315eb9f-de25-4404-a247-6d7eda8adb2e",
      "name": "Telegram Message",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.5,
      "position": [0, 192],
      "parameters": {
        "updates": ["message"],
        "additionalFields": {
          "chatIds": "YOUR_TELEGRAM_CHAT_ID"
        }
      }
    },
    {
      "id": "b91924b3-9d9e-4d07-834f-924f3158b99f",
      "name": "File Attached?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [224, 192],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{ !!($json.message.document || $json.message.photo) }}",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "rightValue": ""
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "9f9c763b-3cd8-4423-8b39-1d08d7b16c2c",
      "name": "Download Attached File",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1248, 0],
      "parameters": {
        "resource": "file",
        "operation": "get",
        "fileId": "={{ $json.message.document ? $json.message.document.file_id : $json.message.photo[$json.message.photo.length - 1].file_id }}",
        "download": true
      }
    },
    {
      "id": "fd32af90-2cd8-4712-80d2-ad1099f26bd5",
      "name": "Extract Text With Tika",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [1472, 96],
      "parameters": {
        "method": "PUT",
        "url": "http://tika:9998/tika",
        "sendHeaders": true,
        "specifyHeaders": "keypair",
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "text/plain"
            }
          ]
        },
        "sendBody": true,
        "contentType": "binaryData",
        "inputDataFieldName": "data",
        "options": {
          "timeout": 120000,
          "response": {
            "response": {
              "responseFormat": "text",
              "outputPropertyName": "data"
            }
          }
        }
      }
    },
    {
      "id": "dee348c4-df6d-4eff-99b5-fb9a3f6d4221",
      "name": "Build Prompt With File",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [1696, 96],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "prompt-file",
              "name": "prompt",
              "type": "string",
              "value": "=Answer using the document below. If the answer is not in the document, say so. If the user gave no instruction, summarize the document.\n\n--- DOCUMENT ---\n{{ ($json.data || \"\").slice(0, 12000) }}\n--- END DOCUMENT ---\n\nUser message: {{ $(\"Telegram Message\").item.json.message.caption || $(\"Telegram Message\").item.json.message.text || \"Summarize this document.\" }}"
            }
          ]
        }
      }
    },
    {
      "id": "0e3ea135-1079-46d4-a79a-cbd0b79ad7f2",
      "name": "Personal Assistant",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [1920, 192],
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.prompt }}",
        "options": {
          "systemMessage": "You are a helpful, concise personal assistant running locally on the user's own server, chatting through Telegram. Give clear, direct answers in plain text. When a document is provided, base your answer on it and say when the document does not contain the answer. Never invent facts."
        }
      }
    },
    {
      "id": "a72355f9-3f99-4da4-a961-3f943a29890a",
      "name": "Ollama Assistant Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [1936, 416],
      "parameters": {
        "model": "qwen2.5:14b-instruct-q5_K_M",
        "options": {
          "temperature": 0.3,
          "numCtx": 16384,
          "keepAlive": "10m"
        }
      }
    },
    {
      "id": "1278008a-9b54-43f4-9959-a051c30a54d3",
      "name": "Assistant Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.4,
      "position": [2064, 416],
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Telegram Message').item.json.message.chat.id }}",
        "contextWindowLength": 6
      }
    },
    {
      "id": "77a82572-6ecd-4db0-868f-388af903b714",
      "name": "Prepare Telegram Reply",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [2272, 192],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "reply-text",
              "name": "reply",
              "type": "string",
              "value": "={{ String($json.output).replaceAll(\"&\", \"&amp;\").replaceAll(\"<\", \"&lt;\").replaceAll(\">\", \"&gt;\").slice(0, 4000) }}"
            }
          ]
        }
      }
    },
    {
      "id": "fcce470e-d37c-434b-bc51-59729bb05e9d",
      "name": "Reply in Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [2496, 192],
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "={{ $('Telegram Message').item.json.message.chat.id }}",
        "text": "={{ $json.reply }}",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "HTML"
        }
      }
    },
    {
      "id": "dc5204d5-2e2b-4faa-bdd8-0f9f658dfc27",
      "name": "Extract File Search Term",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.9,
      "position": [448, 320],
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.message.text || 'NONE' }}",
        "messages": {
          "messageValues": [
            {
              "type": "SystemMessagePromptTemplate",
              "message": "The user is asking about a file stored in a Google Drive folder. Reply with ONLY the one to three words most likely to appear in that file's name, separated by single spaces, in lowercase. Never use underscores, hyphens, quotes, punctuation or file extensions. Leave out filler words like my, the, document, file, summary, please. If the message does not mention any file or document, reply with the single word NONE."
            }
          ]
        }
      }
    },
    {
      "id": "88fb8bab-2af1-4c65-aafa-402d260b36ea",
      "name": "Ollama Search Term Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [528, 544],
      "parameters": {
        "model": "qwen2.5:14b-instruct-q5_K_M",
        "options": {
          "temperature": 0,
          "numCtx": 16384,
          "keepAlive": "10m"
        }
      }
    },
    {
      "id": "2054f85e-1905-45c0-8905-ee603f5e9a8a",
      "name": "Search Drive Folder",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [800, 320],
      "parameters": {
        "resource": "fileFolder",
        "operation": "search",
        "authentication": "oAuth2",
        "searchMethod": "query",
        "queryString": "={{ ($json.text || \"\").replace(/[^a-zA-Z0-9 ]/g, \" \").split(\" \").filter(w => w.length > 2).map(w => \"name contains '\" + w + \"'\").join(\" or \") || \"name = '__no_match__'\" }}",
        "returnAll": false,
        "limit": 1,
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "id",
            "value": "YOUR_GOOGLE_DRIVE_FOLDER_ID"
          },
          "whatToSearch": "files"
        }
      },
      "alwaysOutputData": true
    },
    {
      "id": "85c835b1-51fb-493f-8c8c-39b0f44aee6f",
      "name": "Matching File Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [1024, 320],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{ !!$json.id }}",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "rightValue": ""
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "358c93eb-05ac-4d1a-85c6-b9882b9eabed",
      "name": "Download Drive File",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [1248, 224],
      "parameters": {
        "resource": "file",
        "operation": "download",
        "authentication": "oAuth2",
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {
          "binaryPropertyName": "data",
          "googleFileConversion": {
            "conversion": {
              "docsToFormat": "application/pdf",
              "slidesToFormat": "application/pdf",
              "sheetsToFormat": "text/csv"
            }
          }
        }
      }
    },
    {
      "id": "faaec920-dde9-4a8a-9a01-66a9f1d99bb5",
      "name": "Build Prompt No File Found",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [1696, 384],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "prompt-nofile",
              "name": "prompt",
              "type": "string",
              "value": "=No file was attached and no matching file was found in the Google Drive folder. Tell the user that clearly, then answer from general knowledge if you can.\n\nUser message: {{ $(\"Telegram Message\").item.json.message.text || \"(no text)\" }}"
            }
          ]
        }
      }
    }
  ],
  "connections": {
    "Telegram Message": { "main": [[{ "node": "File Attached?", "type": "main", "index": 0 }]] },
    "File Attached?": {
      "main": [
        [{ "node": "Download Attached File", "type": "main", "index": 0 }],
        [{ "node": "Extract File Search Term", "type": "main", "index": 0 }]
      ]
    },
    "Download Attached File": { "main": [[{ "node": "Extract Text With Tika", "type": "main", "index": 0 }]] },
    "Extract Text With Tika": { "main": [[{ "node": "Build Prompt With File", "type": "main", "index": 0 }]] },
    "Build Prompt With File": { "main": [[{ "node": "Personal Assistant", "type": "main", "index": 0 }]] },
    "Personal Assistant": { "main": [[{ "node": "Prepare Telegram Reply", "type": "main", "index": 0 }]] },
    "Ollama Assistant Model": { "ai_languageModel": [[{ "node": "Personal Assistant", "type": "ai_languageModel", "index": 0 }]] },
    "Assistant Memory": { "ai_memory": [[{ "node": "Personal Assistant", "type": "ai_memory", "index": 0 }]] },
    "Prepare Telegram Reply": { "main": [[{ "node": "Reply in Telegram", "type": "main", "index": 0 }]] },
    "Extract File Search Term": { "main": [[{ "node": "Search Drive Folder", "type": "main", "index": 0 }]] },
    "Ollama Search Term Model": { "ai_languageModel": [[{ "node": "Extract File Search Term", "type": "ai_languageModel", "index": 0 }]] },
    "Search Drive Folder": { "main": [[{ "node": "Matching File Found?", "type": "main", "index": 0 }]] },
    "Matching File Found?": {
      "main": [
        [{ "node": "Download Drive File", "type": "main", "index": 0 }],
        [{ "node": "Build Prompt No File Found", "type": "main", "index": 0 }]
      ]
    },
    "Download Drive File": { "main": [[{ "node": "Extract Text With Tika", "type": "main", "index": 0 }]] },
    "Build Prompt No File Found": { "main": [[{ "node": "Personal Assistant", "type": "main", "index": 0 }]] }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
