Skip to content

AI Assistant Agent: New RAG Input Flow

This workflow automatically manages vector store updates for AI assistants by monitoring Google Drive folders for changes, processing various document types, and maintaining synchronized knowledge bases for retrieval-augmented generation (RAG) systems.

Purpose

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

How It Works

  1. Change Detection: The workflow runs every 30 minutes to check for changes in Google Drive folders using the Drive Changes API
  2. Folder Processing: Identifies subfolders within a configured parent directory and maps them to corresponding AI assistants
  3. File Analysis: Retrieves all files from changed folders and filters out trashed items
  4. Assistant Matching: Matches folder names to existing OpenAI assistants (removing "Assistant" suffix for comparison)
  5. Vector Store Cleanup: Deletes existing files from the assistant's vector store to prevent duplicates
  6. File Processing: Downloads files from Google Drive and processes different formats (DOCX, PDF, Markdown, PowerPoint, JSON)
  7. Upload & Attachment: Uploads processed files to OpenAI and attaches them to the appropriate vector stores
  8. Completion Notification: Sends a Telegram message confirming successful updates

Mermaid Diagram

graph TD
    A[Schedule Trigger] --> B[Configuration]
    B --> C[List Subfolders]
    C --> D[Split Subfolders]
    D --> E[List Files1]
    E --> F[Mapping1]
    F --> G[Match changes within big folder]
    G --> H[Deduplicate Changes]
    H --> I[Changelist]
    I --> J[Split changes1]
    J --> K[Remove Duplicates]
    K --> L[OpenAI1]
    L --> M[Remove assistant from name]
    M --> N[Assistants to update]
    N --> O[Get Assistant Details1]
    O --> P[List Vector Store Files1]
    P --> Q[Each folder's vector store]
    Q --> R[Assistant Info and File Info]
    R --> S[Remove Duplicates1]
    S --> T[Split Out1]
    T --> U[Delete Files1]
    U --> V[Wait for delete flow]
    V --> W[Loop Over Items]
    W --> X[Download File From Google Drive]
    X --> Y[Switch]
    Y --> Z[Wait]
    Z --> AA[Adding extension to filename]
    AA --> BB[Upload File]
    BB --> CC[Attach to Vec Store]
    CC --> DD[Aggregate]
    DD --> EE[Send Completed Message]

Trigger

  • Schedule Trigger: Runs automatically every 30 minutes
  • Manual Trigger: Can be started manually via "Test workflow" button
  • Webhook: Alternative trigger endpoint available (currently disabled)

Nodes Used

Node Type Purpose
Schedule Trigger Initiates workflow every 30 minutes
Google Drive Lists folders, files, and downloads documents
OpenAI Manages assistants, uploads files, and vector store operations
HTTP Request Interacts with OpenAI API and Google Drive Changes API
Telegram Sends notifications and handles user confirmations
PostgreSQL Stores and retrieves page tokens for change tracking
Filter Removes trashed files and applies conditions
Switch Routes files based on type (DOCX, PDF, Markdown, etc.)
Merge Combines data from different sources
Split Out/Split In Batches Processes arrays and manages batch operations
Set/Edit Fields Transforms and maps data between nodes
Remove Duplicates Eliminates duplicate entries
Code Adds file extensions to filenames for OpenAI compatibility

External Services & Credentials Required

  • OpenAI API: For assistant management and file operations
    • Credential: "OpenAI Assistants API"
    • Requires API key with assistant and file permissions
  • Google Drive API: For folder monitoring and file access
    • Credential: "Google Drive account" (OAuth2)
    • Requires Drive API access and appropriate folder permissions
  • Telegram Bot API: For notifications and user interactions
    • Credential: "Telegram account"
    • Requires bot token and chat ID configuration
  • PostgreSQL Database: For storing change tracking tokens
    • Credential: "Postgres account"
    • Requires database with drive_changes table

Environment Variables

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

Data Flow

Input: - Google Drive folder structure with documents - Scheduled trigger or manual execution

Processing: - Monitors Drive changes via API - Downloads and processes various file formats - Manages OpenAI vector stores and file attachments

Output: - Updated AI assistant vector stores - Telegram notifications about completion status - PostgreSQL records for change tracking

Error Handling

  • File download operations continue on error to handle inaccessible files
  • Vector store operations have error handling to prevent workflow failure
  • Loop operations are configured to continue on individual item failures
  • Unsupported file types (XLSX) are routed to a no-op node with a note about future conversion support

Known Limitations

  • XLSX files are not currently supported (marked for future development)
  • Manual approval steps are disabled but remain in the workflow structure
  • Hardcoded configuration values limit flexibility across environments
  • 30-minute polling interval may not capture rapid changes

No related workflows specified in the context.

Setup Instructions

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

  2. Configure Credentials:

    • Set up OpenAI API credentials with assistant permissions
    • Configure Google Drive OAuth2 access
    • Create Telegram bot and obtain credentials
    • Set up PostgreSQL database connection
  3. Database Setup:

    • Create drive_changes table in PostgreSQL with columns: pageToken, created_at, updated_at
  4. Update Configuration:

    • Modify the Configuration node with your Google Drive folder ID
    • Update Telegram chat ID for notifications
    • Adjust folder structure to match your organization
  5. Test Setup:

    • Run manually using "Test workflow" to verify all connections
    • Check that assistants are properly matched to folders
    • Verify file processing works for your document types
  6. Enable Automation:

    • Activate the workflow to enable scheduled execution
    • Monitor initial runs and adjust timing if needed