My first document

Page 1

How to build and integrate generative AI into Azure SQL

Need Help?

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

FirstName

AUTHOR : Jay Parikh POSTED : May 24th,2023

Generative AI

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

LastName Phone BusinessEmail Description Submit

We are here for you
Home Insights Blogs Portfolio Services  Microsoft  AWS  IoT  About Us Insights Contact Us

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 AIpowered 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 importos os

from fromlangchainlangchainllmsllmsimportimportAzureOpenAI AzureOpenAI from fromlangchainlangchainagentsagentsimportimportload_tools load_tools,,AgentExecutor AgentExecutor,,initialize_agen initialize_agen from fromlangchainlangchainagentsagentsagent_toolkitsagent_toolkitsimportimportSQLDatabaseToolkit SQLDatabaseToolkit from fromlangchainlangchain.sql_databasesql_databaseimportimportSQLDatabase 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 osenvironenviron[["OPENAI_API_TYPE" "OPENAI_API_TYPE"]]=="azure" "azure"

os osenvironenviron[["OPENAI_API_VERSION" "OPENAI_API_VERSION"]]=="2022-12-01" "2022-12-01"

os os.environenviron[["OPENAI_API_BASE" "OPENAI_API_BASE"]]=="your_azure_api_base_url" "your_azure_api_base_url"

os osenvironenviron[["OPENAI_API_KEY" "OPENAI_API_KEY"]]=="your_azure_api_key" "your_azure_api_key"

llm llm==AzureOpenAI AzureOpenAI( ( deployment_name deployment_name=="your_azure_deployment_name" "your_azure_deployment_name", , model_name model_name=="your_azure_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 server=='your_server_name' 'your_server_name'

database database=='your_database_name' 'your_database_name'

username username=='your_username' 'your_username'

password password=='your_password' 'your_password'

#Createtheconnectionstring #Createtheconnectionstring conn_str conn_str==ff"mssql+pymssql://{username}:{password}@{server}.database.wi "mssql+pymssql://{username}:{password}@{server}.database.wi

db db==SQLDatabaseSQLDatabasefrom_urifrom_uri((conn_strconn_str) )

4 Combine tools

Portfolio Services  Microsoft  AWS  IoT  About Us Insights Contact Us

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 osenvironenviron[["SERPAPI_API_KEY" "SERPAPI_API_KEY"]]=="your_serpapi_api_key" "your_serpapi_api_key"

sqltoolkit sqltoolkit==SQLDatabaseToolkit SQLDatabaseToolkit((dbdb==dbdb,,llmllm==llmllm) )

tools tools==load_tools load_tools(([["serpapi""serpapi",,"human""human"]]) ) tools tools+=+=sqltoolkitsqltoolkitget_toolsget_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 agent==initialize_agent initialize_agent( ( tools tools==toolstools, , llm llm==llmllm, , type type==AgentTypeAgentTypeZERO_SHOT_REACT_DESCRIPTION ZERO_SHOT_REACT_DESCRIPTION, , verbose verbose==True 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 agentrunrun(("Givemeactiveandinactivecompaniesintotal" "Givemeactiveandinactivecompaniesintotal") )

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

Portfolio Services  Microsoft  AWS  IoT  About Us Insights Contact Us

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 crossfunctional teams, and delivering high-quality software solutions

Company

About Us

Insights

Testimonials

Careers Contact Us

Services

AI Consulting DataAnalytics

Cloud Services Full Stack

Dallas

7950 Legacy Drive,Ste 250, Plano,TX 75024

Chicago

India

5 & 6,Garden View Corporate House, Bodakdev,Ahmedabad,GJ 380054

Call Us

CSR Policy

Cookie Policy

Privacy Policy

Legal and Copyright Notices

Power Platform

XR

Mobile Development Sitecore

Legacy Modernization

2531 Technology Drive,Ste 312, Elgin,IL 60124

866-345-7638 info@softwebsolutions com ©

Solutions Inc (An Avnet Company)All rights reserved Portfolio Services  Microsoft  AWS  IoT  About Us Insights Contact Us
2023 Softweb

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.