Tutorial: Building a Knowledge-DB with Google Drive, Pinecone, and Google Gemini
n8n Tutorial: Building a Knowledge-DB with Google Drive, Pinecone, and Google Gemini
This tutorial will guide you through setting up an n8n workflow named "Knowledge-DB." This workflow has two primary functions:
- Ingesting Data: Automatically takes new files (CSV, PDF, TXT/MD) from a specified Google Drive folder, extracts their content, creates embeddings using Google Gemini, and stores them in a Pinecone Vector Store.
- Chatting with Data: Allows you to query the ingested knowledge base through a chat interface, utilizing an AI Agent powered by Google Gemini and retrieving relevant information from Pinecone.
1. Workflow Objective
The main objective of this workflow is to create a dynamic knowledge base that can be updated by simply dropping files into a Google Drive folder and then queried via a chat interface.
2. Prerequisites
Before you begin, ensure you have the following:
- An active n8n instance: Self-hosted or n8n Cloud.
- Google Drive Account: With a dedicated folder for your knowledge base files.
- Google Gemini (PaLM) API Key: For generating text embeddings and powering the chat model.
- Pinecone Account and API Key: You'll need an active Pinecone index (named
knowledge-dbas configured in the workflow, or adjust the node's settings).
3. Workflow Overview
The "Knowledge-DB" workflow consists of two main branches:
-
Branch 1: Data Ingestion (Left Side of Workflow)
- Trigger:
Google Drive Trigger(watches a specific folder for new files) - Data Retrieval:
Get Document From Google Drive(downloads the new file) - File Type Routing:
Route file type(determines file extension) - Content Extraction:
Extract from CSV,Extract from PDF,Extract from TXT, MD(parses file content) - Data Preparation:
Edit Fields1,Edit Fields2,Edit Fields3(formats data for Pinecone) - Embedding Generation:
Embeddings Google Gemini(converts text to vector embeddings) - Vector Storage:
Pinecone Vector Store(stores embeddings and metadata)
- Trigger:
-
Branch 2: Chat with Data (Right Side of Workflow)
- Trigger:
When chat message received(listens for incoming chat messages) - AI Agent:
AI Agent1(orchestrates the interaction) - Language Model:
Google Gemini Chat Model1(processes natural language) - Memory:
Simple Memory1(maintains chat context) - Knowledge Retrieval:
Pinecone Vector Store3(retrieves relevant data from the knowledge base) - Embedding for Retrieval:
Embeddings Google Gemini3(generates embeddings for search queries)
- Trigger:
4. Step-by-Step Tutorial
Part 1: Setting up Data Ingestion
This part of the workflow automatically processes new files added to a Google Drive folder and stores their vectorized content in Pinecone.
Step 1: Google Drive Trigger This node initiates the workflow when a new file is created in a specified Google Drive folder.
- Drag and drop a
Google Drive Triggernode onto your canvas. - Credentials: Configure a new Google Drive OAuth2 API credential if you haven't already. You'll need to authorize n8n to access your Google Drive.
- Trigger On: Select
Specific Folder. - Folder To Watch: Click the "List" icon and select the Google Drive folder you want to use as your knowledge base input. For this workflow, the cached result name is
knowledge_DB. - Event: Set to
File Created. - Options > File Type: Set to
All. - Activate the workflow to enable the trigger.
Step 2: Get Document From Google Drive This node downloads the file detected by the Google Drive Trigger.
- Connect the
Google Drive Triggernode'smainoutput to a newGoogle Drivenode. - Operation: Select
Download. - File ID: Use an expression to get the file ID from the previous node:
={{ $json.id }}. - Options > Binary Property Name: Set to
data. This ensures the file content is available as binary data for subsequent nodes. - Credentials: Use the same Google Drive OAuth2 API credential as the trigger.
Step 3: Route file type (Switch Node) This node directs the workflow based on the file extension of the downloaded document.
- Connect the
Get Document From Google Drivenode'smainoutput to aSwitchnode. - Configure four output branches (rules) to handle different file types:
- Branch 1 (CSV):
- Condition:
={{ $binary.data.fileExtension }}equalscsv
- Condition:
- Branch 2 (PDF):
- Condition:
={{ $binary.data.fileExtension }}equalspdf
- Condition:
- Branch 3 (TXT, MD, DOCS, DOCX):
- Condition:
={{ $binary.data.fileExtension }}equalstxt, md, docs, docx
- Condition:
- Branch 4 (Other - default):
- Condition:
={{ $binary.data.fileExtension }}notEqualscsv, pdf, txt, md, docs, docx(This branch will catch any other file types, though the example workflow links it to Pinecone directly, which might need adjustment based on desired behavior for unsupported types.)
- Condition:
- Branch 1 (CSV):
Step 4: Extract from File Nodes These nodes extract the text content from different file types.
- Extract from CSV:
- Connect Branch 1 (CSV) of the
Route file typenode to anExtract from Filenode. - Operation: Ensure
CSVis selected (this is usually the default for this node when it's handling CSV). - Options: Leave as default for raw data (false).
- Connect Branch 1 (CSV) of the
- Extract from PDF:
- Connect Branch 2 (PDF) of the
Route file typenode to anExtract from Filenode. - Operation: Select
PDF.
- Connect Branch 2 (PDF) of the
- Extract from TXT, MD:
- Connect Branch 3 (TXT, MD, DOCS, DOCX) of the
Route file typenode to anExtract from Filenode. - Operation: Select
Text. - Destination Key: Set to
text.
- Connect Branch 3 (TXT, MD, DOCS, DOCX) of the
Step 5: Edit Fields (Set Nodes) These nodes standardize the data structure before sending it to Pinecone.
- Edit Fields1 (for CSV):
- Connect the
Extract from CSVnode'smainoutput to aSetnode. - Assignments: Add the following fields:
id(Type:String, Value:={{ $json.id }})prompt(Type:String, Value:={{ $json.prompt }})response(Type:String, Value:={{ $json.response }})sent_at(Type:String, Value:={{ $json.sent_at }})
- Connect the
- Edit Fields2 (for PDF):
- Connect the
Extract from PDFnode'smainoutput to aSetnode. - Assignments: Add the following fields:
info.Title(Type:String, Value:={{ $json.info.Title }})text(Type:String, Value:={{ $json.text }})
- Connect the
- Edit Fields3 (for TXT, MD, DOCS, DOCX):
- Connect the
Extract from TXT, MDnode'smainoutput to aSetnode. - Assignments: Add the following fields:
date(Type:String, Value:={{ $now }})text(Type:String, Value:={{ $json.text }})
- Connect the
Step 6: Embeddings Google Gemini (for Ingestion) This node generates vector embeddings from the text content.
- Connect the
Edit Fields3node (from TXT/MD path) to anEmbeddings Google Gemininode. (Note: In the provided workflow, only the TXT/MD path directly feeds into this. You might want to connect the outputs ofEdit Fields1andEdit Fields2to this node as well, or create separate embedding nodes if your data structure varies significantly). - Credentials: Configure a new Google Gemini (PaLM) API credential. You'll need to provide your API key.
Step 7: Pinecone Vector Store (for Ingestion) This node stores the generated embeddings in your Pinecone index.
- Connect the
Edit Fields1,Edit Fields2,Edit Fields3nodes'mainoutputs to thisPinecone Vector Storenode'smaininput. - Connect the
Embeddings Google Gemininode'sai_embeddingoutput to thisPinecone Vector Storenode'sai_embeddinginput. - Mode: Select
Insert. - Pinecone Index: Select your Pinecone index (e.g.,
knowledge-db). If you don't have one, create it in your Pinecone dashboard with an appropriate dimension (e.g., 768 for Gemini embeddings). - Embedding Batch Size: Set to
={{ 200 }}. This specifies how many embeddings are sent to Pinecone in a single batch. - Credentials: Configure a new Pinecone API credential with your API key and environment.
Part 2: Setting up Chat with Data
This part of the workflow enables users to interact with the knowledge base via a chat interface.
Step 1: When chat message received (Trigger) This node acts as the entry point for chat messages.
- Drag and drop a
When chat message receivednode onto your canvas. - Options: Leave as default.
Step 2: AI Agent1 This node is the core of your AI interaction, orchestrating the language model, memory, and tools.
- Connect the
When chat message receivednode'smainoutput to anAI Agentnode. - Options: Leave as default.
Step 3: Google Gemini Chat Model1 This node provides the large language model capabilities for the AI Agent.
- Connect the
Google Gemini Chat Modelnode'sai_languageModeloutput to theAI Agent1node'sai_languageModelinput. - Credentials: Use the same Google Gemini (PaLM) API credential configured earlier.
Step 4: Simple Memory1 This node helps the AI Agent remember past conversations, providing context to current queries.
- Connect the
Simple Memorynode'sai_memoryoutput to theAI Agent1node'sai_memoryinput. - Context Window Length: Set to
10. This determines how many previous messages the AI Agent will consider for context.
Step 5: Pinecone Vector Store3 (for Retrieval) This node acts as a tool for the AI Agent to search and retrieve information from your knowledge base.
- Connect the
Pinecone Vector Storenode'sai_tooloutput to theAI Agent1node'sai_toolinput. - Mode: Select
Retrieve as Tool. - Tool Description: Provide a clear description for the AI Agent, e.g., "User's knowledge database". This helps the AI Agent understand when to use this tool.
- Pinecone Index: Select your
knowledge-dbPinecone index. - Top K: Set to
10. This specifies the number of top relevant results to retrieve. - Credentials: Use the same Pinecone API credential configured earlier.
Step 6: Embeddings Google Gemini3 (for Retrieval) This node generates embeddings for the search queries made by the AI Agent when it uses the Pinecone retrieval tool.
- Connect the
Embeddings Google Gemininode'sai_embeddingoutput to thePinecone Vector Store3node'sai_embeddinginput. - Credentials: Use the same Google Gemini (PaLM) API credential configured earlier.
5. Activating and Using the Workflow
- Activate the workflow: Ensure both the "Google Drive Trigger" and "When chat message received" are active.
- Ingest Data: Drop some CSV, PDF, or TXT/MD files into your designated Google Drive folder. The workflow will automatically process them and add their content to your Pinecone knowledge base.
- Chat with Data: To interact with your knowledge base, you'll need to send messages via the
When chat message receivednode. The AI Agent will then process your query, retrieve relevant information from Pinecone, and formulate a response.
6. Troubleshooting Tips
- Credentials: Double-check all your API keys and OAuth2 credentials. Incorrect credentials are a common source of errors.
- Pinecone Index: Verify that your Pinecone index is active and has the correct dimensions for the Google Gemini embeddings.
- Node Inputs/Outputs: Carefully review the connections between your nodes to ensure data flows as expected.