Tutorial: Building a Knowledge-DB with Google Drive, Pinecone, and Google Gemini

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:

  1. 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.
  2. 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-db as 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)
  • 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)
      添加数据到向量存储库,支持从TXT、MD等文件导入,可与数据进行聊天交互。界面简洁,操作便捷。

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.

  1. Drag and drop a Google Drive Trigger node onto your canvas.
  2. 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.
  3. Trigger On: Select Specific Folder.
  4. 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.
  5. Event: Set to File Created.
  6. Options > File Type: Set to All.
  7. 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.

  1. Connect the Google Drive Trigger node's main output to a new Google Drive node.
  2. Operation: Select Download.
  3. File ID: Use an expression to get the file ID from the previous node: ={{ $json.id }}.
  4. Options > Binary Property Name: Set to data. This ensures the file content is available as binary data for subsequent nodes.
  5. 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.

  1. Connect the Get Document From Google Drive node's main output to a Switch node.
  2. Configure four output branches (rules) to handle different file types:
    • Branch 1 (CSV):
      • Condition: ={{ $binary.data.fileExtension }} equals csv
    • Branch 2 (PDF):
      • Condition: ={{ $binary.data.fileExtension }} equals pdf
    • Branch 3 (TXT, MD, DOCS, DOCX):
      • Condition: ={{ $binary.data.fileExtension }} equals txt, md, docs, docx
    • Branch 4 (Other - default):
      • Condition: ={{ $binary.data.fileExtension }} notEquals csv, 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.)

Step 4: Extract from File Nodes These nodes extract the text content from different file types.

  • Extract from CSV:
    1. Connect Branch 1 (CSV) of the Route file type node to an Extract from File node.
    2. Operation: Ensure CSV is selected (this is usually the default for this node when it's handling CSV).
    3. Options: Leave as default for raw data (false).
  • Extract from PDF:
    1. Connect Branch 2 (PDF) of the Route file type node to an Extract from File node.
    2. Operation: Select PDF.
  • Extract from TXT, MD:
    1. Connect Branch 3 (TXT, MD, DOCS, DOCX) of the Route file type node to an Extract from File node.
    2. Operation: Select Text.
    3. Destination Key: Set to text.

Step 5: Edit Fields (Set Nodes) These nodes standardize the data structure before sending it to Pinecone.

  • Edit Fields1 (for CSV):
    1. Connect the Extract from CSV node's main output to a Set node.
    2. 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 }})
  • Edit Fields2 (for PDF):
    1. Connect the Extract from PDF node's main output to a Set node.
    2. Assignments: Add the following fields:
      • info.Title (Type: String, Value: ={{ $json.info.Title }})
      • text (Type: String, Value: ={{ $json.text }})
  • Edit Fields3 (for TXT, MD, DOCS, DOCX):
    1. Connect the Extract from TXT, MD node's main output to a Set node.
    2. Assignments: Add the following fields:
      • date (Type: String, Value: ={{ $now }})
      • text (Type: String, Value: ={{ $json.text }})

Step 6: Embeddings Google Gemini (for Ingestion) This node generates vector embeddings from the text content.

  1. Connect the Edit Fields3 node (from TXT/MD path) to an Embeddings Google Gemini node. (Note: In the provided workflow, only the TXT/MD path directly feeds into this. You might want to connect the outputs of Edit Fields1 and Edit Fields2 to this node as well, or create separate embedding nodes if your data structure varies significantly).
  2. 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.

  1. Connect the Edit Fields1, Edit Fields2, Edit Fields3 nodes' main outputs to this Pinecone Vector Store node's main input.
  2. Connect the Embeddings Google Gemini node's ai_embedding output to this Pinecone Vector Store node's ai_embedding input.
  3. Mode: Select Insert.
  4. 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).
  5. Embedding Batch Size: Set to ={{ 200 }}. This specifies how many embeddings are sent to Pinecone in a single batch.
  6. 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.

  1. Drag and drop a When chat message received node onto your canvas.
  2. Options: Leave as default.

Step 2: AI Agent1 This node is the core of your AI interaction, orchestrating the language model, memory, and tools.

  1. Connect the When chat message received node's main output to an AI Agent node.
  2. Options: Leave as default.

Step 3: Google Gemini Chat Model1 This node provides the large language model capabilities for the AI Agent.

  1. Connect the Google Gemini Chat Model node's ai_languageModel output to the AI Agent1 node's ai_languageModel input.
  2. 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.

  1. Connect the Simple Memory node's ai_memory output to the AI Agent1 node's ai_memory input.
  2. 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.

  1. Connect the Pinecone Vector Store node's ai_tool output to the AI Agent1 node's ai_tool input.
  2. Mode: Select Retrieve as Tool.
  3. 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.
  4. Pinecone Index: Select your knowledge-db Pinecone index.
  5. Top K: Set to 10. This specifies the number of top relevant results to retrieve.
  6. 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.

  1. Connect the Embeddings Google Gemini node's ai_embedding output to the Pinecone Vector Store3 node's ai_embedding input.
  2. Credentials: Use the same Google Gemini (PaLM) API credential configured earlier.

5. Activating and Using the Workflow

  1. Activate the workflow: Ensure both the "Google Drive Trigger" and "When chat message received" are active.
  2. 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.
  3. Chat with Data: To interact with your knowledge base, you'll need to send messages via the When chat message received node. 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.