Skip to content

AI Assistant Agent: New RAG Input Flow

This workflow automates the synchronization of documents between Google Drive and OpenAI vector stores for AI assistants. It monitors Google Drive folders for changes every 30 minutes, detects modified files, and updates the corresponding OpenAI assistant vector stores to keep the AI knowledge base current.

Purpose

No business context provided yet — add a context.md to enrich this documentation.

This workflow serves teams using AI assistants that need their knowledge base to stay synchronized with documents stored in Google Drive. It eliminates the manual process of updating vector stores when documents change, ensuring AI assistants always have access to the latest information.

How It Works

  1. Change Detection: Every 30 minutes, the workflow checks Google Drive for file changes using the Drive Changes API
  2. Folder Processing: Identifies which assistant folders have been modified and retrieves their file lists
  3. Assistant Matching: Maps changed folders to their corresponding OpenAI assistants by name matching
  4. Vector Store Cleanup: Deletes all existing files from the assistant's vector store to prevent duplicates
  5. File Processing: Downloads modified files from Google Drive and processes them by file type (PDF, DOCX, MD, PPTX, JSON)
  6. Upload to OpenAI: Uploads processed files to OpenAI and attaches them to the appropriate vector stores
  7. Notification: Sends completion notifications via Telegram

Workflow Diagram

graph TD
    A[Schedule Trigger - 30min] --> B[Configuration]
    B --> C[List Subfolders]
    C --> D[Get Page Token from DB]
    D --> E{Token Exists?}
    E -->|No| F[Get Start Page Token]
    E -->|Yes| G[Drive Changes API]
    F --> G
    G --> H[Update Page Token in DB]
    G --> I[Split Changes]
    I --> J[Match Changes with Files]
    J --> K[Deduplicate Changes]
    K --> L[Get Changed Folders]
    L --> M[List OpenAI Assistants]
    M --> N[Match Assistants to Folders]
    N --> O[Get Assistant Details]
    O --> P[List Vector Store Files]
    P --> Q[Delete Existing Files]
    Q --> R[List Files from Drive]
    R --> S[Download Files]
    S --> T[File Type Switch]
    T --> U[Upload to OpenAI]
    U --> V[Attach to Vector Store]
    V --> W[Send Completion Message]

Trigger

  • Schedule Trigger: Runs every 30 minutes automatically
  • Manual Trigger: Can be triggered manually for testing

Nodes Used

Node Type Purpose
Schedule Trigger Runs workflow every 30 minutes
Google Drive Lists folders, downloads files, tracks changes
OpenAI Manages assistants, uploads files, handles vector stores
Telegram Sends notifications and confirmations
PostgreSQL Stores Google Drive change tokens
HTTP Request Direct API calls to OpenAI and Google Drive
Switch Routes files based on type (PDF, DOCX, etc.)
Filter Removes trashed files and matches conditions
Merge Combines data from different sources
Split Out Processes arrays of data
Code Adds file extensions for OpenAI compatibility

External Services & Credentials Required

Google Drive

  • Google Drive OAuth2 API: For accessing and downloading files
  • Permissions: Read access to specified folders

OpenAI

  • OpenAI API: For managing assistants and vector stores
  • Permissions: File upload, assistant management, vector store operations

Telegram

  • Telegram Bot API: For sending notifications
  • Setup: Bot token and admin chat ID required

PostgreSQL

  • Database: For storing Google Drive change tracking tokens
  • Table: drive_changes with columns for pageToken, created_at, updated_at

Environment Variables

The workflow uses a Configuration node with hardcoded values: - folder_id: "1sfTnMGube-MTyEbchWLQE_Cn-oKTU2G8" (Google Drive folder ID) - admin_chat_id: "5207485332" (Telegram chat ID for notifications)

Data Flow

Input

  • Google Drive folder structure with documents
  • OpenAI assistants with existing vector stores
  • Change tracking tokens from PostgreSQL

Processing

  • File change detection via Google Drive Changes API
  • Document download and type classification
  • Vector store synchronization
  • File upload to OpenAI

Output

  • Updated OpenAI vector stores with latest documents
  • Telegram notifications about completion status
  • Updated change tracking tokens in database

Error Handling

  • File Download Errors: Continues processing other files if individual downloads fail
  • OpenAI Upload Errors: Has fallback vector store attachment mechanism
  • Unsupported File Types: XLSX files are noted as unsupported with placeholder for future conversion
  • API Failures: Uses continueRegularOutput and continueErrorOutput settings to handle failures gracefully

Known Limitations

  • XLSX files are not currently supported (placeholder exists for future Excel conversion)
  • Requires manual configuration of folder IDs and chat IDs
  • 30-minute sync interval may not be suitable for real-time requirements
  • No automatic retry mechanism for failed uploads

No related workflows specified in the context.

Setup Instructions

  1. Import Workflow: Import the JSON into your n8n instance

  2. Configure Credentials:

    • Set up Google Drive OAuth2 API credentials
    • Configure OpenAI API credentials
    • Create Telegram bot and get API token
    • Set up PostgreSQL database connection
  3. Database Setup:

    1
    2
    3
    4
    5
    CREATE TABLE drive_changes (
      pageToken VARCHAR(255),
      created_at TIMESTAMP,
      updated_at TIMESTAMP
    );
    

  4. Update Configuration:

    • Modify the Configuration node with your Google Drive folder ID
    • Update the Telegram admin chat ID
    • Ensure OpenAI assistants are named to match Google Drive folder names
  5. Test Setup:

    • Use the manual trigger to test the workflow
    • Verify Google Drive access and file downloads
    • Check OpenAI assistant and vector store operations
    • Confirm Telegram notifications are working
  6. Activate: Enable the schedule trigger for automatic operation

  7. Monitor: Check Telegram notifications and workflow execution logs for any issues