Automating data analytics with ChatGPT

James Nguyen4 Jul 2023

Problem

Business users want to ask a database complex questions in plain language and get answers with charts. Language models make arithmetic errors, so letting them compute numbers is unreliable. Real schemas are too large for a prompt, and company-specific terms are unknown to the model. Today an analyst has to write every query by hand.

Example from the article: a user asks a business question about a SQLite or SQL Server database and gets a Plotly chart or table in a Streamlit page.

Solution in brief

The model plans and writes code; tools do the computing. Two ReAct agents split the work. A data engineer agent finds tables, reads schemas, writes SQL and saves a dataset. A data scientist agent requests data, analyzes it in Python and shows the result.

Questions this case answers
  • How to let non-technical users get computed answers and visualizations from a structured database without the language model doing the arithmetic itself
  • How to give a language-model agent a large schema without exceeding its context window
  • How to let business users ask analytical questions of a SQL database and get answers with charts?
  • How to make an LLM do reliable quantitative analysis when it is weak at arithmetic?

Tools and methods

  • ReAct agent — a Thought, Action, Observation loop that lets the agent revise its plan after each step
  • Chain of thought — breaks a complex question into steps
  • Multi-agent split — a data engineer agent (SQL) and a data scientist agent (Python, charts), so each prompt stays simple
  • Few-shot prompting — a template with sample steps in the prompt for each agent
  • ChatGPT — the "brain" that plans and writes SQL and Python
  • Streamlit — UI, display of results and session memory shared between agents
  • Plotly, pandas — visualization and analysis in the data scientist's code
  • SQLite, SQL Server — supported data sources
Continue reading — it's free

Google or a one-time email code. No password.