Overview
In this tutorial, you will learn how to build an agent that can answer questions about a SQL database using LangChain agents. At a high level, the agent will:1
Fetch the available tables and schemas from the database
2
Decide which tables are relevant to the question
3
Fetch the schemas for the relevant tables
4
Generate a query based on the question and information from the schemas
5
Double-check the query for common mistakes using an LLM
6
Execute the query and return the results
7
Correct mistakes surfaced by the database engine until the query is successful
8
Formulate a response based on the results
Building Q&A systems of SQL databases requires executing model-generated SQL queries. There are inherent risks in doing this. Make sure that your database connection permissions are always scoped as narrowly as possible for your agent’s needs. This will mitigate, though not eliminate, the risks of building a model-driven system.
Concepts
We will cover the following concepts:- Tools for reading from SQL databases
- LangChain agents
- Human-in-the-loop processes
Setup
Installation
LangSmith
Set up LangSmith to inspect what is happening inside your chain or agent. Then set the following environment variables:1. Select an LLM
Select a model that supports tool-calling:- OpenAI
- Anthropic
- Azure
- Google Gemini
- AWS Bedrock
2. Configure the database
You will be creating a SQLite database for this tutorial. SQLite is a lightweight database that is easy to set up and use. We will be loading thechinook
database, which is a sample database that represents a digital media store.
For convenience, we have hosted the database (Chinook.db
) on a public GCS bucket.
langchain_community
package to interact with the database. The wrapper provides a simple interface to execute SQL queries and fetch results:
3. Add tools for database interactions
Use theSQLDatabase
wrapper available in the langchain_community
package to interact with the database. The wrapper provides a simple interface to execute SQL queries and fetch results:
5. Use create_agent
Use create_agent
to build a ReAct agent with minimal code. The agent will interpret the request and generate a SQL command, which the tools will execute. If the command has an error, the error message is returned to the model. The model can then examine the original request and the new error message and generate a new command. This can continue until the LLM generates the command successfully or reaches an end count. This pattern of providing a model with feedback - error messages in this case - is very powerful.
Initialize the agent with a descriptive system prompt to customize its behavior:
6. Run the agent
Run the agent on a sample query and observe its behavior:You can inspect all aspects of the above run, including steps taken, tools invoked, what prompts were seen by the LLM, and more in the LangSmith trace.
(Optional) Use Studio
Studio provides a “client side” loop as well as memory so you can run this as a chat interface and query the database. You can ask questions like “Tell me the scheme of the database” or “Show me the invoices for the 5 top customers”. You will see the SQL command that is generated and the resulting output. The details of how to get that started are below.Run your agent in Studio
Run your agent in Studio
In addition to the previously mentioned packages, you will need to:In directory you will run in, you will need a Create a file
langgraph.json
file with the following contents:sql_agent.py
and insert this:6. Implement human-in-the-loop review
It can be prudent to check the agent’s SQL queries before they are executed for any unintended actions or inefficiencies. LangChain agents feature support for built-in human-in-the-loop middleware to add oversight to agent tool calls. Let’s configure the agent to pause for human review on calling thesql_db_query
tool:
We’ve added a checkpointer to our agent to allow execution to be paused and resumed. See the human-in-the-loop guide for detalis on this as well as available middleware configurations.
sql_db_query
tool: