{ "cells": [ { "cell_type": "markdown", "id": "cb71c4a7-9f99-4f08-be34-b3ba81ac075f", "metadata": {}, "source": [ "# Build a SQL agent\n", "\n", "In this tutorial, we will walk through how to build an agent that can answer questions about a SQL database.\n", "\n", "At a high level, the agent will:\n", "\n", "1. Fetch the available tables from the database\n", "2. Decide which tables are relevant to the question\n", "3. Fetch the schemas for the relevant tables\n", "4. Generate a query based on the question and information from the schemas\n", "5. Double-check the query for common mistakes using an LLM\n", "6. Execute the query and return the results\n", "7. Correct mistakes surfaced by the database engine until the query is successful\n", "8. Formulate a response based on the results\n", "\n", "!!! warning \"Security note\"\n", "\n", " 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.\n", "\n", "## 1. Setup\n", "\n", "Let's first install some dependencies. This tutorial uses SQL database and tool abstractions from [langchain-community](https://python.langchain.com/docs/concepts/architecture/#langchain-community). We will also require a LangChain [chat model](https://python.langchain.com/docs/concepts/chat_models/)." ] }, { "cell_type": "code", "execution_count": null, "id": "1b613286-41c3-4ca5-9514-36aac0121a5a", "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install -U langgraph langchain_community \"langchain[openai]\"" ] }, { "cell_type": "markdown", "id": "f5258ad0-39e1-418e-bddb-56e9aab1b43a", "metadata": {}, "source": [ "
Set up LangSmith for LangGraph development
\n", "\n", " Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here. \n", "
\n", "