Templates / Analyze legal contract risk with Google Gemini hybrid RAG and Supabase

Analyze legal contract risk with Google Gemini hybrid RAG and Supabase

Ready for 3.0

by Divyanshu Gupta βœ“ Β· 210 views Β· created 2026-04-26 Β· Document Extraction, AI RAG

Ready for 3.0. This template uses no nodes that n8n 3.0 removes. No removed nodes, versions or helpers found. Static check, last run 2026-10-19.

Nodes used (11)

Google GeminiAI AgentGoogle Gemini Chat ModelSupabase

What it does

πŸš€ What This Workflow Does This workflow transforms any PDF legal contract into a detailed AI-powered risk report β€” in under 5 minutes. Upload a contract, and the system automatically splits it into clauses, analyses each one using Hybrid RAG (semantic + keyword search), scores risk as HIGH / MEDIUM / LOW, and delivers plain-English explanations with safer alternative wording. --- πŸ”₯ Why Hybrid RAG? Most dangerous clauses don't use obvious legal keywords. "The Client accepts full responsibility for all third-party claims" is an indemnification clause β€” but keyword search misses it. Hybrid RAG combines: - Vector Search (pgvector) β€” finds semantically similar risky patterns - BM25 Keyword Search β€” catches explicit legal red flags - RRF Reranking β€” merges both results with clause-type boosting --- πŸ” What It Does - Accepts a PDF contract via webhook (with async jobid tracking) - Splits contract into individual numbered clauses - Classifies each clause type using Google Gemini (indemnification, IP, termination, etc.) - Generates vector embeddings and searches a Supabase knowledge base - Scores each clause HIGH / MEDIUM / LOW using regex + AI - AI Agent (Gemini Flash) explains risk in plain language + suggests safer wording - Aggregates all results into a single JSON report - Saves report to Supabase (frontend polls for result asynchronously) --- βš™οΈ Architecture (Two Pipelines) Pipeline 1 β€” Ingestion: Builds the knowledge base of risky clause patterns in Supabase Pipeline 2 β€” Query: Analyses new contracts against the knowledge base Both pipelines run in the same workflow β€” the branch splits at Extract Embedding. --- 🧠 Key Technical Decisions - Async architecture β€” Frontend fires request + polls Supabase. No timeout issues. - jobid tracking β€” Preserved across all nodes via ...$json spread - RRF Reranking β€” Combines vector + BM25 scores with type-based boost multipliers - Regex Risk Scorer β€” First-pass risk classification before expensive LLM call - Gemini Flash β€” Fast, cost-efficient LLM for per-clause annotation --- πŸ“¦ Requirements - Google Gemini API key β€” for clause classification + embeddings + AI Agent - Supabase project β€” with pgvector extension enabled - Supabase tables: legalclauses (knowledge base) + reports (results) - Supabase functions: matchclauses() + keywordsearchclauses() - Frontend (optional): HTML/CSS/JS web app hosted on Netlify --- πŸ’‘ Example Use Cases - Freelancers reviewing client contracts before signing - Startups evaluating vendor or investor agreements - Legal ops teams standardising contract review at scale - Business owners catching risky clauses without legal fees --- 🎯 Output - Per-clause: risklevel, plain-English explanation, riskreason, saferalternative, keyobligations, legalarea - Summary: overallriskscore, riskdistribution, legalareas map, highriskclauses list - Stored as JSON in Supabase reports table, keyed by jobid

View the original on n8n.io β†’ Check your own copy

Get the weekly β€œwhat broke” email

New templates, newly broken ones, and n8n release changes. One short email a week.

Form not loading? Subscribe here.