SQL Agent

This tutorial will guide you through building an intelligent SQL Agent that can interact with databases, generate SQL queries, validate them, execute them, and self-correct when errors occur.

Overview

The SQL Agent flow implements a robust database interaction system that:

  1. Retrieves database schema information

  2. Generates SQL queries based on user questions

  3. Validates generated queries for common mistakes

  4. Executes queries against the database

  5. Checks results for errors and self-corrects when needed

  6. Provides natural language responses based on query results

Step 1: Setting Up the Start Node

Begin by adding a Start node to your canvas. This serves as the entry point for your SQL agent.

Configuration:

  • Input Type: Select "Chat Input" to accept user questions

  • Flow State: Add a state variable with key "sqlQuery" and empty value

The Start node initializes the flow state with an empty sqlQuery variable that will store the generated SQL query throughout the process.

Step 2: Retrieving Database Schema

Add a Custom Function node and connect it to the Start node.

Configuration:

  • Javascript Function: This is an example function that connects to your database and retrieves the complete schema including table structures, column definitions, and sample data.

Step 3: Generating SQL Queries

Add an LLM node connected to the "Get DB Schema" node.

Configuration:

  • Messages: Add a system message:

  • JSON Structured Output: Here we instruct the model only return structured output, to prevent LLM from including other text other than the SQL query.

    • Key: "sql_query"

    • Type: "string"

    • Description: "SQL query"

  • Update Flow State: Set key "sqlQuery" with value {{ output.sql_query }}

This node transforms the user's natural language question into a structured SQL query using the database schema information.

Step 4: Validating SQL Query Syntax

Add a Condition Agent node connected to the "Generate SQL Query" LLM.

Configuration:

  • Instructions:

  • Input: {{ $flow.state.sqlQuery }}

  • Scenarios:

    • Scenario 1: "SQL query is correct and does not contains mistakes"

    • Scenario 2: "SQL query contains mistakes"

This validation step catches common SQL errors before execution.

Step 5: Handling Query Regeneration (Error Path)

For incorrect queries (output 1) from previous Condition Agent node, add a Loop node.

Configuration:

  • Loop Back To: "Generate SQL Query"

  • Max Loop Count: Set to 5

This creates a feedback loop that allows the system to retry query generation when validation fails.

Step 6: Executing Valid SQL Queries

For correct queries (output 0), add a Custom Function node.

Configuration:

  • Input Variables: Here we pass in the generated SQL query as variable to be used in Function.

    • Variable Name: "sqlQuery"

    • Variable Value: {{ $flow.state.sqlQuery }}

  • Javascript Function: This function executes the validated SQL query against the database and formats the results.

Step 7: Checking Query Execution Results

Add a Condition Agent node connected to the "Run SQL Query" function.

Configuration:

  • Instructions: "You are a SQL expert. Check if the query result is correct or contains error."

  • Input: {{ customFunctionAgentflow_1 }}

  • Scenarios:

    • Scenario 1: "Result is correct and does not contains error"

    • Scenario 2: "Result query contains error"

This step validates the execution results and determines if further correction is needed.

Step 8: Generating Final Response (Success Path)

For successful results (output 0 from Condition Agent), add an LLM node.

Configuration:

  • Input Message: {{ customFunctionAgentflow_1 }}

This node generates a natural language response based on the successful query results.

Step 9: Handling Query Regeneration (Runtime Error Path)

For failed executions (output 1 from Condition Agent), add an LLM node.

Configuration:

  • Messages: Add the same system message as Step 3

  • Input Message:

  • JSON Structured Output: Same as Step 3

  • Update Flow State: Set key "sqlQuery" with value {{ output.sql_query }}

This node analyzes runtime errors and generates corrected SQL queries.

Step 10: Adding the Second Loop Back

Add a Loop node connected to the "Regenerate SQL Query" LLM.

Configuration:

  • Loop Back To: "Check SQL Query"

  • Max Loop Count: Set to 5

This creates a second feedback loop for runtime error correction.


Complete Flow Structure

74KB
Open

Summary

  1. Start → Get DB Schema

  2. Get DB Schema → Generate SQL Query

  3. Generate SQL Query → Check SQL Query

  4. Check SQL Query (Correct) → Run SQL Query

  5. Check SQL Query (Incorrect) → Regenerate Query (Loop back)

  6. Run SQL Query → Check Result

  7. Check Result (Success) → Return Response

  8. Check Result (Error) → Regenerate SQL Query

  9. Regenerate SQL Query → Recheck SQL Query (Loop back)


Testing Your SQL Agent

Test your agent with various types of database questions:

  • Simple queries: "Show me all customers"

  • Complex queries: "What are the top 5 products by sales?"

  • Analytical queries: "Calculate the average order value by month"

This SQL Agent flow provides a robust, self-correcting system for database interactions that can handle SQL queries in natural language.

Last updated