TextToSQL
Author: Jaehun Choi
Peer Review: Dooil Kwak, Ilgyun Jeong
Proofread : Juni Lee
This is a part of LangChain Open Tutorial
Overview
This tutorial provides a feature for generating SQL query statements based on arbitrary database information. While it does not cover extracting database information directly, it may include details about column information and descriptions for specific tables. Using OpenAI’s GPT models (e.g., gpt-4o) and prompt templates, the tutorial demonstrates how to generate SQL queries.
Features
Database Information : Introduces the format of database information required for generating SQL queries.
TextToSQL : Generates customized SQL queries based on the provided database information.
Evaluation : Conducts a lightweight evaluation of the generated SQL queries.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.
[Note] This is not necessary if you've already set the required API keys in previous steps.
Database Information
Descriptions of database information can be utilized in two main forms:
Providing raw table or column information without modification.
Providing table or column information with descriptions included.
This tutorial will proceed based on the following example table:
Database Name: CompanyDB
Column Information for the 'employees' table (Employee Information):
Text to SQL
Customized SQL queries are generated based on the two types of database schema information mentioned above.
Additionally, cases involving the generation of SQL queries referencing two or more tables are also introduced.
Evaluation
As detailed in the paper "Enhancing Text-to-SQL Translation for Financial System Design" referenced in the References section, SQL evaluation cannot be assessed using a single metric alone. In this tutorial, we utilize code excerpted from the SQAM GitHub repository for evaluation purposes, selected from among various evaluation metrics. For more information, please refer to the original paper linked in the References section.
[Note] The Structural Query Alignment Metric (SQAM) is a Python package that provides functions to compare SQL queries based on their syntax and structure. Given a query and a ground truth query, the package computes an accuracy score that reflects the degree of similarity between the two queries. The accuracy score is based on the percentage of matching query subitems (e.g., select columns, where conditions, order by clauses) weighted by their importance in the overall query structure.
The evaluation will proceed in the following order.
Comparison of Query Components : Divide the SQL queries into major components such as SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT. Extract detailed elements within each component (e.g., selected columns, conditions, sorting criteria, etc.).
Weight Assignment to Components : Assign weights to each component based on its importance in the query structure. For example, SELECT and WHERE may have relatively higher weights, while FROM and ORDER BY may have lower weights.
Accuracy Calculation : Compare the components of the query being evaluated with the reference query, calculating how many detailed elements (sub-components) match between them. Then compute the ratio of matching elements to the total compared elements and return the accuracy as a percentage (%).
Evaluation Case #1 : This refers to cases where the same columns and conditions are used, producing identical execution results, but the query expressions differ. A key characteristic is the inclusion of aliases, which do not affect the evaluation.
Evaluation Case #2 : This refers to cases where the same columns are used but with different conditions, resulting in variations in execution outcomes. Due to the nature of the evaluation algorithm, differences in the WHERE clause, which pertains to conditions within SELECT, FROM, and WHERE, lead to an inconsistency rate of approximately 33.3%.
Last updated