├── .gitignore ├── docs └── mysql-chains.png ├── readme.md ├── requirements.txt └── src └── app.py /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | -------------------------------------------------------------------------------- /docs/mysql-chains.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alejandro-ao/chat-with-mysql/1cf406a3bc4cfcc6b1f75b90d9b1dab7eed33fc0/docs/mysql-chains.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # MySQL Python Chatbot with GPT-4 and Mistral AI 2 | 3 | Welcome to the GitHub repository for our tutorial on building a natural language SQL chatbot using GPT-4! This project guides you through the development of a chatbot that can interpret natural language queries, generate SQL queries, and fetch results from a SQL database, all in an intuitive and user-friendly way. It utilizes the power of OpenAI's GPT-4 model, integrated with a Streamlit GUI for an enhanced interaction experience. 4 | 5 | 🟡 This repository serves as supporting material for the [YouTube video tutorial](https://youtu.be/YqqRkuizNN4). 6 | 7 | ## Features 8 | - **Natural Language Processing**: Uses GPT-4 to interpret and respond to user queries in natural language. 9 | - **SQL Query Generation**: Dynamically generates SQL queries based on the user's natural language input. 10 | - **Database Interaction**: Connects to a SQL database to retrieve query results, demonstrating practical database interaction. 11 | - **Streamlit GUI**: Features a user-friendly interface built with Streamlit, making it easy for users of all skill levels. 12 | - **Python-based**: Entirely coded in Python, showcasing best practices in software development with modern technologies. 13 | 14 | ## Brief Explanation of How the Chatbot Works 15 | 16 | The chatbot works by taking a user's natural language query, converting it into a SQL query using GPT-4, executing the query on a SQL database, and then presenting the results back to the user in natural language. This process involves several steps of data processing and interaction with the OpenAI API and a SQL database, all seamlessly integrated into a Streamlit application. 17 | 18 | Consider the following diagram to understand how the different chains and components are built: 19 | 20 | ![Chatbot Architecture](./docs/mysql-chains.png) 21 | 22 | For a more detailed explanation and a step-by-step guide, refer this other video: [YouTube video tutorial](https://youtu.be/9ccl1_Wu24Q). 23 | 24 | For a more detailed explanation and a step-by-step guide, refer to the [YouTube video tutorial](Chat with MySQL Database with Python | LangChain Tutorial). 25 | 26 | ## Installation 27 | Ensure you have Python installed on your machine. Then clone this repository: 28 | 29 | ```bash 30 | git clone [repository-link] 31 | cd [repository-directory] 32 | ``` 33 | 34 | Install the required packages: 35 | 36 | ```bash 37 | pip install -r requirements.txt 38 | ``` 39 | 40 | Create your own .env file with the necessary variables, including your OpenAI API key: 41 | 42 | ```bash 43 | OPENAI_API_KEY=[your-openai-api-key] 44 | ``` 45 | 46 | ## Usage 47 | To launch the Streamlit app and interact with the chatbot: 48 | 49 | ```bash 50 | streamlit run app.py 51 | ``` 52 | 53 | ## Contributing 54 | As this repository accompanies the [YouTube video tutorial](https://youtu.be/YqqRkuizNN4), we are primarily focused on providing a comprehensive learning experience. Contributions for bug fixes or typos are welcome. 55 | 56 | ## License 57 | This project is licensed under the MIT License - see the LICENSE file for details. 58 | 59 | --- 60 | 61 | **Note**: This project is intended for educational and research purposes. Please ensure compliance with the terms of use and guidelines of any APIs or services used. 62 | 63 | --- 64 | 65 | We hope this repository aids in your exploration of integrating AI with web technologies. For more informative tutorials, be sure to check out [Your YouTube Channel]. 66 | 67 | Happy Coding! 🚀👨‍💻🤖 68 | 69 | --- 70 | 71 | *If you find this project helpful, please consider giving it a star!* 72 | 73 | --- 74 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit==1.31.1 2 | langchain==0.1.8 3 | langchain-community==0.0.21 4 | langchain-core==0.1.24 5 | langchain-openai==0.0.6 6 | mysql-connector-python==8.3.0 7 | groq==0.4.2 8 | langchain-groq==0.0.1 9 | -------------------------------------------------------------------------------- /src/app.py: -------------------------------------------------------------------------------- 1 | from dotenv import load_dotenv 2 | from langchain_core.messages import AIMessage, HumanMessage 3 | from langchain_core.prompts import ChatPromptTemplate 4 | from langchain_core.runnables import RunnablePassthrough 5 | from langchain_community.utilities import SQLDatabase 6 | from langchain_core.output_parsers import StrOutputParser 7 | from langchain_openai import ChatOpenAI 8 | from langchain_groq import ChatGroq 9 | import streamlit as st 10 | 11 | def init_database(user: str, password: str, host: str, port: str, database: str) -> SQLDatabase: 12 | db_uri = f"mysql+mysqlconnector://{user}:{password}@{host}:{port}/{database}" 13 | return SQLDatabase.from_uri(db_uri) 14 | 15 | def get_sql_chain(db): 16 | template = """ 17 | You are a data analyst at a company. You are interacting with a user who is asking you questions about the company's database. 18 | Based on the table schema below, write a SQL query that would answer the user's question. Take the conversation history into account. 19 | 20 | {schema} 21 | 22 | Conversation History: {chat_history} 23 | 24 | Write only the SQL query and nothing else. Do not wrap the SQL query in any other text, not even backticks. 25 | 26 | For example: 27 | Question: which 3 artists have the most tracks? 28 | SQL Query: SELECT ArtistId, COUNT(*) as track_count FROM Track GROUP BY ArtistId ORDER BY track_count DESC LIMIT 3; 29 | Question: Name 10 artists 30 | SQL Query: SELECT Name FROM Artist LIMIT 10; 31 | 32 | Your turn: 33 | 34 | Question: {question} 35 | SQL Query: 36 | """ 37 | 38 | prompt = ChatPromptTemplate.from_template(template) 39 | 40 | # llm = ChatOpenAI(model="gpt-4-0125-preview") 41 | llm = ChatGroq(model="mixtral-8x7b-32768", temperature=0) 42 | 43 | def get_schema(_): 44 | return db.get_table_info() 45 | 46 | return ( 47 | RunnablePassthrough.assign(schema=get_schema) 48 | | prompt 49 | | llm 50 | | StrOutputParser() 51 | ) 52 | 53 | def get_response(user_query: str, db: SQLDatabase, chat_history: list): 54 | sql_chain = get_sql_chain(db) 55 | 56 | template = """ 57 | You are a data analyst at a company. You are interacting with a user who is asking you questions about the company's database. 58 | Based on the table schema below, question, sql query, and sql response, write a natural language response. 59 | {schema} 60 | 61 | Conversation History: {chat_history} 62 | SQL Query: {query} 63 | User question: {question} 64 | SQL Response: {response}""" 65 | 66 | prompt = ChatPromptTemplate.from_template(template) 67 | 68 | # llm = ChatOpenAI(model="gpt-4-0125-preview") 69 | llm = ChatGroq(model="mixtral-8x7b-32768", temperature=0) 70 | 71 | chain = ( 72 | RunnablePassthrough.assign(query=sql_chain).assign( 73 | schema=lambda _: db.get_table_info(), 74 | response=lambda vars: db.run(vars["query"]), 75 | ) 76 | | prompt 77 | | llm 78 | | StrOutputParser() 79 | ) 80 | 81 | return chain.invoke({ 82 | "question": user_query, 83 | "chat_history": chat_history, 84 | }) 85 | 86 | 87 | if "chat_history" not in st.session_state: 88 | st.session_state.chat_history = [ 89 | AIMessage(content="Hello! I'm a SQL assistant. Ask me anything about your database."), 90 | ] 91 | 92 | load_dotenv() 93 | 94 | st.set_page_config(page_title="Chat with MySQL", page_icon=":speech_balloon:") 95 | 96 | st.title("Chat with MySQL") 97 | 98 | with st.sidebar: 99 | st.subheader("Settings") 100 | st.write("This is a simple chat application using MySQL. Connect to the database and start chatting.") 101 | 102 | st.text_input("Host", value="localhost", key="Host") 103 | st.text_input("Port", value="3306", key="Port") 104 | st.text_input("User", value="root", key="User") 105 | st.text_input("Password", type="password", value="admin", key="Password") 106 | st.text_input("Database", value="Chinook", key="Database") 107 | 108 | if st.button("Connect"): 109 | with st.spinner("Connecting to database..."): 110 | db = init_database( 111 | st.session_state["User"], 112 | st.session_state["Password"], 113 | st.session_state["Host"], 114 | st.session_state["Port"], 115 | st.session_state["Database"] 116 | ) 117 | st.session_state.db = db 118 | st.success("Connected to database!") 119 | 120 | for message in st.session_state.chat_history: 121 | if isinstance(message, AIMessage): 122 | with st.chat_message("AI"): 123 | st.markdown(message.content) 124 | elif isinstance(message, HumanMessage): 125 | with st.chat_message("Human"): 126 | st.markdown(message.content) 127 | 128 | user_query = st.chat_input("Type a message...") 129 | if user_query is not None and user_query.strip() != "": 130 | st.session_state.chat_history.append(HumanMessage(content=user_query)) 131 | 132 | with st.chat_message("Human"): 133 | st.markdown(user_query) 134 | 135 | with st.chat_message("AI"): 136 | response = get_response(user_query, st.session_state.db, st.session_state.chat_history) 137 | st.markdown(response) 138 | 139 | st.session_state.chat_history.append(AIMessage(content=response)) --------------------------------------------------------------------------------