n8n
Workflow
Visualize Your Sql Agent Queries With Openai And Quickchart Io
This n8n workflow automates tasks and integrates with various services. Visualize Your Sql Agent Queries With Openai And Quickchart Io - ready to import and run in your n8n instance.
n8n
workflow.json
About This Script
This n8n workflow automates tasks and integrates with various services. Visualize Your Sql Agent Queries With Openai And Quickchart Io - ready to import and run in your n8n instance.
Features:
- Makes HTTP requests to external APIs
- Conditional logic and branching
Source Code
{
"nodes": [
{
"id": "50695e7f-3334-4124-a46e-1b3819412e26",
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
1260,
540
],
"parameters": {
"model": "gpt-4o",
"options": {
"temperature": 0.2
}
},
"credentials": {
"openAiApi": {
"id": "WqzqjezKh8VtxdqA",
"name": "OpenAi account - Baptiste"
}
},
"typeVersion": 1
},
{
"id": "2f07481d-3ca4-48ab-a8ff-59e9ab5c6062",
"name": "Execute Workflow",
"type": "n8n-nodes-base.executeWorkflow",
"position": [
2100,
300
],
"parameters": {
"options": {
"waitForSubWorkflow": true
},
"workflowId": {
"__rl": true,
"mode": "id",
"value": "={{ $workflow.id }}"
}
},
"typeVersion": 1.1
},
{
"id": "49120164-4ffc-4fe0-8ee3-4ae13bda6c8d",
"name": "Execute \"Generate a chart\" tool",
"type": "n8n-nodes-base.executeWorkflowTrigger",
"position": [
1320,
1140
],
"parameters": [],
"typeVersion": 1
},
{
"id": "0fc6eaf9-8521-44ec-987e-73644d0cba79",
"name": "OpenAI - Generate Chart definition with Structured Output",
"type": "n8n-nodes-base.httpRequest",
"position": [
1620,
1140
],
"parameters": {
"url": "https://api.openai.com/v1/chat/completions",
"method": "POST",
"options": [],
"jsonBody": "={\n \"model\": \"gpt-4o-2024-08-06\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"Based on the user request, generate a valid Chart.js definition. Important: - Be careful with the data scale and beginatzero that all data are visible. Example if ploted data 2 and 3 on a bar chart, the baseline should be 0. - Charts colors should be different only if there are multiple datasets. - Output valid JSON. In scales, min and max are numbers. Example: `{scales:{yAxes:[{ticks:{min:0,max:3}`\"\n },\n {\n \"role\": \"user\",\n \"content\": \"{{ $json.output.replaceAll('\\n', \" \").replaceAll('\"', \"\") }}\"\n }\n ],\n \"response_format\": {\n \"type\": \"json_schema\",\n \"json_schema\": {\n \"name\": \"chart_configuration\",\n \"description\": \"Configuration schema for Chart.js charts\",\n \"strict\": true,\n \"schema\": {\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\"bar\", \"line\", \"radar\", \"pie\", \"doughnut\", \"polarArea\", \"bubble\", \"scatter\", \"area\"]\n },\n \"data\": {\n \"type\": \"object\",\n \"properties\": {\n \"labels\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"string\"\n }\n },\n \"datasets\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"object\",\n \"properties\": {\n \"label\": {\n \"type\": [\"string\", \"null\"]\n },\n \"data\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"number\"\n }\n },\n \"backgroundColor\": {\n \"type\": [\"array\", \"null\"],\n \"items\": {\n \"type\": \"string\"\n }\n },\n \"borderColor\": {\n \"type\": [\"array\", \"null\"],\n \"items\": {\n \"type\": \"string\"\n }\n },\n \"borderWidth\": {\n \"type\": [\"number\", \"null\"]\n }\n },\n \"required\": [\"data\", \"label\", \"backgroundColor\", \"borderColor\", \"borderWidth\"],\n \"additionalProperties\": false\n }\n }\n },\n \"required\": [\"labels\", \"datasets\"],\n \"additionalProperties\": false\n },\n \"options\": {\n \"type\": \"object\",\n \"properties\": {\n \"scales\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"yAxes\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"ticks\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"max\": {\n \"type\": [\"number\", \"null\"]\n },\n \"min\": {\n \"type\": [\"number\", \"null\"]\n },\n \"stepSize\": {\n \"type\": [\"number\", \"null\"]\n },\n \"beginAtZero\": {\n \"type\": [\"boolean\", \"null\"]\n }\n },\n \"required\": [\"max\", \"min\", \"stepSize\", \"beginAtZero\"],\n \"additionalProperties\": false\n },\n \"stacked\": {\n \"type\": [\"boolean\", \"null\"]\n }\n },\n \"required\": [\"ticks\", \"stacked\"],\n \"additionalProperties\": false\n }},\n \"xAxes\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"stacked\": {\n \"type\": [\"boolean\", \"null\"]\n }\n },\n \"required\": [\"stacked\"],\n \"additionalProperties\": false\n }\n },\n \"required\": [\"yAxes\", \"xAxes\"],\n \"additionalProperties\": false\n },\n \"plugins\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"title\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"display\": {\n \"type\": [\"boolean\", \"null\"]\n },\n \"text\": {\n \"type\": [\"string\", \"null\"]\n }\n },\n \"required\": [\"display\", \"text\"],\n \"additionalProperties\": false\n },\n \"legend\": {\n \"type\": [\"object\", \"null\"],\n \"properties\": {\n \"display\": {\n \"type\": [\"boolean\", \"null\"]\n },\n \"position\": {\n \"type\": [\"string\", \"null\"],\n \"enum\": [\"top\", \"left\", \"bottom\", \"right\", null]\n }\n },\n \"required\": [\"display\", \"position\"],\n \"additionalProperties\": false\n }\n },\n \"required\": [\"title\", \"legend\"],\n \"additionalProperties\": false\n }\n },\n \"required\": [\"scales\", \"plugins\"],\n \"additionalProperties\": false\n }\n },\n \"required\": [\"type\", \"data\", \"options\"],\n \"additionalProperties\": false\n}\n}\n}\n}",
"sendBody": true,
"sendHeaders": true,
"specifyBody": "json",
"authentication": "predefinedCredentialType",
"headerParameters": {
"parameters": [
{
"name": "=Content-Type",
"value": "application/json"
}
]
},
"nodeCredentialType": "openAiApi"
},
"credentials": {
"openAiApi": {
"id": "WqzqjezKh8VtxdqA",
"name": "OpenAi account - Baptiste"
}
},
"typeVersion": 4.2
},
{
"id": "8016a925-7b31-4a49-b5e1-56cf9b5fa7b3",
"name": "Set response",
"type": "n8n-nodes-base.set",
"position": [
1860,
1140
],
"parameters": {
"options": [],
"assignments": {
"assignments": [
{
"id": "37512e1a-8376-4ba0-bdcd-34bb9329ae4b",
"name": "output",
"type": "string",
"value": "={{ \"https://quickchart.io/chart?width=200&c=\" + encodeURIComponent($json.choices[0].message.content) }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "9a2b8eca-5303-4eb0-8115-b0d81bfd1d7c",
"name": "When chat message received",
"type": "@n8n/n8n-nodes-langchain.chatTrigger",
"position": [
980,
380
],
"webhookId": "b0e681ae-e00d-450c-9300-2c2a4a0876df",
"parameters": {
"public": true,
"options": []
},
"typeVersion": 1.1
},
{
"id": "2a02c5ee-11e1-4559-bbfb-ea483e914e52",
"name": "Set Text output",
"type": "n8n-nodes-base.set",
"position": [
2080,
480
],
"parameters": {
"options": [],
"assignments": {
"assignments": [
{
"id": "4283fd50-c022-4eba-9142-b3e212a4536c",
"name": "output",
"type": "string",
"value": "={{ $('AI Agent').item.json.output }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "3b0f455a-ab1d-4dcd-ae97-708218c6c4b0",
"name": "Set Text + Chart output",
"type": "n8n-nodes-base.set",
"position": [
2320,
300
],
"parameters": {
"options": [],
"assignments": {
"assignments": [
{
"id": "63bab42a-9b9b-4756-88d2-f41cff9a1ded",
"name": "output",
"type": "string",
"value": "={{ $('AI Agent').item.json.output }}\n\n"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "29e2381a-7650-4e9a-a97f-26c7550ff7ba",
"name": "AI Agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
1240,
380
],
"parameters": {
"agent": "sqlAgent",
"options": {
"prefixPrompt": "You are an agent designed to interact with an SQL database.\nGiven an input question, create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.\nUnless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results using the LIMIT clause.\nYou can order the results by a relevant column to return the most interesting examples in the database.\nNever query for all the columns from a specific table, only ask for a the few relevant columns given the question.\nYou have access to tools for interacting with the database.\nOnly use the below tools. Only use the information returned by the below tools to construct your final answer.\nYou MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.\n\nTable name have to be enclosed in \"\", don't escape the \" with a \\.\nExample: SELECT DISTINCT cash_type FROM \"Sales\";\n\n\nDO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.\n\nIf the question does not seem related to the database, just return \"I don't know\" as the answer."
}
},
"credentials": {
"postgres": {
"id": "pdoWsjndlIgtlZYV",
"name": "Coffee Sales Postgres"
}
},
"typeVersion": 1.7
},
{
"id": "c5fdff53-29fa-474e-abcc-34fa4009250c",
"name": "Window Buffer Memory",
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"position": [
1400,
540
],
"parameters": {
"contextWindowLength": 4
},
"typeVersion": 1.2
},
{
"id": "4e630901-6c6c-4e86-af66-c6dfb9a92138",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"position": [
40,
160
],
"parameters": {
"color": "#FFF59D",
"width": 681,
"height": 765.2414586028488,
"content": "Configuration note: update with your credentials or endpoint."
},
"typeVersion": 1
},
{
"id": "36d7b17f-c7df-4a0a-8781-626dc1edddee",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
1260,
800
],
"parameters": {
"color": "#FFF59D",
"width": 769,
"height": 523,
"content": "Configuration note: update with your credentials or endpoint."
},
"typeVersion": 1
},
{
"id": "9ccea33b-c5d9-422e-a5b9-11efbc05ab1a",
"name": "Sticky Note2",
"type": "n8n-nodes-base.stickyNote",
"position": [
840,
160
],
"parameters": {
"color": "#FFF59D",
"width": 768,
"height": 546,
"content": "Configuration note: update with your credentials or endpoint."
},
"typeVersion": 1
},
{
"id": "d8bf0767-faf0-4030-b325-08315188adcb",
"name": "OpenAI Chat Model Classifier",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
1780,
540
],
"parameters": {
"options": {
"temperature": 0.2
}
},
"credentials": {
"openAiApi": {
"id": "WqzqjezKh8VtxdqA",
"name": "OpenAi account - Baptiste"
}
},
"typeVersion": 1
},
{
"id": "4bcd676f-44f3-4242-a5fd-7cf2098a3a64",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
1660,
160
],
"parameters": {
"color": "#FFF59D",
"width": 888,
"height": 546,
"content": "Configuration note: update with your credentials or endpoint."
},
"typeVersion": 1
},
{
"id": "256cb28b-0d83-4f6d-bb11-33745c9efa4a",
"name": "Text Classifier - Chart required?",
"type": "@n8n/n8n-nodes-langchain.textClassifier",
"position": [
1680,
380
],
"parameters": {
"options": [],
"inputText": "={{ $json.output }}",
"categories": {
"categories": [
{
"category": "chart_required",
"description": "if a chart can help the user understand the response. Only if there are multiple data to show"
},
{
"category": "chart_not_required",
"description": "if a chart doesn't help the user understand the response (e.g a single data point that doesn't require visualization)"
}
]
}
},
"typeVersion": 1
}
],
"connections": {
"AI Agent": {
"main": [
[
{
"node": "Text Classifier - Chart required?",
"type": "main",
"index": 0
}
]
]
},
"Execute Workflow": {
"main": [
[
{
"node": "Set Text + Chart output",
"type": "main",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Window Buffer Memory": {
"ai_memory": [
[
{
"node": "AI Agent",
"type": "ai_memory",
"index": 0
}
]
]
},
"When chat message received": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"OpenAI Chat Model Classifier": {
"ai_languageModel": [
[
{
"node": "Text Classifier - Chart required?",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Execute \"Generate a chart\" tool": {
"main": [
[
{
"node": "OpenAI - Generate Chart definition with Structured Output",
"type": "main",
"index": 0
}
]
]
},
"Text Classifier - Chart required?": {
"main": [
[
{
"node": "Execute Workflow",
"type": "main",
"index": 0
}
],
[
{
"node": "Set Text output",
"type": "main",
"index": 0
}
]
]
},
"OpenAI - Generate Chart definition with Structured Output": {
"main": [
[
{
"node": "Set response",
"type": "main",
"index": 0
}
]
]
}
},
"n8n_version": "1.5.0"
}
Requirements
n8n instance, API credentials for connected services
Tags
#n8n
#automation
#workflow