How to Build a RAG Chatbot with n8n AI Agents

145.1K views
•
September 26, 2024
by
Leon van Zyl
YouTube video player
How to Build a RAG Chatbot with n8n AI Agents

TL;DR

Building a RAG chatbot in n8n takes two workflows: one that loads documents from a Google Drive folder into a Pinecone vector store, and one that runs an AI Agent answering questions from that knowledge base. Adding a new file to the Drive folder automatically expands what the chatbot knows, and the finished chat can be shared by URL or embedded on a website.

Transcript

Imagine having an AI assistant that can answer questions using the documents in your Google Drive folder. Today, I'll show you how to build exactly that using N8N's AI Agent node. With the rapid development of AI, learning to create your own AI agents is becoming an essential skill for developers and businesses alike. So in this video, we'll focus ... Read More

Key Insights

  • Building a RAG chatbot in n8n involves two phases: loading data from a source like a PDF or database into a vector store, then building the AI assistant that queries that store to answer questions.
  • A vector store acts as the knowledge base the AI model uses to answer questions, and in this tutorial Pinecone is used as that store because it offers a generous free tier.
  • The Google Drive trigger node watches a specific folder for changes, and selecting 'File Created' under Watch For means the update-knowledge-base workflow runs whenever a new file is uploaded to that folder.
  • Connecting Google Drive to n8n requires OAuth credentials created in Google Cloud Platform: create a project, enable the Google Drive API, configure the OAuth consent screen, publish the app, then generate a Web Application client ID and secret.
  • Adding new information to the chatbot's knowledge is as simple as dropping another file, such as a menu list with prices, into the watched Google Drive folder, and the agent can then answer questions from it.
  • The Pinecone index is created with the text-embedding-3-small model, and the same embedding model must be selected in n8n's OpenAI embeddings node so the dimensions match between indexing and querying.
  • The default data loader uses a recursive character text splitter, set here to a chunk size of 500 with an overlap of 20, though larger documents are better with chunk sizes of 1000 to 2000 and an overlap of about 200.
  • Adding a metadata property such as the file name to the document loader lets you see which source file each chunk of information was extracted from, which matters once the knowledge base holds many files.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How do you build a RAG chatbot with n8n AI agents?

You build it in two phases. First, create an 'Update Knowledge Base' workflow that grabs data from a source like a Google Drive folder and upserts it into a vector store such as Pinecone. Second, build the AI assistant itself using the AI Agent node, which answers user questions by drawing on that vector store knowledge base. The video walks through connecting Google Drive, downloading files, embedding their contents, and storing them in Pinecone.

Q: What is a vector store and why is it needed for a RAG chatbot?

A vector store is the knowledge base that the AI model uses to answer questions. In this tutorial Pinecone serves as the vector store. Documents from the data source are split into chunks, converted into embeddings, and inserted into the store. When a user asks a question, the agent retrieves relevant information from this store to produce an accurate, grounded answer rather than relying only on the model's general knowledge.

Q: How do you set up the Google Drive trigger in n8n?

Add a Google Drive node and select the trigger 'On changes involving a specific folder.' Create a new credential using a client ID and secret generated in Google Cloud Platform. Set the poll time, for example every minute, choose the folder you created, and under Watch For select 'File Created.' This way the workflow executes whenever a new file is uploaded to that folder, passing along all the information about the added file.

Q: How do you create Google Drive OAuth credentials for n8n?

Go to Google Cloud Platform and create a new project, then enable the Google Drive API from the Library. Configure the OAuth consent screen by selecting External, naming the app, and adding your email and developer contact, then publish the app. Create OAuth Client ID credentials of type Web Application, add the redirect URL copied from n8n under Authorized Redirect URIs, and paste the resulting client ID and secret back into n8n.

Q: Which vector database and embedding model does the tutorial use?

The tutorial uses Pinecone as the vector database because it offers a generous free tier that costs nothing to start. When creating the index, dimensions are set up by model using the text-embedding-3-small model. That same OpenAI text-embedding-3-small model must be selected in the n8n embeddings node, since the model used to query must match the model used when the index was created.

Q: How do you add new information to the chatbot's knowledge base?

You simply add another file to the watched Google Drive folder. In the example, uploading a list of menu items with their prices lets the chatbot then answer questions about the menu, such as how much the lamb chops cost, and it returns the correct response. Because the workflow triggers on file creation, no extra setup is needed. You could upload lecture notes or any other documents the same way.

Q: What chunk size and overlap should you use for the text splitter?

The default data loader uses a recursive character text splitter. In this tutorial the chunk size is set to 500 with an overlap of 20. For larger documents, the tutorial recommends changing the chunk size to something like 1000 to 2000 characters with a chunk overlap of perhaps 200. Adjusting these values helps the vector store handle bigger files while keeping context across chunk boundaries.

Q: How can you see which file a piece of information came from in Pinecone?

Add a metadata property in the document loader. Go to Options, then Metadata, then Add Property, and name the property, for example 'file.' For the value, switch to Mapping, select Schema, and choose the file name field from the Google Drive node. This attaches the source file name to each stored document, so once the knowledge base contains many files you can tell which file each chunk of information was extracted from.

Summary & Key Takeaways

  • The goal is an AI chatbot that answers questions using documents in a Google Drive folder, demonstrated with an 'Oak and Barrel' restaurant example where the agent answers questions about specials and menu prices. Adding a new file to the folder instantly expands the chatbot's knowledge without further configuration.

  • Phase one builds an 'Update Knowledge Base' workflow: a Google Drive trigger watches a folder for created files, a Download File node pulls the file's contents, and a Pinecone Vector Store node in Insert Documents mode upserts the data using an OpenAI embedding function and a default data loader.

  • Credentials are set up along the way: Google Drive OAuth via Google Cloud Platform, a Pinecone API key with an index built on text-embedding-3-small, and an OpenAI API key for both embeddings and chat. A namespace and file-name metadata are added so each project and source file stays organized.


Read in Other Languages (beta)

Share This Summary 📚

Summarize YouTube Videos and Get Video Transcripts with 1-Click

Download browser extensions on:

Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator

Explore More Summaries from Leon van Zyl 📚

Summarize YouTube Videos and Get Video Transcripts with 1-Click

Download browser extensions on:

Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator