├── SQL_Chain_Process.png ├── requirments.txt ├── README.md └── main.py /SQL_Chain_Process.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maram-bakini/Querying-Your-database/HEAD/SQL_Chain_Process.png -------------------------------------------------------------------------------- /requirments.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | google-generativeai 3 | python-dotenv 4 | langchain==0.1.8 5 | langchain-community==0.0.21 6 | mysql-connector-python==8.3.0 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL Chatbot with Gemini Pro using Natural language 2 | 3 | Welcome to the SQL Chatbot project! This application allows users to interact with a SQL database through a conversational AI interface. Using **Gemini Pro**—an open-source large language model (LLM)—this chatbot converts natural language questions into SQL queries, connects to a database, and retrieves answers for the user. This project is built for accessibility, enabling non-technical users to explore and understand data without needing SQL expertise. 4 | 5 | ## Table of Contents 6 | - [Overview](#overview) 7 | - [Key Features](#key-features) 8 | - [Tech Stack](#tech-stack) 9 | - [Explanation of How the Chatbot Works](#Explanation-of-How-the-Chatbot-Works) 10 | 11 | 12 | ## Overview 13 | The SQL Chatbot was developed to make data retrieval from SQL databases easier and more intuitive. By entering questions in plain language, users can interact directly with the database without writing SQL queries. The bot also includes memory capabilities, allowing for smooth follow-up questions and more conversational interactions. 14 | 15 | ## Key Features 16 | - **Natural Language to SQL Query Translation** 17 | Uses **Gemini Pro** to translate user questions into accurate SQL queries. 18 | 19 | - **Conversational Memory** 20 | Implements memory to enable seamless follow-up questions and context retention, enhancing the user experience. 21 | 22 | - **SQL Database Connectivity** 23 | Connects to a SQL database to execute queries and retrieve data. 24 | 25 | - **User-Friendly Chat Interface** 26 | Built with **Streamlit** to provide a simple and accessible frontend where users can type questions and view results. 27 | 28 | ## Tech Stack 29 | - **Gemini Pro** - Large Language Model for generating SQL from natural language 30 | - **Python** - Core programming language 31 | - **Streamlit** - Framework for creating an interactive web interface 32 | - **LangChain** - For integrating and managing LLM-based workflows 33 | 34 | ## Explanation of How the Chatbot Works 35 | 36 | ![Chatbot Interface](SQL_Chain_Process.png) 37 | 38 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from dotenv import load_dotenv 2 | load_dotenv() 3 | import streamlit as st 4 | import os 5 | import google.generativeai as genai 6 | from langchain_community.utilities.sql_database import SQLDatabase 7 | from langchain_core.messages import AIMessage, HumanMessage 8 | 9 | genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) 10 | 11 | def init_database(db_user: str,db_password: str,db_host: str,db_name: str) -> SQLDatabase: 12 | db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}") 13 | return db 14 | 15 | 16 | def get_schema(db): 17 | return db.get_table_info() 18 | 19 | 20 | def get_sql_chain(schema,chat_history,question): 21 | template= """ 22 | 23 | You are an expert in converting English questions to SQL query! 24 | Always ensure the SQL command is optimized and doesn't have any unnecessary keywords. 25 | Based on the table schema below, write an sql query that would answer the user's question.Take the conversation history into account. 26 | 27 | {schema} 28 | Conversation History: {chat_history} 29 | 30 | 31 | Please generate the most efficient SQL query and nothing else without unnecessary comments or SQL keywords like 'sql'. 32 | Please ensure that the response does not start or contain "'''". 33 | 34 | 35 | For example: 36 | Example 1: How many records are present? 37 | SQL: SELECT COUNT(*) FROM ; 38 | 39 | Example 2: Show all students in the Data Science class. 40 | SQL: SELECT * FROM STUDENT WHERE CLASS='Data Science'; 41 | 42 | Your turn: 43 | 44 | Question:{question} 45 | SQL Query: 46 | 47 | """ 48 | return template.format(schema=schema, chat_history=chat_history, question=question) 49 | 50 | 51 | def Get_NL_response(sql_query,schema,sql_response): 52 | template=""" 53 | You are an expert in SQL! 54 | You have to convert the result of the SQL query execution into human language response. 55 | Based on the table schema below, the user query, the sql response transform the sql response into human language response.Take the conversation history into account. 56 | 57 | {schema} 58 | 59 | Sql_execution_response:{sql_response} 60 | 61 | Your turn: 62 | 63 | Sql query:{sql_query} 64 | Response: 65 | 66 | """ 67 | return template.format(sql_query=sql_query,schema=schema,sql_response=sql_response) 68 | 69 | 70 | 71 | if "chat_history" not in st.session_state: 72 | st.session_state.chat_history=[ 73 | AIMessage("Hello! I'm a SQL assistant. Ask me anything about your database."), 74 | 75 | ] 76 | 77 | def get_gemini_query_explanation(query,prompt): 78 | model = genai.GenerativeModel('gemini-pro') 79 | response = model.generate_content([prompt[1], query]) 80 | return response.text 81 | 82 | def get_gemini_response(question, prompt): 83 | try: 84 | model = genai.GenerativeModel('gemini-pro') 85 | response = model.generate_content([prompt, question]) 86 | return response.text.strip() 87 | except Exception as e: 88 | st.error(f"Error generating SQL query: {e}") 89 | return None 90 | 91 | def get_human_response(prompt): 92 | try: 93 | model = genai.GenerativeModel('gemini-pro') 94 | response = model.generate_content(prompt) 95 | return response.text.strip() 96 | except Exception as e: 97 | st.error(f"Error generating Human response: {e}") 98 | return None 99 | 100 | 101 | 102 | # Streamlit app 103 | st.set_page_config(page_title="SQL Query Generator with LLM") 104 | st.header("Query Your Database ") 105 | 106 | 107 | with st.sidebar: 108 | st.subheader("Database Connection") 109 | 110 | st.text_input("Host",value="localhost",key="host") 111 | st.text_input("User",key="user") 112 | st.text_input("Password",type="password",key="password") 113 | st.text_input("Database",key="database") 114 | 115 | if st.button("Connect"): 116 | with st.spinner("Connecting to the database..."): 117 | db= init_database( 118 | st.session_state["user"], 119 | st.session_state["password"], 120 | st.session_state["host"], 121 | st.session_state["database"], 122 | ) 123 | 124 | st.session_state.db=db 125 | st.success("Connected to the database !") 126 | 127 | 128 | for message in st.session_state.chat_history: 129 | if isinstance(message,AIMessage): 130 | with st.chat_message("ai"): 131 | st.markdown(message.content) 132 | elif isinstance(message,HumanMessage): 133 | with st.chat_message("human"): 134 | st.markdown(message.content) 135 | 136 | if "db" in st.session_state: 137 | schema = get_schema(st.session_state.db) 138 | user_query=st.chat_input("Type a message...") 139 | 140 | 141 | if user_query is not None and user_query.strip() != "": 142 | st.session_state.chat_history.append(HumanMessage(content=user_query)) 143 | 144 | with st.chat_message("human"): 145 | st.markdown(user_query) 146 | with st.chat_message("ai"): 147 | 148 | prompt=get_sql_chain(schema,st.session_state.chat_history,user_query) 149 | sql_query=get_gemini_response(user_query, prompt) 150 | sql_response=st.session_state.db.run(sql_query) 151 | response_prompt=Get_NL_response(sql_query,schema,sql_response) 152 | response=get_human_response(response_prompt) 153 | st.markdown(response) 154 | 155 | st.session_state.chat_history.append(AIMessage(content=response)) 156 | 157 | --------------------------------------------------------------------------------