This project is a web application built using Streamlit, LangChain, and SQLAlchemy that allows users to interact with an MSSQLServer database using natural language processing (NLP). By integrating Azure OpenAI's GPT-3.5, it enables users to ask questions in natural language, which are then processed and translated into SQL queries to retrieve data from the database.
Features Natural Language Queries: Users can ask questions in plain English, which the application translates into SQL queries to interact with an MSSQLServer database. Azure OpenAI GPT-3.5 Integration: Leverages Azure’s GPT-3.5 Turbo model to generate responses. Streamlit UI: A simple, user-friendly interface for selecting models, providing API keys, and querying the database. SQLAlchemy and LangChain: Utilizes LangChain to manage the process of converting user inputs into SQL queries using SQLAlchemy as the database engine.
How It Works
User Input: Users provide a question or query in the chat input box (e.g., "What is the average salary from the employees table?"). LLM Processing: The question is sent to the Azure OpenAI service, where it is processed by a selected LLM model (GPT-3.5 Turbo). SQL Query Generation: LangChain agents use the user's input to generate a corresponding SQL query for the connected MSSQLServer database. Database Interaction: The SQL query is executed on the database, and the results are fetched. Response Display: The results are displayed back to the user in the Streamlit interface.
Requirements
Python 3.9+ Streamlit LangChain SQLAlchemy Azure OpenAI API Access MSSQLServer Database
Installation
- Clone the repository:
![image](https://private-user-images.githubusercontent.com/106134125/374314891-72b1f505-d174-40fa-879b-ea294e396ae2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDAwMzgyODcsIm5iZiI6MTc0MDAzNzk4NywicGF0aCI6Ii8xMDYxMzQxMjUvMzc0MzE0ODkxLTcyYjFmNTA1LWQxNzQtNDBmYS04NzliLWVhMjk0ZTM5NmFlMi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIyMFQwNzUzMDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lZjQxOGI4M2EyZjRiNjEyNjZmMmY0N2RiYzE1ZmU4NDk1OGNiMDJmNGY3ZjRlM2I4MzkzODEzNTMzNjQzNGYwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.hYaCizWbGFc7VwH9WL3Hj4kV-LvlDjEZ_Opw72MzFII)
- Navigate to the project directory:
![image](https://private-user-images.githubusercontent.com/106134125/374315463-29291f01-3905-43ec-941e-7e89ac72d6a0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDAwMzgyODcsIm5iZiI6MTc0MDAzNzk4NywicGF0aCI6Ii8xMDYxMzQxMjUvMzc0MzE1NDYzLTI5MjkxZjAxLTM5MDUtNDNlYy05NDFlLTdlODlhYzcyZDZhMC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIyMFQwNzUzMDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yMTQ3NmE0Y2I3ZWFkMmI1MWM1YTJiMTY4YzIzYmIwODkxZDlkMGE1YjllZjhmZTJjNzZhNzkyYzA4ZmYxYmM0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.EZOu2jPqKdBBQCoEovXTxi7uIOaNDAU4xoBgpAbWJ0s)
- Install the required dependencies:
![image](https://private-user-images.githubusercontent.com/106134125/374315656-ea7d0ee0-e71b-4b54-a6e3-dc947d3e241a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDAwMzgyODcsIm5iZiI6MTc0MDAzNzk4NywicGF0aCI6Ii8xMDYxMzQxMjUvMzc0MzE1NjU2LWVhN2QwZWUwLWU3MWItNGI1NC1hNmUzLWRjOTQ3ZDNlMjQxYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIyMFQwNzUzMDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yNjg4NjEzNzEzZWQ0ZTcwYjAyOTNlZWY0ZjJhN2M3ODYwMmY1N2QxYmEzNDdlN2UzMTllZGJhNTUxMjFhNTM5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.8_XPtrq40Af1aMxdHb_4kV02yrpdUBhMzvBuCgbcQoc)
-
Set up an MSSQLServer database and note down the connection string (e.g., mssql+pymssql://username:password@localhost:1433/database).
-
Obtain Azure OpenAI API access and note down your:
- Deployment Name
- API Version
- API Key
- API Endpoint
- Running the Application
-
Run the Streamlit application:
![image](https://private-user-images.githubusercontent.com/106134125/374315955-3f6e3b4f-e682-44db-894e-e7bb6fff1c05.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDAwMzgyODcsIm5iZiI6MTc0MDAzNzk4NywicGF0aCI6Ii8xMDYxMzQxMjUvMzc0MzE1OTU1LTNmNmUzYjRmLWU2ODItNDRkYi04OTRlLWU3YmI2ZmZmMWMwNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjIwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIyMFQwNzUzMDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lZDRjY2ZjYzZhNzAyODcyYjg2NDU2MDBiYmM5NjZkYmM1M2I4OGJhYWYxZThjYzAzNTQwOTI2MzhmYjA2ZDYyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.c5929V2aB-jGUQKQ1Gr4bqNJ6REJ2ixcLEVxfCGjYNs)
In the sidebar, provide the necessary details:
- Chat Model: Choose GPT 3.5 Turbo.
- Azure LLM Deployment Name: Enter your deployment name from Azure OpenAI.
- API Version: Select the API version, e.g., 2023-12-01-preview.
- Azure LLM Endpoint: Enter your Azure OpenAI endpoint URL.
- Azure LLM API Key: Provide your API key (this will be kept secure).
- In the chat box, ask your natural language query related to the database, and the system will return the results.
Example Usage
- The user inputs a question: "Which sales order has the hightest quantity?" The application:
- Processes the input with the selected Azure LLM.
- Converts the question into an SQL query.
- Executes the query on the MSSQLServer database.
- Displays the result in the chat.
Project Structure - app.py: Main application script. - requirements.txt: List of required dependencies.
License This project is licensed under the MIT License. See the LICENSE file for more details.
Acknowledgments - Streamlit for building intuitive web applications. - LangChain for the robust NLP and agent tools. - Azure OpenAI for providing the language models.