Integrating Azure OpenAI with a Database Connection in Python (Azure OpenAI Chatbot)

In recent years, the integration of AI models into applications has become increasingly accessible and powerful. Azure OpenAI offers an excellent platform for leveraging the capabilities of GPT-4 and other AI models to build intelligent applications. In this blog post, we’ll walk through how to implement Azure OpenAI in a Python application and connect it to a database to create a robust and dynamic system.

Prerequisites

Before we get started, ensure you have the following prerequisites:

  1. An Azure account with access to Azure OpenAI.
  2. A database to connect.
  3. Python installed on your system.
  4. Required Python libraries: langchain, pyodbc, and sqlalchemy.

Step 1: Setting Up Azure OpenAI

First, you need to set up Azure OpenAI in your Azure portal:

Create an Azure OpenAI resource:

  • Navigate to the Azure portal.
  • Create a new Azure OpenAI resource.
  • Note down the API key and endpoint provided.

Step 2: Setting Up the Database

 conn_str = (
            r'DRIVER={ODBC Driver 17 for SQL Server};'
            f'SERVER={server};'
            f'DATABASE={database};'
            f'UID={user_name};'
            f'PWD={password};'
        )
 
 params = urllib.parse.quote_plus(conn_str)

 connection_uri = "mssql+pyodbc:///?odbc_connect=%s" % params

 engine = create_engine(connection_uri)

 db = SQLDatabase(engine)


                                                )

Step 3: Connecting to Azure OpenAI


 llm = AzureChatOpenAI(
            openai_api_version=os.environ.get("AZURE_OPENAI_API_VERSION"),
            temperature=0.0,
            max_tokens=2000,
            azure_endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT"),
            openai_api_key= os.environ.get("AZURE_OPENAI_API_KEY"),
            openai_api_type="azure",
            deployment_name= os.environ.get("AZURE_OPENAI_DEPLOYMENT"),
            model="gpt-4"
        )

 sql_toolkit = SQLDatabaseToolkit(db=db, llm=llm)

 sql_toolkit.get_tools()

 sqldb_agent = create_sql_agent(llm=llm, toolkit=sql_toolkit, agent_type =     AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, max_execution_time=200, max_iterations=500

sqldb_agent.run("QUERY")

Leave a Comment

Your email address will not be published. Required fields are marked *