QueryGPT – Natural Language to SQL Using Generative AI

Jeffrey Johnson, Callie Busch, Abhi Khune, Pradeep Chakka19 Sep 2024

Problem

Analysts and operations staff at Uber write SQL against a warehouse that serves about 1.2 million interactive queries a month, and the Operations organization produces about 36% of them. Writing a query takes roughly 10 minutes, much of it spent finding datasets in the data dictionary. A first LLM prototype worked on a handful of tables, but accuracy dropped as more were added.

Example from the article: the question "Find the number of trips completed yesterday in Seattle" does not retrieve the right schemas when it is matched directly against table definitions and SQL text.

Solution in brief

QueryGPT puts several narrow LLM calls in front of the final SQL generation. An intent agent maps the question to a business domain ("workspace"), a table agent picks tables and lets the user confirm them, and a column prune agent shrinks the schemas. A hand-labelled golden set scores each stage separately, so a change to one agent can be judged on its own.

Questions this case answers
  • How to generate correct SQL from free-text questions when the warehouse is too large to fit into one prompt and business terms are internal
  • How to measure an LLM data-query assistant stage by stage when there is no single correct answer and results are non-deterministic
  • How to generate accurate SQL from natural-language questions over a warehouse with hundreds of thousands of datasets?
  • How to keep large table schemas inside an LLM token limit without losing the columns a query needs?

Tools and methods

  • Retrieval-augmented generation (RAG) — fetches schemas and sample SQL for the prompt
  • Few-shot prompting — sample question-to-SQL pairs per domain
  • Intent, table and column prune agents — LLM calls that narrow the context
  • Golden set — logged questions with verified intent, tables and SQL
  • LLM-as-a-judge — scores similarity of generated and golden SQL
  • GPT-4 Turbo via Azure OpenAI — generation model
Continue reading — it's free

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