How to integrate OpenAI and LangChain into Azure SQL through NLP

How to integrate OpenAI and LangChain into Azure SQL through NLP
Posted :

In today’s data-driven world, efficient access to database information is crucial. Traditional methods of querying databases with complex SQL queries can be challenging for non-technical users. However, generative AI and cloud computing advancements have revolutionized how we interact with databases. This blog explores leveraging generative AI, specifically Azure Open AI and LangChain, to transform the search experience on an Azure SQL Database. By integrating generative AI with the database, users can perform natural language queries without writing SQL queries.

Imagine users entering everyday language queries into a search box and receiving accurate results from the database. This approach eliminates the SQL barrier, making data retrieval intuitive and accessible. By combining Azure Open AI’s language model with LangChain’s integration, we will create a solution that allows you to integrate a single search box into your applications for querying the database. This blog will delve into implementing generative AI on Azure SQL Database. From defining the language model to initializing the agent, we will guide you in creating a solution that empowers you to search and retrieve information effortlessly.

By the end, you will understand how to leverage generative AI to streamline search on an Azure SQL Database, providing a user-friendly experience for developers and end-users. Let’s unlock the potential of generative AI in database interactions.

Use case explanation: Effortless database search

The use case for this solution is to simplify the process of searching and retrieving information from a SQL database using natural language queries. Instead of writing complex SQL queries or stored procedures, users can interact with the database by simply typing their queries into a search box. This makes the search process more intuitive and accessible, even for users without SQL expertise. The combination of generative AI and SQL database integration streamlines the data retrieval process and enhances the user experience.

Tools and frameworks

For this project, we will utilize two key tools: Azure Open AI and LangChain.

Azure Open AI provides a powerful language model that enables natural language processing (NLP) capabilities. It allows us to generate human-like responses to queries, making it ideal for understanding and responding to user input.

LangChain is a library that simplifies the integration of AI models into various applications. It provides a set of tools and APIs that help in building conversational agents and executing AI-powered actions.

Technical steps

Let’s dive into the technical steps involved in implementing generative AI on a SQL database using Azure Open AI and LangChain.

1. Import libraries

First, we need to import the necessary libraries for our project. These libraries include the LangChain tools, agent executor, SQL database toolkit, and SQL database library.



import os
from langchain.llms import AzureOpenAI
from langchain.agents import load_tools, AgentExecutor, initialize_agent, AgentType
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase

2. Define language model (LLM)

Next, we define the language model (LLM) using Azure Open AI. We set up the required environment variables and instantiate the AzureOpenAI class.



os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_VERSION"] = "2022-12-01"
os.environ["OPENAI_API_BASE"] = "your_azure_api_base_url"
os.environ["OPENAI_API_KEY"] = "your_azure_api_key"

llm = AzureOpenAI(
    deployment_name="your_azure_deployment_name",
    model_name="your_azure_model_name"
)

3. Define SQL database tool

We need to define the SQL database tool, which involves providing the necessary connection details such as server, database, username, and password. Below example points to the Aure SQL Databases but if you want to use MS SQL Databases then change the conn_str and remove “database.windows.net:1433”.



server = 'your_server_name'
database = 'your_database_name'
username = 'your_username'
password = 'your_password'

# Create the connection string
conn_str = f"mssql+pymssql://{username}:{password}@{server}.database.windows.net:1433/{database}"

db = SQLDatabase.from_uri(conn_str)

4. Combine tools

To perform various actions and interact with different APIs, we combine the SQL database tool with other necessary tools. In this example, we also include the serpapi and human tools. To integrate this with your actual program, you don’t need human tools but if you are using code editor/console and want agent to ask few inputs, you can use human tool here. Sometimes, your queries might need external source of information, which is the reason, we have used serpapi.



os.environ["SERPAPI_API_KEY"] = "your_serpapi_api_key"

sqltoolkit = SQLDatabaseToolkit(db=db, llm=llm)

tools = load_tools(["serpapi", "human"])
tools += sqltoolkit.get_tools()

5. Initialize agent with tools and LLM

We initialize the agent using the tools and language model defined above. The agent type is set to ZERO_SHOT_REACT_DESCRIPTION, which allows us to generate responses based on user queries.



agent = initialize_agent(
    tools=tools,
    llm=llm,
    type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

6. Start asking questions in natural language

Finally, we can start interacting with the agent by asking questions in natural language. The agent will generate responses based on the input queries.



agent.run("Give me active and inactive companies in total")

Generative AI on Azure SQL Database

Conclusion

In this blog post, we explored how to implement generative AI on a SQL database using Azure Open AI and LangChain. Combining the power of Azure Open AI’s language model with LangChain’s tools and SQL database integration, we created a solution allowing users to search the database using natural language queries. This approach simplifies the search process and enhances the user experience by eliminating the need to write SQL queries or procedures. With the code provided, you can integrate this functionality into your applications and provide your users with a seamless and intuitive search experience.

Author Bio:

Jay Parikh is an accomplished technology leader with over 17 years of experience in software engineering and management. Currently serving as the Associate Vice President of Software Engineering at Softweb Solutions, he is responsible for driving innovation, managing cross-functional teams, and delivering high-quality software solutions.

Need Help?
We are here for you

Step into a new land of opportunities and unearth the benefits of digital transformation.