workflows.fit
Back to n8n workflows
n8n templateFreeBy Divyanshu Gupta

Analyze legal contract risk with Google Gemini hybrid RAG and Supabase

šŸš€ 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 ...

DevelopmentCore NodesData & StorageAILangchainExtract From FileGoogle GeminiCode
Loading interactive preview...

Template notes

šŸš€ 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.