See llms.txt for all machine-readable content.

Back to Templates

Answer Google Drive document questions with citations and OpenAI RAG

Quick overview

This workflow indexes files from a Google Drive folder into an in-memory vector store and answers questions in an n8n Chat using OpenAI, returning short answers with file citations and a staleness warning when the best source document is older than a defined threshold.

How it works

  1. Triggers when a chat question is asked, on a nightly schedule, or when n8n restarts.
  2. On restart, clears the existing file index in n8n Data Tables so all documents get re-indexed.
  3. On the schedule (or after a restart), lists all files in the configured Google Drive folder, compares them to the stored file index, and decides whether a full re-index is needed.
  4. When re-indexing runs, downloads each Google Drive file, loads and splits its text into chunks, and embeds and stores those chunks in an in-memory vector store with file metadata (name, URL, and last-modified date).
  5. When a chat question is received, embeds the question, retrieves the closest chunks from the in-memory vector store, filters by a similarity threshold, and checks whether the best matching source is stale.
  6. Uses OpenAI Chat to generate a structured answer strictly from the retrieved context, then formats the reply with citations and an optional staleness warning or a not-found response.
  7. Logs each indexing run and each chat question (including whether it was answered and which files were cited) to n8n Data Tables, and returns the final message to the chat.

Setup

  1. Add Google Drive OAuth2 credentials and set the target Google Drive folder ID in the workflow settings.
  2. Add an OpenAI credential and confirm the chat model and embedding model values in “Your Settings” match your OpenAI account access.
  3. Run the manual “Run once to create the tables” path to create the required n8n Data Tables (rag_file_index, rag_query_log, and rag_index_runs).
  4. Run the nightly indexing trigger once to build the initial in-memory index, then test by asking a question via the Chat trigger.
  5. Update the “Who to ask when not found” contact in “Your Settings” and publish the workflow so the schedule and restart trigger can run.

Additional info

The chat is private by default. Before you share it, choose an authentication option, because anyone with the link can ask about your documents. Any change to a file re-reads the whole folder, so this suits a small folder of short documents. The index lives in memory and is rebuilt after an n8n restart.

Need this fitted to your own tools or process? Tell us the job and we will send a fixed-price quote within one working day. fractionalhq.uk/custom-builds