Integrate n8n with GCP for Efficient Document Management

Integrating n8n with GCP for Document Management

This mirrors the Azure RAG architecture but uses Google Cloud Platform services — Vertex AI for embeddings, Vertex AI Search (or AlloyDB/Cloud SQL with pgvector) for vector storage, and n8n as the orchestration layer.


The Full Architecture

Your Documents (PDFs, Docs, Sheets)
Google Cloud Storage (GCS)
Document AI / Dataflow (chunk + clean)
Vertex AI Embeddings (text → vector)
Vertex AI Search / pgvector (store vectors)
n8n Workflow
User gets grounded answer + sources

GCP Services Mapping

Azure ServiceGCP EquivalentRole
Azure Data LakeGoogle Cloud Storage (GCS)Store raw documents
Azure Data FactoryCloud Dataflow / Document AIProcess & chunk text
Azure OpenAI EmbeddingsVertex AI EmbeddingsConvert text → vectors
Azure AI SearchVertex AI Search / pgvectorStore & search vectors
Azure OpenAI ChatVertex AI Gemini / PaLMGenerate answers
n8nn8nOrchestrate everything

Step-by-Step Implementation


Step 1 — Store Documents in GCS

Upload all your PDFs, Word docs, and text files to a GCS bucket:

# Create a bucket
gsutil mb gs://my-company-docs
# Upload documents
gsutil cp *.pdf gs://my-company-docs/raw/

Bucket structure:

gs://my-company-docs/
├── raw/ ← original documents
├── processed/ ← cleaned text chunks
└── embeddings/ ← vector JSON files

Step 2 — Process & Chunk Documents

Use Google Document AI to extract clean text from PDFs, then split into chunks:

# Cloud Function or Dataflow job
from google.cloud import documentai, storage
def chunk_document(text, chunk_size=500, overlap=50):
words = text.split()
chunks = []
for i in range(0, len(words), chunk_size - overlap):
chunk = " ".join(words[i:i + chunk_size])
chunks.append({
"chunk_id": f"chunk_{i}",
"text": chunk,
"source": "refund_policy.pdf",
"page": i // chunk_size + 1
})
return chunks

Output chunk format:

{
"chunk_id": "refund_policy_001",
"text": "Refunds are available within 30 days of purchase...",
"source": "refund_policy.pdf",
"page": 1,
"metadata": {
"department": "finance",
"last_updated": "2026-01-15"
}
}

Step 3 — Generate Embeddings with Vertex AI

Call the Vertex AI Embeddings API to convert each chunk into a vector:

# REST API call
POST https://us-central1-aiplatform.googleapis.com/v1/projects/YOUR_PROJECT/
locations/us-central1/publishers/google/models/text-embedding-004:predict
Headers:
Authorization: Bearer $(gcloud auth print-access-token)
Content-Type: application/json
Body:
{
"instances": [
{ "content": "Refunds are available within 30 days of purchase..." }
]
}

Response:

{
"predictions": [
{
"embeddings": {
"values": [0.023, -0.841, 0.334, ...],
"statistics": { "truncated": false, "token_count": 42 }
}
}
]
}

Vertex AI embedding models:

ModelDimensionsBest for
text-embedding-004768General text, RAG
text-multilingual-embedding-002768Multi-language docs
text-embedding-preview-0815768Latest preview

Step 4 — Store Vectors

You have two main options on GCP:

Option A — Vertex AI Search (fully managed)

# Create a data store
gcloud alpha discovery-engine data-stores create \
--project=YOUR_PROJECT \
--location=global \
--display-name="company-docs" \
--industry-vertical=GENERIC \
--solution-types=SOLUTION_TYPE_SEARCH

Option B — AlloyDB / Cloud SQL with pgvector (more control)

-- Enable pgvector extension
CREATE EXTENSION vector;
-- Create table with vector field
CREATE TABLE document_chunks (
chunk_id TEXT PRIMARY KEY,
text TEXT,
source TEXT,
page INT,
metadata JSONB,
embedding VECTOR(768) -- matches Vertex AI output dimensions
);
-- Create HNSW index for fast similarity search
CREATE INDEX ON document_chunks
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);

Insert a chunk with its vector:

INSERT INTO document_chunks
(chunk_id, text, source, embedding)
VALUES (
'refund_policy_001',
'Refunds are available within 30 days...',
'refund_policy.pdf',
'[0.023, -0.841, 0.334, ...]'::vector
);

Step 5 — Build the n8n Workflow

The n8n workflow has these nodes:

Webhook Trigger
HTTP Request → Vertex AI Embeddings
HTTP Request → pgvector / Vertex AI Search
Code Node → Format retrieved context
HTTP Request → Vertex AI Gemini (chat)
Respond to Webhook

Step 6 — Webhook Receives User Question

Incoming request to n8n:

{
"question": "What is the refund policy?",
"user_id": "user_123"
}

Step 7 — n8n Calls Vertex AI Embeddings

HTTP Request node configuration:

Method: POST
URL: https://us-central1-aiplatform.googleapis.com/v1/projects/
{{ $env.GCP_PROJECT }}/locations/us-central1/publishers/google/
models/text-embedding-004:predict
Headers:
Authorization: Bearer {{ $env.GCP_ACCESS_TOKEN }}
Content-Type: application/json
Body:
{
"instances": [
{ "content": "{{ $json.question }}" }
]
}

Output stored in state:

{ "query_vector": [0.021, -0.834, 0.291, ...] }

Step 8 — n8n Searches pgvector

HTTP Request node (calling Cloud SQL proxy or AlloyDB REST):

-- n8n Code Node generates this query
SELECT
chunk_id,
text,
source,
page,
1 - (embedding <=> '[0.021, -0.834, 0.291, ...]'::vector) AS similarity
FROM document_chunks
ORDER BY embedding <=> '[0.021, -0.834, 0.291, ...]'::vector
LIMIT 5;

pgvector distance operators:

OperatorMetricUse case
<=>Cosine distanceText similarity (recommended)
<->Euclidean distanceImage embeddings
<#>Negative dot productNormalized vectors

Results returned:

[
{ "chunk_id": "refund_policy_001", "text": "Refunds are available within 30 days...", "source": "refund_policy.pdf", "similarity": 0.97 },
{ "chunk_id": "returns_guide_003", "text": "To initiate a return, visit our portal...", "source": "returns_guide.pdf", "similarity": 0.81 }
]

Step 9 — Format Context in n8n Code Node

// n8n Code Node
const results = items[0].json.results;
const question = $node["Webhook Trigger"].json.question;
const context = results
.map(r => `Source: ${r.source} (Page ${r.page})\nContent: ${r.text}`)
.join("\n\n---\n\n");
return [{
json: {
question: question,
context: context,
sources: results.map(r => r.source)
}
}];

Step 10 — Send Grounded Prompt to Vertex AI Gemini

HTTP Request node:

Method: POST
URL: https://us-central1-aiplatform.googleapis.com/v1/projects/
{{ $env.GCP_PROJECT }}/locations/us-central1/publishers/google/
models/gemini-1.5-pro:generateContent
Body:
{
"contents": [{
"role": "user",
"parts": [{
"text": "You are an internal company assistant.\nAnswer ONLY using the context below.\nIf the answer is not in the context, say: I don't know.\nAlways cite the source document.\n\nContext:\n{{ $json.context }}\n\nQuestion: {{ $json.question }}"
}]
}],
"generationConfig": {
"temperature": 0.2,
"maxOutputTokens": 512
}
}

Step 11 — Return Answer to User

n8n Respond to Webhook node:

{
"answer": "Refunds are available within 30 days of purchase. To initiate a return, visit our returns portal.",
"sources": ["refund_policy.pdf", "returns_guide.pdf"],
"confidence": "high"
}

Complete n8n Workflow Diagram

┌─────────────────────────────────────────────────────────┐
│ n8n WORKFLOW │
│ │
│ [Webhook]──→[Vertex AI Embed]──→[pgvector Search] │
│ ↓ │
│ [Code: Format] │
│ ↓ │
│ [Gemini Chat] │
│ ↓ │
│ [Respond] │
└─────────────────────────────────────────────────────────┘

GCP vs Azure — Side by Side

StepAzureGCP
Document storageAzure Data LakeGoogle Cloud Storage
Text extractionAzure Form RecognizerDocument AI
ChunkingAzure Data FactoryCloud Dataflow / Functions
Embedding modeltext-embedding-ada-002text-embedding-004
Vector dimensions1,536768
Vector storeAzure AI SearchAlloyDB pgvector / Vertex AI Search
Search algorithmHNSW (built-in)HNSW via pgvector
LLMAzure OpenAI ChatVertex AI Gemini
Orchestrationn8nn8n

Security Best Practices on GCP

n8n running on GCP VM / Cloud Run
Uses Workload Identity (no hardcoded keys)
Accesses GCS, Vertex AI, AlloyDB
via IAM roles:
- roles/aiplatform.user
- roles/storage.objectViewer
- roles/cloudsql.client

Store secrets in Google Secret Manager, not in n8n environment variables directly:

# Store API credentials securely
gcloud secrets create vertex-ai-key --data-file=key.json
# n8n fetches at runtime via HTTP Request node
GET https://secretmanager.googleapis.com/v1/projects/YOUR_PROJECT/
secrets/vertex-ai-key/versions/latest:access

Key Takeaway

The GCP RAG pipeline with n8n gives you:

  • GCS for durable, scalable document storage
  • Document AI for accurate PDF/text extraction
  • Vertex AI Embeddings for state-of-the-art semantic vectors
  • pgvector on AlloyDB for flexible, SQL-native vector search
  • Gemini for grounded, citation-aware answer generation
  • n8n as the glue — zero custom application code needed

The result is a fully managed, enterprise-grade document Q&A system where every answer is grounded in your actual documents, with sources always cited.

Leave a comment