Skip to content

Example application for constructing and running an LLM-based LangChain SQL Agent based on GPT-4o mini that can dynamically query a database and invoke multiple visualization tools

Notifications You must be signed in to change notification settings

EliasK93/LangChain-SQL-Agent-for-dynamic-data-visualization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangChain SQL Agent for dynamic data visualization

Example application for the construction and inference of an LLM-based LangChain SQL Agent that can dynamically query a database and invoke multiple visualization tools. The language model used is OpenAIs GPT-4o mini.

For this, four datasets from the European Statistical Office (Eurostat) are loaded into a local SQL database that the LLM can query for up to 15 iterations per run. It can then use the results to independently call and output one of three basic visualizations functions based on Plotly.

The four datasets are all sourced from the Health determinants part of Eurostats public dataset API and include statistics on:

  • tobacco consumption by country of citizenship for the years 2014 and 2019 (Link)
  • body mass index (BMI) by country of citizenship for the years 2014 and 2019 (Link)
  • physical exercise by country of citizenship for the years 2014 and 2019 (Link)
  • alcohol consumption by country of citizenship for the years 2014 and 2019 (Link)

The LLM agent can use the following three tool functions to visualize the results (see agent_tools.py):

  • output_table(): output 2D table contents as a pretty table using Plotly table viewer
  • output_bar_plot(): output a simple bar plot
  • output_time_series_plot(): output one or multiple line plots along one main time axis

Example Results

User Input Agent Output
Show me the change in the percentage points of daily smokers between 2014 and 2019 for Germany, Denmark, Poland and Austria in a pretty table (one row per country). Plotly Figure:

imgs/result-query1.png
Main SQL query used by the LLM:

SELECT "country (ISO-639-1)", "daily smoker"
FROM smoking_of_tobacco_products
WHERE year IN ('2014', '2019')
AND "country (ISO-639-1)" IN ('DE', 'DK', 'PL', 'AT');
Plot the percentage of people who are obese in Germany, Denmark, Estonia, Finland, Poland and Austria as a time series (one series per country). Plotly Figure:

imgs/result-query2.png
Main SQL query used by the LLM:

SELECT year, "country (ISO-639-1)", obese
FROM body_mass_index
WHERE "country (ISO-639-1)" IN ('DE', 'DK', 'EE', 'FI', 'PL', 'AT')
ORDER BY year;
Calculate the minimum, average and maximum percentages of people in 2019 who do only aerobic, only muscle-strengthening and both aerobic and muscle-strengthening exercise and show the result as a bar plot (nine bars overall). Plotly Figure:

imgs/result-query3.png
Main SQL query used by the LLM:

SELECT
    MIN(aerobic) AS min_aerobic,
    AVG(aerobic) AS avg_aerobic,
    MAX(aerobic) AS max_aerobic,
    MIN("muscle-strengthening") AS min_muscle_strengthening,
    AVG("muscle-strengthening") AS avg_muscle_strengthening,
    MAX("muscle-strengthening") AS max_muscle_strengthening,
    MIN("aerobic and muscle-strengthening") AS min_aerobic_and_strengthening,
    AVG("aerobic and muscle-strengthening") AS avg_aerobic_and_strengthening,
    MAX("aerobic and muscle-strengthening") AS max_aerobic_and_strengthening
FROM health_enhancing_physical_activity
WHERE year = '2019';
What are the five countries with most people who in 2014 stated that they have not drunk alcohol in the last year? Plot the result as a bar plot. Plotly Figure:

imgs/result-query4.png
Main SQL query used by the LLM:

SELECT "country (ISO-639-1)", "never or not in the last 12 months"
FROM alcohol_consumption
WHERE year = '2014'
ORDER BY "never or not in the last 12 months" DESC
LIMIT 5;

Requirements

- Python >= 3.10
- pip
  • langchain
  • langchain-community
  • langchain-openai
  • sqlalchemy
  • pydantic
  • pandas
  • plotly

About

Example application for constructing and running an LLM-based LangChain SQL Agent based on GPT-4o mini that can dynamically query a database and invoke multiple visualization tools

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages