Animated data flow diagram

AI SQL Agent with Dynamic Chart Generation

Version: 1.0.0 | Last Updated: 2025-05-16

Integrates with:

OpenAI PostgreSQL QuickChart.io Langchain

Overview

Unlock Interactive Data Analysis & Visualization with this AI Agent

This n8n workflow transforms how you interact with your SQL databases. It's an AI Agent that understands your questions in plain English, queries your database (e.g., PostgreSQL, MySQL), and returns answers conversationally. What makes it stand out is its ability to intelligently decide when a visual representation would be beneficial, automatically generating a chart using OpenAI and QuickChart.io to accompany the textual answer. This AI Agent handles the complexity of SQL and chart generation, allowing you to focus on insights.

Key Features & Benefits

  • Natural Language Database Querying: Ask questions like "What were our top 5 best-selling products last month?" or "Show me user sign-ups per day for the last week as a bar chart." No SQL knowledge needed.
  • AI-Powered Data Interpretation: Leverages OpenAI's GPT-4o to understand complex requests, translate them into accurate SQL queries, and interpret the results.
  • Automated & Intelligent Chart Generation: The AI Agent, augmented with a classifier, determines if the data warrants a chart. If so, it uses OpenAI's structured output (JSON schema) to create a precise Chart.js definition and renders it via QuickChart.io.
  • Supported Chart Types: Generates various chart types including bar, line, pie, doughnut, and more, based on the data and request.
  • Conversational Context: Remembers the conversation history, allowing for follow-up questions and iterative data exploration.
  • Adaptable SQL Agent: The core SQL agent's prompts can be customized for specific SQL dialects, database schemas, and desired output formats.
  • Direct Database Connection: Securely connects to your existing SQL databases to work with your live data.

Use Cases

  • Enable marketing teams at B2C e-commerce companies to query sales databases for campaign performance (e.g., 'Chart daily sales for product X after promotion Y') and get instant visual reports.
  • Allow product managers at B2B SaaS companies to ask for user engagement metrics (e.g., 'What's the feature adoption rate for new users this quarter? Show as a pie chart.') without developer intervention.
  • Provide founders and solopreneurs with a quick way to analyze business KPIs from their operational database through a simple chat interface, complete with visualizations.
  • Streamline internal reporting by generating charts on-demand for sales figures, customer support trends, or operational data directly from database queries.
  • Empower non-technical users across departments to perform self-service data analysis, reducing reliance on data teams for ad-hoc requests.

Prerequisites

  • An n8n instance (Cloud or self-hosted).
  • OpenAI API Key with access to a GPT-4o model (or similar capable model like gpt-4o-2024-08-06 as used in the template for chart generation).
  • Credentials for your SQL database (the template is pre-configured for PostgreSQL, specifically using a credential named 'Coffee Sales Postgres' in the 'AI Agent' node). You will need to adapt this for your database type and credentials.

Setup Instructions

  1. Download the n8n workflow JSON file.
  2. Import the workflow into your n8n instance.
  3. Configure the 'OpenAI Chat Model', 'OpenAI Chat Model Classifier', and 'OpenAI - Generate Chart definition with Structured Output' (HTTP Request node) with your OpenAI API Key and credential.
  4. In the 'AI Agent' node: a. Select your SQL database type from the 'Database' dropdown. b. Configure the 'Credential' parameter to connect to your SQL database. You may need to create a new credential in n8n for your database if you haven't already.
  5. (Optional) Customize the prefixPrompt in the 'AI Agent' node to better suit your database's SQL dialect or specific querying requirements.
  6. (Optional) Review and adjust the system_prompt in the 'OpenAI - Generate Chart definition with Structured Output' node to fine-tune chart generation logic or supported chart types if needed.
  7. The chart generation capability is invoked as an internal tool. The 'Text Classifier - Chart required?' node decides whether to trigger the 'Execute Workflow' node, which in turn calls the 'Execute "Generate a chart" tool' trigger within this same workflow. This should work out-of-the-box.
  8. Activate the workflow. You can interact with the AI Agent via the 'When chat message received' trigger's URL or by embedding the n8n chat interface.

Tags:

AI AgentSQLData VisualizationOpenAIAutomationChatbotDatabaseLangchainChart Generation

Want your own unique AI agent?

Talk to us - we know how to build custom AI agents for your specific needs.

Schedule a Consultation