Chatbot Zalo – Truy Vấn Tài Liệu Nội bộ | Mido AI


Chatbot Zalo cá nhân & n8n: Giải pháp “Trợ Lý Ảo” thông minh cho cá nhân và doanh nghiệp nhỏ

Cách Xây Dựng Chatbot Zalo Cá Nhân Với n8n

Để triển khai thành công, bạn cần chuẩn bị một số yếu tố cơ bản:

Bước 1: Chuẩn Bị Công Cụ

  • Tài khoản Zalo cá nhân: nơi khách hàng sẽ nhắn tin.

  • n8n: có thể dùng bản cloud hoặc cài đặt local/self-host.

  • Access token Zalo: phục vụ cho việc gửi – nhận tin nhắn qua API.

Bước 2: Tạo Workflow Cơ Bản Trong n8n

  1. Webhook Trigger: n8n nhận tin nhắn từ khách gửi đến Zalo.

  2. Xử lý logic (Function/IF Node): phân loại tin nhắn, xác định kịch bản.

  3. HTTP Request Node: gửi phản hồi ngược lại qua API của Zalo.

  4. Action node : tuỳ biến vào nhu cầu công việc muốn trợ lý ảo thực hiện

Bước 3: Tùy Biến & Cá Nhân Hóa

  • Cài đặt kịch bản phản hồi cho từng tình huống (truy vấn tài liệu hỏi sản phẩm, bảo hành).

  • Tích hợp AI/LLM để chatbot trả lời thông minh hơn.

  • Thêm bước thống kê: mỗi ngày gửi báo cáo số lượng khách mới qua email.

Bước 4: Kiểm Tra & Vận Hành

  • Kiểm tra thử tin nhắn trên Zalo cá nhân.

  • Chạy test toàn bộ workflow để đảm bảo không lỗi.

  • Triển khai thực tế cho khách hàng và theo dõi hiệu suất.

Hướng dẫn sử dụng tại https://youtu.be/_LE22g2YLPQ

Download workflow có sẵn tại: https://workflow.midoai.edu.vn/

Xem thêm tại: https://midoai.edu.vn/chatbot-zalo-ca-nhan-n8n/

Nội dung File JSON

{
    "name": "My workflow 8",
    "nodes": [
        {
            "parameters": {
                "httpMethod": "POST",
                "path": "481e66d2-992c-410a-b21c-f8907fd0c413",
                "options": {}
            },
            "type": "n8n-nodes-base.webhook",
            "typeVersion": 2.1,
            "position": [
                256,
                224
            ],
            "id": "641b6329-c5b9-4bbe-89b3-48cbcade1de2",
            "name": "Webhook",
            "webhookId": "481e66d2-992c-410a-b21c-f8907fd0c413"
        },
        {
            "parameters": {
                "options": {}
            },
            "id": "9511ce32-50f4-479c-b03d-776b27284992",
            "name": "OpenAI Chat Model",
            "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
            "typeVersion": 1,
            "position": [
                512,
                448
            ],
            "credentials": {
                "openAiApi": {
                    "id": "yS1gimiL6UYP0t5O",
                    "name": "GPT"
                }
            }
        },
        {
            "parameters": {
                "jsonMode": "expressionData",
                "jsonData": "={{ $json.data || $json.text || $json.concatenated_data }}",
                "options": {
                    "metadata": {
                        "metadataValues": [
                            {
                                "name": "=file_id",
                                "value": "={{ $('Set File ID').first().json.file_id }}"
                            },
                            {
                                "name": "file_title",
                                "value": "={{ $('Set File ID').first().json.file_title }}"
                            }
                        ]
                    }
                }
            },
            "id": "db812271-3665-4cee-ae29-8db6a5af4459",
            "name": "Default Data Loader",
            "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
            "typeVersion": 1,
            "position": [
                2256,
                1232
            ]
        },
        {
            "parameters": {
                "model": "text-embedding-3-small",
                "options": {}
            },
            "id": "e838ae0b-79cb-480e-8a14-8c5946e46b86",
            "name": "Embeddings OpenAI1",
            "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
            "typeVersion": 1,
            "position": [
                2016,
                1232
            ],
            "credentials": {
                "openAiApi": {
                    "id": "yS1gimiL6UYP0t5O",
                    "name": "GPT"
                }
            }
        },
        {
            "parameters": {
                "content": "## Agent Tools for RAG",
                "height": 528.85546469693,
                "width": 583.4552380860637,
                "color": 4
            },
            "id": "2c833d97-90af-4979-9789-80f8e1f208ed",
            "name": "Sticky Note",
            "type": "n8n-nodes-base.stickyNote",
            "typeVersion": 1,
            "position": [
                1248,
                80
            ]
        },
        {
            "parameters": {
                "content": "## Tool to Add a Google Drive File to Vector DB",
                "height": 867,
                "width": 3073,
                "color": 5
            },
            "id": "1d21b2d7-01c8-496d-886c-95962eb84932",
            "name": "Sticky Note1",
            "type": "n8n-nodes-base.stickyNote",
            "typeVersion": 1,
            "position": [
                -496,
                624
            ]
        },
        {
            "parameters": {
                "operation": "download",
                "fileId": {
                    "__rl": true,
                    "value": "={{ $('Set File ID').item.json.file_id }}",
                    "mode": "id"
                },
                "options": {
                    "googleFileConversion": {
                        "conversion": {
                            "docsToFormat": "text/plain"
                        }
                    }
                }
            },
            "id": "967c9fb6-ddd7-4665-ba7b-03f1d4c8c072",
            "name": "Download File",
            "type": "n8n-nodes-base.googleDrive",
            "typeVersion": 3,
            "position": [
                592,
                912
            ],
            "executeOnce": true,
            "credentials": {
                "googleDriveOAuth2Api": {
                    "id": "UPZIEJXghJrHC8fL",
                    "name": "Driver zinc"
                }
            }
        },
        {
            "parameters": {
                "pollTimes": {
                    "item": [
                        {
                            "mode": "everyMinute"
                        }
                    ]
                },
                "triggerOn": "specificFolder",
                "folderToWatch": {
                    "__rl": true,
                    "value": "1uIf6ijjUQirzm2wzdcMZrszk4VVyZfQN",
                    "mode": "list",
                    "cachedResultName": "chat_bot_2_cap",
                    "cachedResultUrl": "https://drive.google.com/drive/folders/1uIf6ijjUQirzm2wzdcMZrszk4VVyZfQN"
                },
                "event": "fileCreated",
                "options": {}
            },
            "id": "6a78f676-1afb-4f8b-843f-dc6cdcad5a82",
            "name": "File Created",
            "type": "n8n-nodes-base.googleDriveTrigger",
            "typeVersion": 1,
            "position": [
                -432,
                752
            ],
            "credentials": {
                "googleDriveOAuth2Api": {
                    "id": "UPZIEJXghJrHC8fL",
                    "name": "Driver zinc"
                }
            }
        },
        {
            "parameters": {
                "pollTimes": {
                    "item": [
                        {
                            "mode": "everyMinute"
                        }
                    ]
                },
                "triggerOn": "specificFolder",
                "folderToWatch": {
                    "__rl": true,
                    "value": "1uIf6ijjUQirzm2wzdcMZrszk4VVyZfQN",
                    "mode": "list",
                    "cachedResultName": "chat_bot_2_cap",
                    "cachedResultUrl": "https://drive.google.com/drive/folders/1uIf6ijjUQirzm2wzdcMZrszk4VVyZfQN"
                },
                "event": "fileUpdated",
                "options": {}
            },
            "id": "3b21bdea-6b54-48f7-9997-10f94c2f4bf3",
            "name": "File Updated",
            "type": "n8n-nodes-base.googleDriveTrigger",
            "typeVersion": 1,
            "position": [
                -432,
                912
            ],
            "credentials": {
                "googleDriveOAuth2Api": {
                    "id": "UPZIEJXghJrHC8fL",
                    "name": "Driver zinc"
                }
            }
        },
        {
            "parameters": {
                "operation": "text",
                "options": {}
            },
            "id": "7698990d-ec65-44f3-a1a4-323b8911d073",
            "name": "Extract Document Text",
            "type": "n8n-nodes-base.extractFromFile",
            "typeVersion": 1,
            "position": [
                1456,
                1232
            ],
            "alwaysOutputData": true
        },
        {
            "parameters": {
                "sessionIdType": "customKey",
                "sessionKey": "={{ $json.chat_id}}"
            },
            "id": "1c0fb96d-a427-4134-816e-94a986b2c7ec",
            "name": "Postgres Chat Memory",
            "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
            "typeVersion": 1,
            "position": [
                640,
                448
            ],
            "notesInFlow": false,
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "operation": "delete",
                "tableId": "documents",
                "filterType": "string",
                "filterString": "=metadata->>file_id=like.*{{ $json.file_id }}*"
            },
            "id": "2d8d8387-cf09-4801-b1e1-9be76b9e01fb",
            "name": "Delete Old Doc Rows",
            "type": "n8n-nodes-base.supabase",
            "typeVersion": 1,
            "position": [
                112,
                752
            ],
            "alwaysOutputData": true,
            "credentials": {
                "supabaseApi": {
                    "id": "rIRiq7wME466hZCt",
                    "name": "Vector A Dung"
                }
            }
        },
        {
            "parameters": {
                "assignments": {
                    "assignments": [
                        {
                            "id": "10646eae-ae46-4327-a4dc-9987c2d76173",
                            "name": "file_id",
                            "value": "={{ $json.id }}",
                            "type": "string"
                        },
                        {
                            "id": "f4536df5-d0b1-4392-bf17-b8137fb31a44",
                            "name": "file_type",
                            "value": "={{ $json.mimeType }}",
                            "type": "string"
                        },
                        {
                            "id": "77d782de-169d-4a46-8a8e-a3831c04d90f",
                            "name": "file_title",
                            "value": "={{ $json.name }}",
                            "type": "string"
                        },
                        {
                            "id": "9bde4d7f-e4f3-4ebd-9338-dce1350f9eab",
                            "name": "file_url",
                            "value": "={{ $json.webViewLink }}",
                            "type": "string"
                        }
                    ]
                },
                "options": {}
            },
            "id": "bdbf8057-d9c8-4796-9318-4b6711a5b121",
            "name": "Set File ID",
            "type": "n8n-nodes-base.set",
            "typeVersion": 3.4,
            "position": [
                -80,
                912
            ]
        },
        {
            "parameters": {
                "content": "## RAG AI Agent with MIDO AI",
                "height": 464.8027193303974,
                "width": 1035.6381264595484
            },
            "id": "46a736f7-f9c2-4ff6-802d-cb43e67bd7d0",
            "name": "Sticky Note2",
            "type": "n8n-nodes-base.stickyNote",
            "typeVersion": 1,
            "position": [
                208,
                144
            ]
        },
        {
            "parameters": {
                "assignments": {
                    "assignments": [
                        {
                            "id": "9a9a245e-f1a1-4282-bb02-a81ffe629f0f",
                            "name": "chatInput",
                            "value": "={{ $json.body.message.text }}",
                            "type": "string"
                        },
                        {
                            "id": "b80831d8-c653-4203-8706-adedfdb98f77",
                            "name": "chat_id",
                            "value": "={{ $json.body.message.chat.id }}",
                            "type": "string"
                        }
                    ]
                },
                "options": {}
            },
            "id": "a63cc6a2-5c7d-4b7e-a2ff-dbb9f6d4a6f1",
            "name": "Edit Fields",
            "type": "n8n-nodes-base.set",
            "typeVersion": 3.4,
            "position": [
                512,
                224
            ]
        },
        {
            "parameters": {
                "operation": "pdf",
                "options": {}
            },
            "id": "7718de56-35d2-4101-bb1b-df31deaedcbc",
            "name": "Extract PDF Text",
            "type": "n8n-nodes-base.extractFromFile",
            "typeVersion": 1,
            "position": [
                1456,
                672
            ]
        },
        {
            "parameters": {
                "aggregate": "aggregateAllItemData",
                "options": {}
            },
            "id": "f65cabce-69ec-4927-8d9f-e57d9e2a7bb2",
            "name": "Aggregate",
            "type": "n8n-nodes-base.aggregate",
            "typeVersion": 1,
            "position": [
                1488,
                848
            ]
        },
        {
            "parameters": {},
            "id": "6ff52b34-5685-4f91-ae3a-b24daa5d5fa3",
            "name": "Character Text Splitter",
            "type": "@n8n/n8n-nodes-langchain.textSplitterCharacterTextSplitter",
            "typeVersion": 1,
            "position": [
                2160,
                1344
            ]
        },
        {
            "parameters": {
                "fieldsToSummarize": {
                    "values": [
                        {
                            "aggregation": "concatenate",
                            "field": "data"
                        }
                    ]
                },
                "options": {}
            },
            "id": "7fe7b9bb-4620-478b-bad7-657c048dad19",
            "name": "Summarize",
            "type": "n8n-nodes-base.summarize",
            "typeVersion": 1,
            "position": [
                1696,
                928
            ]
        },
        {
            "parameters": {
                "promptType": "define",
                "text": "={{ $json.chatInput }}",
                "options": {
                    "systemMessage": "You are a personal assistant who helps answer questions from a corpus of documents. The documents are either text based (Txt, docs, extracted PDFs, etc.) or tabular data (CSVs or Excel documents).\n\nYou are given tools to perform RAG in the 'documents' table, look up the documents available in your knowledge base in the 'document_metadata' table, extract all the text from a given document, and query the tabular files with SQL in the 'document_rows' table.\n\nAlways start by performing RAG unless the question requires a SQL query for tabular data (fetching a sum, finding a max, something a RAG lookup would be unreliable for). If RAG doesn't help, then look at the documents that are available to you, find a few that you think would contain the answer, and then analyze those.\n\nAlways tell the user if you didn't find the answer. Don't make something up just to please them."
                }
            },
            "id": "b8bf976b-9eac-43bf-bac6-e96dc42918e0",
            "name": "RAG AI Agent",
            "type": "@n8n/n8n-nodes-langchain.agent",
            "typeVersion": 1.6,
            "position": [
                720,
                224
            ]
        },
        {
            "parameters": {
                "rules": {
                    "values": [
                        {
                            "conditions": {
                                "options": {
                                    "caseSensitive": true,
                                    "leftValue": "",
                                    "typeValidation": "strict",
                                    "version": 1
                                },
                                "conditions": [
                                    {
                                        "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                                        "rightValue": "application/pdf",
                                        "operator": {
                                            "type": "string",
                                            "operation": "equals"
                                        }
                                    }
                                ],
                                "combinator": "and"
                            }
                        },
                        {
                            "conditions": {
                                "options": {
                                    "caseSensitive": true,
                                    "leftValue": "",
                                    "typeValidation": "strict",
                                    "version": 1
                                },
                                "conditions": [
                                    {
                                        "id": "2ae7faa7-a936-4621-a680-60c512163034",
                                        "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                                        "rightValue": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                        "operator": {
                                            "type": "string",
                                            "operation": "equals",
                                            "name": "filter.operator.equals"
                                        }
                                    }
                                ],
                                "combinator": "and"
                            }
                        },
                        {
                            "conditions": {
                                "options": {
                                    "caseSensitive": true,
                                    "leftValue": "",
                                    "typeValidation": "strict",
                                    "version": 1
                                },
                                "conditions": [
                                    {
                                        "id": "fc193b06-363b-4699-a97d-e5a850138b0e",
                                        "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                                        "rightValue": "=application/vnd.google-apps.spreadsheet",
                                        "operator": {
                                            "type": "string",
                                            "operation": "equals",
                                            "name": "filter.operator.equals"
                                        }
                                    }
                                ],
                                "combinator": "and"
                            }
                        },
                        {
                            "conditions": {
                                "options": {
                                    "caseSensitive": true,
                                    "leftValue": "",
                                    "typeValidation": "strict",
                                    "version": 1
                                },
                                "conditions": [
                                    {
                                        "id": "b69f5605-0179-4b02-9a32-e34bb085f82d",
                                        "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                                        "rightValue": "application/vnd.google-apps.document",
                                        "operator": {
                                            "type": "string",
                                            "operation": "equals",
                                            "name": "filter.operator.equals"
                                        }
                                    }
                                ],
                                "combinator": "and"
                            }
                        }
                    ]
                },
                "options": {
                    "fallbackOutput": 3
                }
            },
            "id": "e97102b2-3671-49b2-a7c7-176fc73b485a",
            "name": "Switch",
            "type": "n8n-nodes-base.switch",
            "typeVersion": 3,
            "position": [
                784,
                880
            ]
        },
        {
            "parameters": {
                "mode": "insert",
                "tableName": {
                    "__rl": true,
                    "value": "documents",
                    "mode": "list",
                    "cachedResultName": "documents"
                },
                "options": {
                    "queryName": "match_documents"
                }
            },
            "id": "0db0f032-6c4a-4484-8be5-8527c90b6722",
            "name": "Insert into Supabase Vectorstore",
            "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
            "typeVersion": 1,
            "position": [
                2176,
                1008
            ],
            "credentials": {
                "supabaseApi": {
                    "id": "rIRiq7wME466hZCt",
                    "name": "Vector A Dung"
                }
            }
        },
        {
            "parameters": {
                "operation": "xlsx",
                "options": {}
            },
            "id": "4692d7ca-7a4c-4e21-9711-a83197ba8b76",
            "name": "Extract from Excel",
            "type": "n8n-nodes-base.extractFromFile",
            "typeVersion": 1,
            "position": [
                1280,
                848
            ]
        },
        {
            "parameters": {
                "assignments": {
                    "assignments": [
                        {
                            "id": "f422e2e0-381c-46ea-8f38-3f58c501d8b9",
                            "name": "schema",
                            "value": "={{ $('Extract from Excel').isExecuted ? $('Extract from Excel').first().json.keys().toJsonString() : $('Extract from CSV').first().json.keys().toJsonString() }}",
                            "type": "string"
                        },
                        {
                            "id": "bb07c71e-5b60-4795-864c-cc3845b6bc46",
                            "name": "data",
                            "value": "={{ $json.concatenated_data }}",
                            "type": "string"
                        }
                    ]
                },
                "options": {}
            },
            "type": "n8n-nodes-base.set",
            "typeVersion": 3.4,
            "position": [
                2128,
                784
            ],
            "id": "93d636cf-6ed2-4be6-ba62-61a9525efa5f",
            "name": "Set Schema"
        },
        {
            "parameters": {
                "options": {}
            },
            "type": "n8n-nodes-base.extractFromFile",
            "typeVersion": 1,
            "position": [
                1280,
                1024
            ],
            "id": "2ead11ca-b71c-40e5-9eb6-5ed78ffbef8f",
            "name": "Extract from CSV"
        },
        {
            "parameters": {
                "content": "## Run Each Node Once to Set Up Database Tables | Midoai.edu.vn",
                "height": 300,
                "width": 680,
                "color": 3
            },
            "type": "n8n-nodes-base.stickyNote",
            "position": [
                -496,
                304
            ],
            "typeVersion": 1,
            "id": "006ae5f1-7117-4b58-9ed0-d1bf6a19aad3",
            "name": "Sticky Note3"
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "CREATE TABLE document_metadata (\n    id TEXT PRIMARY KEY,\n    title TEXT,\n    url TEXT,\n    created_at TIMESTAMP DEFAULT NOW(),\n    schema TEXT\n);",
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.5,
            "position": [
                -240,
                400
            ],
            "id": "4cccccf8-8d54-4a7c-af74-5a25b34fb1d1",
            "name": "Create Document Metadata Table",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "CREATE TABLE document_rows (\n    id SERIAL PRIMARY KEY,\n    dataset_id TEXT REFERENCES document_metadata(id),\n    row_data JSONB  -- Store the actual row data\n);",
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.5,
            "position": [
                -16,
                400
            ],
            "id": "aa32a9da-8ef9-46b0-8b2b-2dea7b37506c",
            "name": "Create Document Rows Table (for Tabular Data)",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "descriptionType": "manual",
                "toolDescription": "Use this tool to fetch all available documents, including the table schema if the file is a CSV or Excel file.",
                "operation": "select",
                "schema": {
                    "__rl": true,
                    "mode": "list",
                    "value": "public"
                },
                "table": {
                    "__rl": true,
                    "value": "document_metadata",
                    "mode": "list",
                    "cachedResultName": "document_metadata"
                },
                "returnAll": true,
                "options": {}
            },
            "type": "n8n-nodes-base.postgresTool",
            "typeVersion": 2.5,
            "position": [
                784,
                448
            ],
            "id": "88914a6e-ad0f-4b94-a906-319e15c164d7",
            "name": "List Documents",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "descriptionType": "manual",
                "toolDescription": "Given a file ID, fetches the text from the document.",
                "operation": "executeQuery",
                "query": "SELECT \n    string_agg(content, ' ') as document_text\nFROM documents\n  WHERE metadata->>'file_id' = $1\nGROUP BY metadata->>'file_id';",
                "options": {
                    "queryReplacement": "={{ $fromAI('file_id') }}"
                }
            },
            "type": "n8n-nodes-base.postgresTool",
            "typeVersion": 2.5,
            "position": [
                928,
                448
            ],
            "id": "396442c8-7793-4961-b854-309d4965ea78",
            "name": "Get File Contents",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "descriptionType": "manual",
                "toolDescription": "Run a SQL query - use this to query from the document_rows table once you know the file ID you are querying. dataset_id is the file_id and you are always using the row_data for filtering, which is a jsonb field that has all the keys from the file schema given in the document_metadata table.\n\nExample query:\n\nSELECT AVG((row_data->>'revenue')::numeric)\nFROM document_rows\nWHERE dataset_id = '123';\n\nExample query 2:\n\nSELECT \n    row_data->>'category' as category,\n    SUM((row_data->>'sales')::numeric) as total_sales\nFROM dataset_rows\nWHERE dataset_id = '123'\nGROUP BY row_data->>'category';",
                "operation": "executeQuery",
                "query": "{{ $fromAI('sql_query') }}",
                "options": {}
            },
            "type": "n8n-nodes-base.postgresTool",
            "typeVersion": 2.5,
            "position": [
                1088,
                448
            ],
            "id": "64a4c4ca-6b5a-4e11-a813-26a422d6598f",
            "name": "Query Document Rows",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "mode": "retrieve-as-tool",
                "toolName": "documents",
                "toolDescription": "Use RAG to look up information in the knowledgebase.",
                "tableName": {
                    "__rl": true,
                    "value": "documents",
                    "mode": "list",
                    "cachedResultName": "documents"
                },
                "options": {
                    "queryName": "match_documents"
                }
            },
            "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
            "typeVersion": 1,
            "position": [
                1408,
                224
            ],
            "id": "6accb683-411e-4773-a4fb-23d5ebfc7bd0",
            "name": "Supabase Vector Store1",
            "credentials": {
                "supabaseApi": {
                    "id": "rIRiq7wME466hZCt",
                    "name": "Vector A Dung"
                }
            }
        },
        {
            "parameters": {
                "options": {}
            },
            "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
            "typeVersion": 1.2,
            "position": [
                1520,
                400
            ],
            "id": "8689c59e-e8f2-49ac-956d-5b7db8334b7b",
            "name": "Embeddings OpenAI2",
            "credentials": {
                "openAiApi": {
                    "id": "yS1gimiL6UYP0t5O",
                    "name": "GPT"
                }
            }
        },
        {
            "parameters": {
                "options": {
                    "reset": false
                }
            },
            "type": "n8n-nodes-base.splitInBatches",
            "typeVersion": 3,
            "position": [
                -256,
                752
            ],
            "id": "0b7d8608-5f19-4498-96df-ffa9f91e456d",
            "name": "Loop Over Items"
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "-- Enable the pgvector extension to work with embedding vectors\ncreate extension vector;\n\n-- Create a table to store your documents\ncreate table documents (\n  id bigserial primary key,\n  content text, -- corresponds to Document.pageContent\n  metadata jsonb, -- corresponds to Document.metadata\n  embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed\n);\n\n-- Create a function to search for documents\ncreate function match_documents (\n  query_embedding vector(1536),\n  match_count int default null,\n  filter jsonb DEFAULT '{}'\n) returns table (\n  id bigint,\n  content text,\n  metadata jsonb,\n  similarity float\n)\nlanguage plpgsql\nas $$\n#variable_conflict use_column\nbegin\n  return query\n  select\n    id,\n    content,\n    metadata,\n    1 - (documents.embedding <=> query_embedding) as similarity\n  from documents\n  where metadata @> filter\n  order by documents.embedding <=> query_embedding\n  limit match_count;\nend;\n$$;",
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.5,
            "position": [
                -432,
                400
            ],
            "id": "a0c54527-e205-45df-899a-d7382c52bb46",
            "name": "Create Documents Table and Match Function",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "operation": "delete",
                "tableId": "document_rows",
                "filters": {
                    "conditions": [
                        {
                            "keyName": "dataset_id",
                            "condition": "eq",
                            "keyValue": "={{ $('Set File ID').item.json.file_id }}"
                        }
                    ]
                }
            },
            "type": "n8n-nodes-base.supabase",
            "typeVersion": 1,
            "position": [
                272,
                912
            ],
            "id": "aa798f93-651c-48ad-8431-1bb1102326c2",
            "name": "Delete Old Data Rows",
            "alwaysOutputData": true,
            "executeOnce": true,
            "credentials": {
                "supabaseApi": {
                    "id": "rIRiq7wME466hZCt",
                    "name": "Vector A Dung"
                }
            }
        },
        {
            "parameters": {
                "operation": "upsert",
                "schema": {
                    "__rl": true,
                    "mode": "list",
                    "value": "public"
                },
                "table": {
                    "__rl": true,
                    "value": "document_metadata",
                    "mode": "list",
                    "cachedResultName": "document_metadata"
                },
                "columns": {
                    "mappingMode": "defineBelow",
                    "value": {
                        "id": "={{ $('Set File ID').item.json.file_id }}",
                        "title": "={{ $('Set File ID').item.json.file_title }}",
                        "url": "={{ $('Set File ID').item.json.file_url }}"
                    },
                    "matchingColumns": [
                        "id"
                    ],
                    "schema": [
                        {
                            "id": "id",
                            "displayName": "id",
                            "required": true,
                            "defaultMatch": true,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": true,
                            "removed": false
                        },
                        {
                            "id": "title",
                            "displayName": "title",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": false
                        },
                        {
                            "id": "url",
                            "displayName": "url",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": false,
                            "removed": false
                        },
                        {
                            "id": "created_at",
                            "displayName": "created_at",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "dateTime",
                            "canBeUsedToMatch": false
                        },
                        {
                            "id": "schema",
                            "displayName": "schema",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": false,
                            "removed": true
                        }
                    ],
                    "attemptToConvertTypes": false,
                    "convertFieldsToString": false
                },
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.5,
            "position": [
                432,
                768
            ],
            "id": "760da7eb-1e16-428b-9946-55bcebab15f7",
            "name": "Insert Document Metadata",
            "executeOnce": true,
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "schema": {
                    "__rl": true,
                    "mode": "list",
                    "value": "public"
                },
                "table": {
                    "__rl": true,
                    "value": "document_rows",
                    "mode": "list",
                    "cachedResultName": "document_rows"
                },
                "columns": {
                    "mappingMode": "defineBelow",
                    "value": {
                        "dataset_id": "={{ $('Set File ID').item.json.file_id }}",
                        "row_data": "={{ $json.toJsonString().replaceAll(/'/g, \"''\") }}"
                    },
                    "matchingColumns": [
                        "id"
                    ],
                    "schema": [
                        {
                            "id": "id",
                            "displayName": "id",
                            "required": false,
                            "defaultMatch": true,
                            "display": true,
                            "type": "number",
                            "canBeUsedToMatch": true,
                            "removed": true
                        },
                        {
                            "id": "dataset_id",
                            "displayName": "dataset_id",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": true
                        },
                        {
                            "id": "row_data",
                            "displayName": "row_data",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "object",
                            "canBeUsedToMatch": true
                        }
                    ],
                    "attemptToConvertTypes": false,
                    "convertFieldsToString": false
                },
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.5,
            "position": [
                1488,
                1024
            ],
            "id": "ba214097-caec-4dfa-8fba-334fb63da844",
            "name": "Insert Table Rows",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "operation": "upsert",
                "schema": {
                    "__rl": true,
                    "mode": "list",
                    "value": "public"
                },
                "table": {
                    "__rl": true,
                    "value": "document_metadata",
                    "mode": "list",
                    "cachedResultName": "document_metadata"
                },
                "columns": {
                    "mappingMode": "defineBelow",
                    "value": {
                        "id": "={{ $('Set File ID').item.json.file_id }}",
                        "schema": "={{ $json.schema }}"
                    },
                    "matchingColumns": [
                        "id"
                    ],
                    "schema": [
                        {
                            "id": "id",
                            "displayName": "id",
                            "required": true,
                            "defaultMatch": true,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": true,
                            "removed": false
                        },
                        {
                            "id": "title",
                            "displayName": "title",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": false,
                            "removed": true
                        },
                        {
                            "id": "url",
                            "displayName": "url",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": false,
                            "removed": true
                        },
                        {
                            "id": "created_at",
                            "displayName": "created_at",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "dateTime",
                            "canBeUsedToMatch": false
                        },
                        {
                            "id": "schema",
                            "displayName": "schema",
                            "required": false,
                            "defaultMatch": false,
                            "display": true,
                            "type": "string",
                            "canBeUsedToMatch": false,
                            "removed": false
                        }
                    ],
                    "attemptToConvertTypes": false,
                    "convertFieldsToString": false
                },
                "options": {}
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 2.5,
            "position": [
                2352,
                784
            ],
            "id": "b458af1b-e299-47b0-85db-d7b5d687e925",
            "name": "Update Schema for Document Metadata",
            "credentials": {
                "postgres": {
                    "id": "6YjGMRi9Qafn789v",
                    "name": "Rag A Dung"
                }
            }
        },
        {
            "parameters": {
                "method": "POST",
                "url": "https://bot-api.zapps.me/bot2095168039013681253:FyRPbygLAbIIUlMoFbUryHGPDnppMWCgEQFHPrXxlSYYdOEHSQzWFqNlAgQjPnld/sendMessage",
                "sendBody": true,
                "bodyParameters": {
                    "parameters": [
                        {
                            "name": "chat_id",
                            "value": "={{ $('Edit Fields').item.json.chat_id }}"
                        },
                        {
                            "name": "text",
                            "value": "={{ $json.output }}"
                        }
                    ]
                },
                "options": {}
            },
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 4.2,
            "position": [
                1056,
                224
            ],
            "id": "e36ec16c-56ac-4d40-8a02-7bdd4c5baac0",
            "name": "HTTP Request1"
        }
    ],
    "pinData": {},
    "connections": {
        "Webhook": {
            "main": [
                [
                    {
                        "node": "Edit Fields",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "OpenAI Chat Model": {
            "ai_languageModel": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "ai_languageModel",
                        "index": 0
                    }
                ]
            ]
        },
        "Default Data Loader": {
            "ai_document": [
                [
                    {
                        "node": "Insert into Supabase Vectorstore",
                        "type": "ai_document",
                        "index": 0
                    }
                ]
            ]
        },
        "Embeddings OpenAI1": {
            "ai_embedding": [
                [
                    {
                        "node": "Insert into Supabase Vectorstore",
                        "type": "ai_embedding",
                        "index": 0
                    }
                ]
            ]
        },
        "Download File": {
            "main": [
                [
                    {
                        "node": "Switch",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "File Created": {
            "main": [
                [
                    {
                        "node": "Loop Over Items",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "File Updated": {
            "main": [
                [
                    {
                        "node": "Loop Over Items",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Extract Document Text": {
            "main": [
                [
                    {
                        "node": "Insert into Supabase Vectorstore",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Postgres Chat Memory": {
            "ai_memory": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "ai_memory",
                        "index": 0
                    }
                ]
            ]
        },
        "Delete Old Doc Rows": {
            "main": [
                [
                    {
                        "node": "Delete Old Data Rows",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Set File ID": {
            "main": [
                [
                    {
                        "node": "Delete Old Doc Rows",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Edit Fields": {
            "main": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Extract PDF Text": {
            "main": [
                [
                    {
                        "node": "Insert into Supabase Vectorstore",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Aggregate": {
            "main": [
                [
                    {
                        "node": "Summarize",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Character Text Splitter": {
            "ai_textSplitter": [
                [
                    {
                        "node": "Default Data Loader",
                        "type": "ai_textSplitter",
                        "index": 0
                    }
                ]
            ]
        },
        "Summarize": {
            "main": [
                [
                    {
                        "node": "Set Schema",
                        "type": "main",
                        "index": 0
                    },
                    {
                        "node": "Insert into Supabase Vectorstore",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "RAG AI Agent": {
            "main": [
                [
                    {
                        "node": "HTTP Request1",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Switch": {
            "main": [
                [
                    {
                        "node": "Extract PDF Text",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "Extract from Excel",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "Extract from CSV",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "Extract Document Text",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Insert into Supabase Vectorstore": {
            "main": [
                [
                    {
                        "node": "Loop Over Items",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Extract from Excel": {
            "main": [
                [
                    {
                        "node": "Aggregate",
                        "type": "main",
                        "index": 0
                    },
                    {
                        "node": "Insert Table Rows",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Set Schema": {
            "main": [
                [
                    {
                        "node": "Update Schema for Document Metadata",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Extract from CSV": {
            "main": [
                [
                    {
                        "node": "Aggregate",
                        "type": "main",
                        "index": 0
                    },
                    {
                        "node": "Insert Table Rows",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "List Documents": {
            "ai_tool": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "ai_tool",
                        "index": 0
                    }
                ]
            ]
        },
        "Get File Contents": {
            "ai_tool": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "ai_tool",
                        "index": 0
                    }
                ]
            ]
        },
        "Query Document Rows": {
            "ai_tool": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "ai_tool",
                        "index": 0
                    }
                ]
            ]
        },
        "Supabase Vector Store1": {
            "ai_tool": [
                [
                    {
                        "node": "RAG AI Agent",
                        "type": "ai_tool",
                        "index": 0
                    }
                ]
            ]
        },
        "Embeddings OpenAI2": {
            "ai_embedding": [
                [
                    {
                        "node": "Supabase Vector Store1",
                        "type": "ai_embedding",
                        "index": 0
                    }
                ]
            ]
        },
        "Loop Over Items": {
            "main": [
                [],
                [
                    {
                        "node": "Set File ID",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Delete Old Data Rows": {
            "main": [
                [
                    {
                        "node": "Insert Document Metadata",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Insert Document Metadata": {
            "main": [
                [
                    {
                        "node": "Download File",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "HTTP Request1": {
            "main": [
                []
            ]
        }
    },
    "active": false,
    "settings": {
        "executionOrder": "v1"
    },
    "versionId": "720cf4fa-7355-4760-893e-3cfa2af1e7e0",
    "meta": {
        "instanceId": "14cba0b19bf9c0c3b3b9e48bb7c8540e19b8204fca1f1d4d282b9df8e4e4aa2a"
    },
    "id": "sH9y1V4AD0pt5DYn",
    "tags": []
}