Chat with Postgresql database
Who is this template for?
This workflow template is designed for any professionals seeking relevent data from database using natural language.
How it works
- Each time user ask's question using the n8n chat interface, the workflow runs.
- Then the message is processed by AI Agent using relevent tools -
Execute SQL Query,Get DB Schema and Tables ListandGet Table Definition, if required. Agent uses these tool to form and run sql query which are necessary to answer the questions. - Once AI Agent has the data, it uses it to form answer and returns it to the user.
Set up instructions
Complete the Set up credentials step when you first open the workflow. You'll need a Postgresql Credentials, and OpenAI api key.
Template was created in n8n v1.77.0
Chat with PostgreSQL Database Workflow
This n8n workflow provides a foundation for building a conversational AI agent that can interact with a PostgreSQL database. It leverages LangChain nodes to set up a chat interface, an AI agent, a language model, and a simple memory system.
What it does
This workflow sets up the core components for an AI agent capable of engaging in a chat conversation:
- Listens for Chat Messages: The workflow is triggered whenever a new chat message is received, acting as the entry point for user interaction.
- Manages Conversation History: A simple memory buffer is used to store and retrieve previous chat messages, allowing the AI to maintain context throughout the conversation.
- Processes with an AI Chat Model: An OpenAI Chat Model is integrated to understand user input and generate responses.
- Orchestrates with an AI Agent: An AI Agent node (from LangChain) is the central orchestrator, responsible for taking the user's message, considering the conversation history, and deciding on the appropriate action or response using the configured language model and potentially other tools (which would be added to the agent in a more complete workflow).
Prerequisites/Requirements
To use this workflow, you will need:
- n8n Instance: A running instance of n8n.
- OpenAI API Key: An API key for OpenAI to use with the
OpenAI Chat Modelnode. This will need to be configured as an n8n credential.
Setup/Usage
- Import the Workflow:
- Copy the provided JSON code for the workflow.
- In your n8n instance, click on "Workflows" in the left sidebar.
- Click "New" -> "Import from JSON".
- Paste the JSON code and click "Import".
- Configure Credentials:
- Locate the
OpenAI Chat Modelnode. - Click on it to open its settings.
- In the "Authentication" section, select or create an "OpenAI API" credential.
- Provide your OpenAI API Key.
- Locate the
- Activate the Workflow:
- Toggle the workflow to "Active" in the top right corner of the canvas.
This workflow provides the basic conversational structure. To enable interaction with a PostgreSQL database, you would typically add:
- Tools for the AI Agent: Add database-specific tools (e.g., a SQL query tool, a custom function to interact with your PostgreSQL database) to the
AI Agentnode. These tools would allow the agent to execute queries or perform operations based on the user's requests. - PostgreSQL Node: Configure a PostgreSQL node to connect to your database and execute queries as instructed by the AI Agent's tools.