{
  "name": "Knowledge base AI assistant",
  "nodes": [
    {
      "parameters": {},
      "id": "f6cf8296-4281-4170-9ab2-80f252fde32a",
      "name": "When clicking \"Execute Workflow\"",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        1640,
        140
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "31a3b843-e794-4854-a31e-8871db8c1ffb",
      "name": "Embeddings OpenAI",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        2440,
        360
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## 1. Setup: Fetch file from Google Drive, split it into chunks and insert into a vector database\nNote that running this part multiple times will insert multiple copies into your DB",
        "height": 748,
        "width": 1386,
        "color": 7
      },
      "id": "fd0baad4-e589-4bff-a596-11ad237f9db8",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1820,
        -40
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "dataType": "binary",
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "file_url",
                "value": "={{ $json.file_url }}"
              },
              {
                "name": "file_name",
                "value": "={{ $('Add in metadata').item.json.file_name }}"
              }
            ]
          }
        }
      },
      "id": "03ab3831-45ba-4484-818d-b7241898fd2a",
      "name": "Default Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        2620,
        360
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Add a new field called 'myNewField' to the JSON of the item\n$input.item.json.file_name = $input.item.binary.data.fileName;\n$input.item.json.file_ext = $input.item.binary.data.fileExtension;\n$input.item.json.file_url = $('Set file URL in Google Drive').item.json.file_url\n\nreturn $input.item;"
      },
      "id": "2884e5ad-ffbb-4d2e-bb3a-bd06eb55d206",
      "name": "Add in metadata",
      "type": "n8n-nodes-base.code",
      "position": [
        2280,
        140
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "mode": "url",
          "value": "={{ $json.file_url }}"
        },
        "options": {}
      },
      "id": "436c583b-b529-4861-837f-6b259df6a69b",
      "name": "Download file",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        2000,
        140
      ],
      "typeVersion": 3
    },
    {
      "parameters": {},
      "id": "1762cbfe-5972-4b72-8323-7c136aea6735",
      "name": "Chat Trigger",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        1640,
        860
      ],
      "webhookId": "1727c687-aed0-49cf-96af-e7796819fbb3",
      "typeVersion": 1
    },
    {
      "parameters": {
        "jsCode": "let out = \"\"\nfor (const i in $input.all()) {\n  let itemText = \"--- CHUNK \" + i + \" ---\\n\"\n  itemText += $input.all()[i].json.document.pageContent + \"\\n\"\n  itemText += \"\\n\"\n  out += itemText\n}\n\nreturn {\n  'context': out\n};"
      },
      "id": "bdd73b10-958b-4cc1-a1c4-f3a30278e062",
      "name": "Prepare chunks",
      "type": "n8n-nodes-base.code",
      "position": [
        2460,
        860
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "0b5aca60-701f-47dd-b6da-6523a71c63a0",
      "name": "Embeddings OpenAI2",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        2080,
        1080
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "b2031f3c-9bbf-424d-821a-f5807b70abe7",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        2700,
        1080
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "236047ff-75a2-47fd-b338-1e9763c4015e",
              "name": "chunks",
              "type": "number",
              "value": 4
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "61d80c98-0e14-413e-a8fa-3ee00121afee",
      "name": "Set max chunks to send to model",
      "type": "n8n-nodes-base.set",
      "position": [
        1860,
        860
      ],
      "typeVersion": 3.3
    },
    {
      "parameters": {
        "jsonSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"answer\": {\n      \"type\": \"string\"\n    },\n    \"citations\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"number\"\n      }\n    }\n  }\n}"
      },
      "id": "59090845-c175-476b-b539-b89f91e2cf52",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        2880,
        1080
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "67ecefcf-a30c-4cc4-89ca-b9b23edd6585",
              "name": "citations",
              "type": "array",
              "value": "={{ $json.citations.map(i => '[' + $('Get top chunks matching query').all()[$json.citations].json.document.metadata.file_name + ', lines ' + $('Get top chunks matching query').all()[$json.citations].json.document.metadata['loc.lines.from'] + '-' + $('Get top chunks matching query').all()[$json.citations].json.document.metadata['loc.lines.to'] + ']') }}"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "b54a2039-2dd8-49ce-a66e-68be70494138",
      "name": "Compose citations",
      "type": "n8n-nodes-base.set",
      "position": [
        3060,
        860
      ],
      "typeVersion": 3.3
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d77956c4-0ff4-4c64-80c2-9da9d4c8ad34",
              "name": "text",
              "type": "string",
              "value": "={{ $json.answer }} {{ $if(!$json.citations.isEmpty(), \"\\n\" + $json.citations.join(\"\"), '') }}"
            }
          ]
        },
        "options": {}
      },
      "id": "dd700443-c0a9-482a-8c81-86d74c70e73d",
      "name": "Generate response",
      "type": "n8n-nodes-base.set",
      "position": [
        3280,
        860
      ],
      "typeVersion": 3.3
    },
    {
      "parameters": {
        "content": "## 2. Chat with file, getting citations in reponse",
        "height": 548.5086735412393,
        "width": 1693.989843925635,
        "color": 7
      },
      "id": "f6ac2dc9-a279-47ae-ab70-b8de5056fd46",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1820,
        740
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Important: In your response, also include the the indexes of the chunks you used to generate the answer.\n\n{{ $json.context }}\n\nQuestion: {{ $(\"Chat Trigger\").first().json.chatInput }}\nHelpful Answer:",
        "hasOutputParser": true
      },
      "id": "23bf2d57-a46c-47c5-b081-82eaf7c90976",
      "name": "Answer the query based on chunks",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        2680,
        860
      ],
      "typeVersion": 1.4
    },
    {
      "parameters": {
        "mode": "load",
        "pineconeIndex": {
          "__rl": true,
          "mode": "list",
          "value": "test-index",
          "cachedResultName": "test-index"
        },
        "prompt": "={{ $json.chatInput }}",
        "topK": "={{ $json.chunks }}",
        "options": {}
      },
      "id": "d020cfa3-5a85-4f75-b834-2e1cc3cb213c",
      "name": "Get top chunks matching query",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
      "position": [
        2080,
        860
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "mode": "insert",
        "pineconeIndex": {
          "__rl": true,
          "mode": "list",
          "value": "test-index",
          "cachedResultName": "test-index"
        },
        "options": {}
      },
      "id": "dea57757-813e-43d8-a38b-0f75ff104925",
      "name": "Add to Pinecone vector store",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
      "position": [
        2500,
        140
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "chunkSize": 3000,
        "chunkOverlap": 200,
        "options": {}
      },
      "id": "997cbda2-9b5a-40c2-8548-d2a11c960a5b",
      "name": "Recursive Character Text Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        2620,
        520
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "# Get the data through chatbot with citations in the response\n\n## How to pull data with Chat Trigger publicity\n\n1. Double click on Chat Trigger \n2. Select \"Make chat publicity available\"\n3. Start chatting and asking  ",
        "height": 591,
        "width": 700
      },
      "id": "b1174963-bd67-4ce4-a6a2-d9465909da5c",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        720
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "# Ingest any type of documents with Google Drive\n\n## How to setup this flow\n\n\n### Google drive\n1. Follow [this guide](https://docs.n8n.io/integrations/builtin/credentials/google/oauth-generic/#configure-your-oauth-consent-screen)\n2. Complete the setup credentials here\n3. Ready to start \n\n### Pinecone account\n1. Create or Login into Pinecone account\n2. In Pinecone, create an index with 1536 dimensions and select it in the two vector store nodes\n3. Ready to start \n\n\n### Google Drive \n1. Create or Login into your Google Drive account \n2. Upload in a folder all files you want to train\n3. Ready to start \n\n## How to start training\nPopulate Pinecone by clicking the 'test workflow' button below",
        "height": 651,
        "width": 700
      },
      "id": "8d673593-9d4b-4d68-bf53-1c305c6e4b7e",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        -40
      ],
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "When clicking \"Execute Workflow\"": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Add to Pinecone vector store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Add to Pinecone vector store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Add in metadata": {
      "main": [
        [
          {
            "node": "Add to Pinecone vector store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "Add in metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chat Trigger": {
      "main": [
        [
          {
            "node": "Set max chunks to send to model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare chunks": {
      "main": [
        [
          {
            "node": "Answer the query based on chunks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI2": {
      "ai_embedding": [
        [
          {
            "node": "Get top chunks matching query",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Answer the query based on chunks",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Set max chunks to send to model": {
      "main": [
        [
          {
            "node": "Get top chunks matching query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Answer the query based on chunks",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Compose citations": {
      "main": [
        [
          {
            "node": "Generate response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answer the query based on chunks": {
      "main": [
        [
          {
            "node": "Compose citations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get top chunks matching query": {
      "main": [
        [
          {
            "node": "Prepare chunks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1609793d-dd95-44f6-a4e5-a2c2a02d8309",
  "meta": {
    "templateId": "2413",
    "instanceId": "ba1ee3cb283f555c8259933930d7eed40bbd9b6f053facc8f3835cd568408124"
  },
  "id": "y4LiXDVkwHlBufbB",
  "tags": [
    {
      "createdAt": "2024-12-02T15:08:18.373Z",
      "updatedAt": "2024-12-02T15:08:18.373Z",
      "id": "0FSW1zVaMXhFvXIu",
      "name": "small-product"
    }
  ]
}