⚒️ Conversing with data: transforming text into SQL queries and visual curves

3,380 views · ⚒️ Engineering

Description

Conversational Data Retrieval and Visualization Workflow

This workflow enables users to interact with a PostgreSQL database using natural language. It translates text inputs into SQL queries, retrieves the corresponding data, and generates visualizations using QuickChart, facilitating seamless data analysis without manual query writing.

Table of Contents

Pre-conditions and Requirements

1. API Keys and Services Required

To operate this workflow, access to the following services is necessary:

2. n8n Instance Setup

Database Schema Setup

Before initiating the workflow, ensure that the database schema is extracted and saved:

  1. Extract Schema: Retrieve the database schema, including table names and column details.
  2. Save Schema: Store the extracted schema in a JSON file for reference during query generation.

Step-by-Step Workflow Explanation

  1. User Input Handling

    • The workflow begins by receiving a natural language query from the user.
  2. Schema Retrieval

    • Loads the previously saved database schema from the JSON file.
  3. AI-Based SQL Generation

    • Combines the user’s query with the database schema.
    • Utilizes the DeepSeek API to translate the natural language query into a SQL statement.
  4. SQL Query Execution

    • Executes the generated SQL query against the PostgreSQL database.
    • Retrieves the data corresponding to the query.
  5. Data Visualization

    • Formats the retrieved data into a structure compatible with QuickChart.
    • Sends the data to QuickChart to generate a visual representation.
      • Example: To create a bar chart, construct a URL with the chart configuration:
        https://quickchart.io/chart?c={type:'bar',data:{labels:['Label1','Label2'],datasets:[{label:'Dataset1',data:[10,20]}]}}
        This URL returns an image of the chart.
  6. Response Delivery

    • Presents the generated visualizations and data insights to the user.

Customization Guide

Modifying the AI Model

Changing Visualization Services

Expanding Database Support


This workflow streamlines the process of data retrieval and visualization, allowing users to interact with their database using natural language, thereby enhancing accessibility and efficiency in data analysis.


🔗 Nodes Used

Postgres, AI Agent, OpenAI Chat Model, Simple Memory, Structured Output Parser, Read/Write Files from Disk

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup