AutomationFlowsData & Sheets › AI Legal Chat Assistant with OpenAI & Supabase

AI Legal Chat Assistant with OpenAI & Supabase

Original n8n title: Legal Chat Assistant

Legal Chat Assistant. Uses openAi, supabase, respondToWebhook. Webhook trigger; 7 nodes.

Webhook trigger★★★★☆ complexityAI-powered7 nodesOpenAISupabase
Data & Sheets Trigger: Webhook Nodes: 7 Complexity: ★★★★☆ AI nodes: yes Added:

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "name": "Legal Chat Assistant",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "chat",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-chat",
      "name": "Chat Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "resource": "text",
        "operation": "embed",
        "model": "text-embedding-3-small",
        "input": "={{ $json.query }}"
      },
      "id": "create-query-embedding",
      "name": "Create Query Embedding",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM match_laws($1, $2, $3)",
        "parameters": [
          "={{ $json.embedding }}",
          "0.7",
          "5"
        ]
      },
      "id": "search-laws",
      "name": "Search Laws",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Prepare context for AI\nconst laws = $input.first().json;\nconst query = $input.first().json.query;\n\n// Create context from matched laws\nconst context = laws.map(law => \n  `Ti\u00eau \u0111\u1ec1: ${law.title}\\n` +\n  `\u0110i\u1ec1u/Kho\u1ea3n: ${law.article_reference || 'N/A'}\\n` +\n  `N\u1ed9i dung: ${law.content}\\n` +\n  `Ngu\u1ed3n: ${law.source || 'N/A'}\\n`\n).join('\\n---\\n');\n\n// Create sources array\nconst sources = laws.map(law => ({\n  id: law.id,\n  title: law.title,\n  article_reference: law.article_reference,\n  source: law.source,\n  similarity: law.similarity\n}));\n\nreturn {\n  json: {\n    query: query,\n    context: context,\n    sources: sources,\n    matched_ids: laws.map(law => law.id)\n  }\n};"
      },
      "id": "prepare-context",
      "name": "Prepare Context",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "create",
        "model": "gpt-3.5-turbo",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "B\u1ea1n l\u00e0 m\u1ed9t tr\u1ee3 l\u00fd ph\u00e1p lu\u1eadt chuy\u00ean nghi\u1ec7p. H\u00e3y tr\u1ea3 l\u1eddi c\u00e2u h\u1ecfi v\u1ec1 ph\u00e1p lu\u1eadt Vi\u1ec7t Nam d\u1ef1a tr\u00ean c\u00e1c v\u0103n b\u1ea3n ph\u00e1p lu\u1eadt \u0111\u01b0\u1ee3c cung c\u1ea5p. Tr\u1ea3 l\u1eddi ch\u00ednh x\u00e1c, r\u00f5 r\u00e0ng v\u00e0 c\u00f3 tr\u00edch d\u1eabn ngu\u1ed3n. N\u1ebfu kh\u00f4ng t\u00ecm th\u1ea5y th\u00f4ng tin li\u00ean quan, h\u00e3y n\u00f3i r\u00f5 \u0111i\u1ec1u \u0111\u00f3."
            },
            {
              "role": "user",
              "content": "C\u00e2u h\u1ecfi: {{ $json.query }}\\n\\nV\u0103n b\u1ea3n ph\u00e1p lu\u1eadt li\u00ean quan:\\n{{ $json.context }}"
            }
          ]
        },
        "options": {
          "temperature": 0.3,
          "maxTokens": 1000
        }
      },
      "id": "generate-response",
      "name": "Generate Response",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "query_logs",
        "columns": "user_id, query, matched_ids, response"
      },
      "id": "log-query",
      "name": "Log Query",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { \"response\": $json.message.content, \"sources\": $json.sources, \"matched_ids\": $json.matched_ids } }}"
      },
      "id": "response",
      "name": "Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1560,
        300
      ]
    }
  ],
  "connections": {
    "Chat Webhook": {
      "main": [
        [
          {
            "node": "Create Query Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Query Embedding": {
      "main": [
        [
          {
            "node": "Search Laws",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Laws": {
      "main": [
        [
          {
            "node": "Prepare Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Context": {
      "main": [
        [
          {
            "node": "Generate Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Response": {
      "main": [
        [
          {
            "node": "Log Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Query": {
      "main": [
        [
          {
            "node": "Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

Legal Chat Assistant. Uses openAi, supabase, respondToWebhook. Webhook trigger; 7 nodes.

Source: https://github.com/Minh-sawada/DO_AN_CHUYEN_NGANH/blob/614b91391cf04523a3317fbe44c6e01cf5ed680b/n8n-workflows/chat-workflow.json — original creator credit. Request a take-down →

More Data & Sheets workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

Data & Sheets

Postgres. Uses openAi, postgres, postgresTool, httpRequest. Webhook trigger; 19 nodes.

OpenAI, Postgres, Postgres Tool +2
Data & Sheets

• Multi-Layer Message Analysis - Every customer interaction passes through three specialized AI classifiers: privacy detection (identifies sensitive data and security requirements), intent recognition

OpenAI, OpenAI Chat, Tool Think +13
Data & Sheets

upload_answer. Uses httpRequest, openAi, googleDrive, googleSheets. Webhook trigger; 27 nodes.

HTTP Request, OpenAI, Google Drive +2
Data & Sheets

DAta lake 1. Uses openAi, httpRequest, googleSheets, mongoDb. Webhook trigger; 23 nodes.

OpenAI, HTTP Request, Google Sheets +1
Data & Sheets

This workflow receives English practice messages via a webhook, uses an OpenRouter chat model with Postgres-backed conversation memory to generate tutoring feedback, logs the exchange to Supabase, and

Agent, OpenRouter Chat, Memory Postgres Chat +1