SQL Data Retriever is a chat-based interface designed to interact with a local SQLite database using natural language queries. It provides a way for users to fetch data from a company database by simply typing queries in conversational language. The system translates these queries into SQL SELECT statements and retrieves the relevant data.
This project can be further modified to integrate it with a MySQL data for real-world applications.
- Natural Language Interface: Allows users to input queries in natural language.
- SQL Query Execution: Translates natural language queries into SQL and executes them against a
SQLitedatabase. - Error Handling: Includes extensive error handling for query execution, formatting, and database connectivity.
- Tool Integration: Utilizes a tool-based architecture to execute SQL queries.
- Model Selection: Supports selection from available language models for query processing.
-
Company_Chatbot:- The main class that initializes the chatbot, reads the system prompt, sets up the SQL tool, and handles interactions.
-
SQLFetchTool:- A function designed to execute and handle SQL
SELECTqueries, including error management for invalid queries or connection issues.
- A function designed to execute and handle SQL
-
tool_call_handling:- Handles tool calls, processes results, and formats responses for the chat interface.
-
chat_func:- Manages chat interactions, processes user inputs, and integrates with LLMs to generate responses.
- Python installed on your machine.
- gradio: The UI framework for the chat interface.
- SQLAlchemy: ORM for database connectivity and queries.
- ollama Open Source API for running LLMs locally.
- Use the
create_db.pyto create a new custom db or use the one provided in repo.
-
Clone this repository:
git clone https://github.com/your_username/sql-data-retriever.git cd sql-data-retriever -
Install the required Python packages:
pip install gradio sqlalchemy
-
Ensure your
Company.dbdatabase is properly set up and configured.
Launch the chat interface locally by executing the script:
python Main.pyThe interface will open in your default web browser and the website will be hosted locally for 72 hours. The public link will be available in the terminal.
- Select the preferred language model from the dropdown.
- Enter your natural language query in the provided textbox.
- Receive SQL query results directly through the chat interface.
The system is designed to handle various errors during query execution, including:
- Invalid Query Format: Ensures only valid
SELECTqueries are run. - Database Connection Errors: Reports issues with connecting to the
SQLitedatabase. - Empty Query or Results: Handles cases where queries are empty or return no results.
- System Prompt: Modify the
system_prompt.txtfile to tailor the system’s initial behavior. - Database: Replace
Company.dbwith your own SQLite database.
Distributed under the Apache 2.0 License. See LICENSE for more information.
Your Name - Aniket Das
Project Link: https://github.com/SINISTERX69/SQL_Retriever_Chatbot_gr