Microsoft Dev Blogs

NL2SQL with LangChain and Azure SQL Database

thumbnail

Table of Contents

Setting up Python Environment

In this section, the necessary libraries for the LangChain SQL Database Toolkit integration are set up. Environment variables from the .env file are also loaded for later use.

Creating and Testing Database Connection

This section involves creating a database connection engine and initializing the LangChain SQL Database Toolkit integration by adding it to a db variable.

Creating LangChain SQL Agent

Here, a LangChain SQL agent using OpenAI as the Language Model and the database connection is created using the LangChain SQL Database Toolkit.

Executing NL2SQL Prompt

Finally, the NL2SQL prompt is executed to interact with the Azure SQL Database, where a sample query to count the rows in a test table is performed.

You have successfully created a LangChain application with Azure SQL Database using NL2SQL agents.