Advanced AI Chat Assistant with External Tooling and Database Memory - n8n Workflow

Build an advanced n8n workflow using OpenAI Assistants, PostgreSQL memory, and external API calls (MySQL/HTTP). This n8n template manages context and retrieves real-time data.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Automation specialists looking for complex n8n templates integrating AI agents.
E-commerce or service providers needing a smart chatbot that leverages real-time product data (MySQL).
Developers utilizing n8n to manage conversational context with persistent database memory (Postgres).
Teams who want to extend their OpenAI Assistant capabilities using custom n8n node tooling.

Overview

This is a powerful, production-ready n8n workflow designed to power a modern chat assistant. The primary challenge this n8n template solves is maintaining conversational state (memory) and enabling the AI to interact with external systems for real-time data access. It uses PostgreSQL to ensure long-term, persistent memory storage, allowing the AI to recall details about the user, even across sessions. Furthermore, by utilizing specialized n8n node components like the MySQL Tool and HTTP Request Tool, the OpenAI assistant can perform complex tasks, such as looking up product quotes based on detailed user criteria (age, location, group size), transforming the chat assistant from a simple responder into a powerful service agent. This architecture highlights the flexibility of the n8n platform for advanced AI orchestration.

How it Works

The n8n workflow begins execution using the dedicated Chat Trigger n8n node, which initiates the conversation upon receiving a user request, along with session information and optional lead data.


  1. Initial Data Check: An If n8n node checks if initial lead qualification data (leadData) is present.

  2. Lead Data Ingestion (If True): If leadData exists, the flow takes the true path. An Edit Fields n8n node constructs a highly specific, non-conversational instruction prompt using the available user data (age, city, profession, etc.). This prompt is fed into the first OpenAI n8n node, whose sole purpose is to utilize the Postgres Chat Memory n8n node to silently save this lead information, establishing context for future interactions.

  3. Query Preparation: After the initial data ingestion, another Edit Fields n8n node retrieves the original user query and session ID, preparing for the main conversational response.

  4. Core AI Processing: The user’s actual query is passed to the main conversational engine, OpenAI2.

  5. Context and Tooling: This primary OpenAI n8n node retrieves session history from the Postgres Chat Memory n8n node (configured with a 30-message context window) to maintain context. It is linked to multiple AI tools: the 'Products in Daatabase' MySQL n8n node, the 'Knowledge Base' HTTP tool, and the 'External API' HTTP tool, which the OpenAI assistant uses to gather the necessary data to formulate a comprehensive answer. The flexibility of this n8n workflow allows for complex data retrieval.

  6. Final Response: The OpenAI Assistant generates the response, utilizing data from the tools if needed, and the n8n trigger sends the final output back to the user interface.

Installation Guide

To deploy this advanced n8n workflow, follow these steps:


  1. Import: Copy the entire n8n workflow JSON provided and paste it into your n8n canvas using the 'New' -> 'Import from JSON' option.

  2. OpenAI Credentials: You must provide credentials for the OpenAI n8n node. This n8n workflow requires access to two specific OpenAI Assistants (identified by IDs asstnumdCoMZPQ6GwfiJg5drg9hr and asstx2qfc7EuoPv7XGOL84ClEZ3L) which must be pre-configured in your OpenAI account, including their function calling capabilities matching the tools defined in this n8n template.

  3. PostgreSQL Setup: Configure the Postgres Chat Memory n8n node credentials to connect to your database. Ensure the specified table (aimessages) exists for storing chat history.

  4. MySQL Setup: Configure the MySQL n8n node credentials for the 'Products in Daatabase' tool. The MySQL connection must allow the complex product search query defined in the n8n node parameters to run successfully.

  5. External Tools: Review and, if necessary, update the URLs and body structures in the 'Knowledge Base' and 'External API' HTTP Request Tools to match your live service endpoints.

  6. Activation: Once all credentials and configurations are set, activate the n8n workflow by toggling the 'Active' switch. The n8n trigger webhook endpoint is now live, ready to accept chat requests.

Node Details

Chat Trigger (n8n trigger): The entry point of this n8n workflow. It is publicly accessible and listens for incoming chat requests, providing the initial chatInput and sessionid.
If n8n node: Controls the branching logic. It determines if the incoming payload contains leadData, routing the request either to a data ingestion path or directly to the standard conversation path.
Edit Fields (Set n8n node): Used to construct specialized system prompts for the AI, particularly for silently uploading user lead information into the chat memory, which is critical for personalized responses.
OpenAI (LangChain Assistant n8n node): There are two instances managing the conversational flow. They utilize specific OpenAI Assistants to process queries, handle tool utilization, and generate the final output for this n8n workflow.
Postgres Chat Memory (LangChain Memory n8n node): A crucial n8n node for persistent state management. It retrieves and saves session history using the sessionid key, allowing the AI to maintain long-term context using Postgres.
Products in Daatabase (MySQL Tool n8n node): Allows the AI to execute a complex SQL query to search for filtered product results based on criteria (like age and city), enabling real-time data querying within the n8n workflow.


  • Knowledge Base / External API (HTTP Request Tool n8n node): Configured as AI Tools, these allow the main OpenAI n8n node to query external REST services, fetching dynamic information needed to accurately respond to user inquiries (a key function in this n8n template).

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by

Software Developer. Instagram @ferzinia

Featured*