See llms.txt for all machine-readable content.

Back to Templates

Ingest and index Nextcloud documents for RAG with Mistral OCR and Postgres

Quick overview

This workflow polls a Nextcloud folder, OCRs newly uploaded documents with Mistral, extracts normalized metadata with Mistral Chat Completions, and stores the results in Postgres before moving the file into the appropriate Nextcloud archive path, optionally requesting human review by email.

How it works

  1. Runs on a schedule and lists files in a Nextcloud “A_classer” inbox folder.
  2. Selects the oldest eligible file, moves it to an “en_cours” working folder in Nextcloud, downloads it, converts it to base64, and computes a SHA-256 fingerprint.
  3. Rejects unsupported file types by moving them to a Nextcloud “erreurs” folder, and checks Postgres to see if the SHA-256 has already been ingested.
  4. If the document is a duplicate, moves it to a Nextcloud “doublons” folder; otherwise, sends the file to the Mistral OCR API and builds an extraction request using document-type vocabulary loaded from Postgres.
  5. Uses the Mistral Chat Completions API to extract structured metadata, validates and normalizes the output (dates, confidence, keywords), and saves the document and extracted fields to Postgres via an ingestion function.
  6. Moves the file in Nextcloud to the storage path decided by Postgres (for example bibliotheque/, a_revoir/, or rejetes/) and either marks it as automatically accepted or sends an email with an n8n review form link.
  7. If human review is required, waits for the form submission, applies the reviewer’s decision in Postgres, moves the file to its final Nextcloud location, and outputs the document ID for optional downstream chunking/embedding.

Setup

  1. Create a Postgres database with the required tables and functions (including ref_doc_type, ingest_document(), and review_document()) and add a Postgres credential in n8n.
  2. Configure a Nextcloud account credential with WebDAV access and create the required folders under your chosen root (A_classer, en_cours, bibliotheque, a_revoir, rejetes, doublons, erreurs).
  3. Add a Mistral API credential (HTTP header authentication) and ensure the OCR and extraction model names in the workflow configuration match models available to your account.
  4. Add an SMTP credential for sending review emails, and set the reviewer and sender email addresses in the workflow configuration.
  5. Update the workflow configuration values (Nextcloud paths, minimum file age, and any organization-specific defaults) to match your environment before activating the schedule.