{ "cells": [ { "cell_type": "markdown", "id": "9f853e403eabd4f8", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "# An agent for interacting with a SQL database\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", "1. Fetch the available tables from the database\n", "2. Decide which tables are relevant to the question\n", "3. Fetch the DDL for the relevant tables\n", "4. Generate a query based on the question and information from the DDL\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", "The end-to-end workflow will look something like below:\n", "\n", "" ] }, { "cell_type": "markdown", "id": "b5a87813ffe7e4d2", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "## Setup\n", "\n", "First let's install our required packages and set our API keys" ] }, { "cell_type": "code", "execution_count": null, "id": "4a4be247", "metadata": {}, "outputs": [], "source": [ "%capture --no-stderr\n", "%pip install -U langgraph langchain_openai langchain_community" ] }, { "cell_type": "code", "execution_count": 1, "id": "6c05a600f1afb5b6", "metadata": { "ExecuteTime": { "end_time": "2024-06-12T21:24:00.532147Z", "start_time": "2024-06-12T21:24:00.526043Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "\n", "def _set_env(key: str):\n", " if key not in os.environ:\n", " os.environ[key] = getpass.getpass(f\"{key}:\")\n", "\n", "\n", "_set_env(\"OPENAI_API_KEY\")" ] }, { "cell_type": "markdown", "id": "80559636", "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", "