At a glance
- Trigger
- Execute Workflow Trigger (Execute "Generate a chart" tool)
- Nodes
- 14
- Apps
- OpenAI
- AI nodes
- 7
- Credentials
- 2 types
- Category
- AI agents and LLM tools
Apps and integrations
Node types used
How it works
The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.
Information Extractor
- This Information Extractor is added to extract the user's question
- In some cases, if the question contains a visualization aspect, the SQL Agent may not responding accurately.
SQL Agent
- This SQL Agent is connected to a Database.
- It queries the Database for each user message.
- In this example, the prompt has been slightly changed to address an issue with querying a Supabase DB. Feel free to change the Prefix Prompt to suit your needs.
- This example uses the data from this Kaggle dataset
Generate a Quickchart definition
HTTP Request node
- Send the chart query to OpenAI, with a defined JSON response format - using HTTP Request node as it has not yet been implemented in the OpenAI nodes
- The JSON structure is based on ChartJS and Quickchart.io definitions, that let us create nice looking graphs.
- The output is a JSON containing the chart definition that is passed to the next node.
Set Response node
- Adds the chart definition at the end of a Quickchart.io URL (see documentation)
- Note that in the parameters, we specify the width to 250 in order to be properly displayed in the chart interface.
Respond with a text only or also include a chart
- The text classifier determines if the response from the SQL Agent would benefit from a chart
- If it does, then it executes the subworkflow to dynamically generate a chart, and append the chart to the response from the SQL Agent
- If it doesn't, then the SQL Agent response is directly outputted.
Setup notes
The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.
Overview
- This workflow aims to provide data visualization capabilities to a native SQL Agent.
- Together, they can help foster data analysis and data visualization within a team.
- It uses the native SQL Agent that works well and adds visualization capabilities thanks to OpenAI’s Structured Output and Quickchart.io.
How it works
- Information Extraction
- The Information Extractor identifies and extracts the user's question.
- If the question includes a visualization aspect, the SQL Agent alone may not respond accurately.
- SQL Querying
- It leverages a regular SQL Agent: it connects to a database, queries it, and translates the response into a human-readable format.
- Chart Decision
- The Text Classifier determines whether the user would benefit from a chart to support the SQL Agent's response.
- Chart Generation
- If a chart is needed, the sub-workflow dynamically generates a chart and appends it to the SQL Agent’s response.
- If not, the SQL Agent’s response is output as is.
- Calling OpenAI for Chart Definition
- The sub-workflow calls OpenAI via the HTTP Request node to retrieve a chart definition.
- Building and Returning the Chart
- In the "Set Response" node, the chart definition is appended to a Quickchart.io URL, generating the final chart image.
- The AI Agent returns the response along with the chart.
How to use it
- Use an existing database or create a new one.
- For example, I've used this Kaggle dataset and uploaded it to a Supabase DB.
- Add the PostgreSQL or MySQL credentials.
- Alternatively, you can use SQLite binary files (check this template).
- Activate the workflow.
- Start chatting with the AI SQL Agent.
- If the Text Classifier determines a chart would be useful, it will generate one in addition to the SQL Agent's response.
Notes
- The full Quickchart.io specifications have not been fully integrated, so there may be some glitches (e.g., radar graphs may not display properly due to size limitations).
Nodes in this workflow
The 14 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:
- OpenAI Chat Model (OpenAI Chat Model)
- Execute Workflow (Execute Workflow)
- Execute "Generate a chart" tool (Execute Workflow Trigger)
- OpenAI - Generate Chart definition with Structured Output (HTTP Request)
- Set response (Edit Fields (Set))
- When chat message received (Chat Trigger)
- Set Text output (Edit Fields (Set))
- Set Text + Chart output (Edit Fields (Set))
- AI Agent (AI Agent)
- Window Buffer Memory (Window Buffer Memory)
- OpenAI Chat Model Classifier (OpenAI Chat Model)
- Text Classifier - Chart required? (Text Classifier)
- User question + Agent initial response (Edit Fields (Set))
- Information Extractor - User question (Information Extractor)
Credentials you need
- OpenAI API – used by OpenAI - Generate Chart definition with Structured Output, OpenAI Chat Model and OpenAI Chat Model Classifier
- Postgres – used by AI Agent
Configure your own credentials after importing. Create each credential in n8n, then select it in the node.
How to import into n8n
- Download the JSON file with the button above.
- In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
- Open each node that shows a warning and select or create its credential.
- Read the notes on the canvas, test the workflow with sample data, then activate it.
Do you need somewhere to run it? Read our guide to self-hosted n8n.