09. Agent interacting with SQL database

In this tutorial Agent to answer questions about SQL database Introducing how to build step by step.

The flow of agents running SQL queries is:

  1. Identify database schemas : Get a list of available tables.

  2. Select related table : Select the table associated with the question.

  3. DDL lookup : Get the schema definition (DDL) of the selected table.

  4. Query generation : Create SQL queries based on questions and DDL information.

  5. Query check : Use LLM to review common errors and improve queries.

  6. Query execution and error handling : Run the query on the database engine, fix it in case of an error, and successfully perform the query.

  7. Create response : Provides final answer based on query results.

Main contents

  • database : SQLite database settings and chinook Sample database load

  • Utility function : Define utility functions for agent implementation

  • Tool definition : Define tools to interact with the database

  • Define walkflow : Define the agent's workflow (graph)

  • Graph visualization : Defined graph visualization

  • Agent execution : Agent execution and results verification

  • evaluation : Agent evaluation and performance comparison

Preferences

First, install the required package and set the API key.

Copy

Copy

Copy

Copy

Copy

Copy

Database settings

This tutorial creates an SQLite database. SQLite is a lightweight database that is easy to set up and use.

In this tutorial, it is a sample database chinook The database will be loaded, which is a sample database representing the digital media store.

For more information about the database here You can check in. First, use it for practice chinook Download the database.

Copy

Copy

Next downloadable chinook Using the database SQLDatabase Generate tools and sample queries "SELECT * FROM Artist LIMIT 5;" Run.

Copy

Copy

Copy

If error occurs, it returns an error message.

db_query_tool In the case of, run the query against the database and return the results.

next db_query_tool Defines.

Copy

Copy

Below list_tables_tool and get_schema_tool Example of execution for.

Copy

Copy

  • function : Validation before Query runs

  • Inspection items :

  • Using NULL values and NOT IN

  • UNION vs UNION ALL adequacy

  • BETWEEN range setting

  • Data type match

  • Identifier citation adequacy

  • Number of Function arguments

  • Data type casting

  • Join column accuracy

  • Features : GPT-4 model based verification

QuerySQLCheckerTool

  • function : Database View my table list

ListSQLDatabaseTool

  • function : Table schema and sample data lookup

  • Input : List of tables separated by comas

  • Example of use : table1, table2, table3

  • caution : sql_db_list_tables Pre-check availability as table

InfoSQLDatabaseTool

  • function : Execute SQL query and return results

  • Input : Accurate SQL query

  • Output : Database results or error message

  • Error processing :

  • Query error generation and retry

  • Unknown column Error sql_db_schema Check accurate table fields with

QuerySQLDataBaseTool

The following is intended to interact with SQL database. SQLDatabaseToolkit List of tools.

Database query related tools

  • CREATE : Create database objects.

  • Yes: CREATE TABLE users (id INT, name VARCHAR(100));

  • ALTER : Modifies existing database objects.

  • Yes: ALTER TABLE users ADD COLUMN email VARCHAR(100);

  • DROP : Delete database objects.

  • Yes: DROP TABLE users;

Main DDL instructions

  • DDL (Data Definition Language, Data Definition Language ) Refers to the structure of the database and the SQL instructions used to define or modify the schema. Mainly used to create, modify, and delete database objects such as tables, indexes, views, and schemas.

Reference

  1. list_tables_tool : Get the available tables from the database.

  2. get_schema_tool : Get the DDL of the table.

  3. db_query_tool : Run the query and get the result or return an error message if the query fails.

Define some tools to help agents interact with the database.

SQL Query Execution Tool

Copy

Copy

When running normally

Copy

Copy

Example if an error occurs

Copy

Copy

SQL Query Checker

Next, we will use LLM to check for common mistakes in SQL queries.

This is not a tool, strictly speaking, but will be added as a node to the subsequent workflow.

Copy

Blow and call the wrong query to make sure the results are well corrected.

Reference

  • LIMIT instead LIMITS I used to blow the query.

Copy

Copy

The results were well corrected.

Graph definition

Defines the agent's workflow.

Agent first list_tables_tool After forcibly calling to get the available table from the database, follow the steps mentioned at the beginning of the tutorial.

Copy

Copy

Copy

Copy

Copy

Copy

Copy

SQL Agent evaluation using LangSmith Evaluator

Now evaluate the SQL query response of the Agent you created. Generate a data set for evaluation to evaluate query responses.

Next, define the evaluator and proceed with the evaluation.

The evaluators utilized at this time are LLM-as-judge, and the prompts used utilize the prompts provided by the default hub.

However, it is recommended to tune and use each prompt for a more accurate evaluation.

Copy

The following defines the function to predict the SQL query response of the agent we created.

Copy

Defines the prompt and evaluator (LLM-as-judge) to evaluate SQL query responses.

Copy

Now, conduct an assessment and confirm the results.

Copy

Copy

Copy

Copy

The evaluation results can be checked at the URL created.

Last updated