File size: 11,891 Bytes
85397cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import streamlit as st
import os
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_community.utilities import SQLDatabase
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langchain_groq import ChatGroq
import toml

# Function to update secrets.toml file
def update_secrets_file(data):
    secrets_file_path = ".streamlit/secrets.toml"
    if os.path.exists(secrets_file_path):
        with open(secrets_file_path, "r") as file:
            secrets_data = toml.load(file)
    else:
        secrets_data = {}

    secrets_data.update(data)

    with open(secrets_file_path, "w") as file:
        toml.dump(secrets_data, file)

# Initialize database connections
def init_databases():
    secrets_file_path = ".streamlit/secrets.toml"
    with open(secrets_file_path, "r") as file:
        secrets_data = toml.load(file)
        
    db_connections = {}
    for database in secrets_data["Databases"].split(','):
        database = database.strip()
        db_uri = f"mysql+mysqlconnector://{secrets_data['User']}:{secrets_data['Password']}@{secrets_data['Host']}:{secrets_data['Port']}/{database}"
        db_connections[database] = SQLDatabase.from_uri(db_uri)
    return db_connections

# Function to get SQL chain
def get_sql_chain(dbs, llm):
    template = """

    You are a Senior and vastly experienced Data analyst at a company with around 20 years of experience. 

    You are interacting with a user who is asking you questions about the company's databases.

    Based on the table schemas below, write SQL queries that would answer the user's question. Take the conversation history into account.

    

    <SCHEMAS>{schemas}</SCHEMAS>

    

    Conversation History: {chat_history}

    

    Write the SQL queries for each relevant database, prefixed by the database name (e.g., DB1: SELECT * FROM ...; DB2: SELECT * FROM ...).

    Do not wrap the SQL queries in any other text, not even backticks.

    

    For example:

    Question: which 3 artists have the most tracks?

    SQL Query: SELECT ArtistId, COUNT(*) as track_count FROM Track GROUP BY ArtistId ORDER BY track_count DESC LIMIT 3;

    Question: Name 10 artists

    SQL Query: SELECT Name FROM Artist LIMIT 10;

    Question: How much is the price of the inventory for all small size t-shirts?

    SQL Query: SELECT SUM(price * stock_quantity) FROM t_shirts WHERE size = 'S';

    Question: If we have to sell all the Levi's T-shirts today with discounts applied. How much revenue our store will generate (post discounts)?

    SQL Query: SELECT SUM(a.total_amount * ((100 - COALESCE(discounts.pct_discount, 0)) / 100)) AS total_revenue 

               FROM (SELECT SUM(price * stock_quantity) AS total_amount, t_shirt_id 

               FROM t_shirts 

               WHERE brand = 'Levi' GROUP BY t_shirt_id) a   

               LEFT JOIN discounts ON a.t_shirt_id = discounts.t_shirt_id;

    Question: For each brand, find the total revenue generated from t-shirts with a discount applied, grouped by the discount percentage.

    SQL Query: SELECT brand, COALESCE(discounts.pct_discount, 0) AS discount_pct, SUM(t.price * t.stock_quantity * (1 - COALESCE(discounts.pct_discount, 0) / 100)) AS total_revenue

               FROM t_shirts t

               LEFT JOIN discounts ON t.t_shirt_id = discounts.t_shirt_id

               GROUP BY brand, COALESCE(discounts.pct_discount, 0);

    Question: Find the top 3 most popular colors for each brand, based on the total stock quantity.

    SQL Query: SELECT brand, color, SUM(stock_quantity) AS total_stock

               FROM t_shirts

               GROUP BY brand, color

               ORDER BY brand, total_stock DESC;

               

    Question: Calculate the average price per size for each brand, excluding sizes with less than 10 t-shirts in stock.

    SQL Query: SELECT brand, size, AVG(price) AS avg_price

               FROM t_shirts

               WHERE stock_quantity >= 10

               GROUP BY brand, size

               HAVING COUNT(*) >= 10;

               

    Question: Find the brand and color combination with the highest total revenue, considering discounts.

    SQL Query: SELECT brand, color, SUM(t.price * t.stock_quantity * (1 - COALESCE(d.pct_discount, 0) / 100)) AS total_revenue

               FROM t_shirts t

               LEFT JOIN discounts d ON t.t_shirt_id = d.t_shirt_id

               GROUP BY brand, color

               ORDER BY total_revenue DESC

               LIMIT 1;

               

    Question: Create a view that shows the total stock quantity and revenue for each brand, size, and color combination.

    SQL Query: CREATE VIEW brand_size_color_stats AS

               SELECT brand, size, color, SUM(stock_quantity) AS total_stock, SUM(price * stock_quantity) AS total_revenue

               FROM t_shirts

               GROUP BY brand, size, color;

    

    Question: How much is the price of the inventory for all varients t-shirts and group them y brands?

    SQL Query: SELECT brand, SUM(price * stock_quantity) FROM t_shirts GROUP BY brand;

    

    Question: List the total revenue of t-shirts of L size for all brands

    SQL Query: SELECT brand, SUM(price * stock_quantity) AS total_revenue FROM t_shirts WHERE size = 'L' GROUP BY brand;   

    

    Question: How many shirts are available in stock grouped by colours from each size and finally show me all brands?

    SQL Query: SELECT brand, color, size, SUM(stock_quantity) AS total_stock FROM t_shirts GROUP BY brand, color, size



    Your turn:

    

    Question: {question}

    SQL Queries:

    """
    
    prompt = ChatPromptTemplate.from_template(template)
    llm = llm

    def get_schema(_):
        schemas = {db_name: db.get_table_info() for db_name, db in dbs.items()}
        return schemas

    return (
        RunnablePassthrough.assign(schemas=get_schema)
        | prompt
        | llm
        | StrOutputParser()
        | (lambda result: {line.split(":")[0]: line.split(":")[1].strip() for line in result.strip().split("\n") if ":" in line and line.strip()})
    )

# Function to get response
def get_response(user_query, dbs, chat_history, llm):
    sql_chain = get_sql_chain(dbs, llm)
    
    template = """

    You are a Senior and vastly experienced Data analyst at a company with around 20 years of experience.

    You are interacting with a user who is asking you questions about the company's databases.

    Based on the table schemas below, question, sql queries, and sql responses, write an 

    accurate natural language response so that the end user can understand things

    and make sure do not include words like "Based on the SQL queries I ran". 

    Just provide only the answer with some text that the user expects.

    <SCHEMAS>{schemas}</SCHEMAS>

    Conversation History: {chat_history}

    SQL Queries: <SQL>{queries}</SQL>

    User question: {question}

    SQL Responses: {responses}"""
    
    prompt = ChatPromptTemplate.from_template(template)
    llm = llm
   
    def run_queries(var):
        responses = {}
        for db_name, query in var["queries"].items():
            responses[db_name] = dbs[db_name].run(query)
        return responses
    
    chain = (
        RunnablePassthrough.assign(queries=sql_chain).assign(
            schemas=lambda _: {db_name: db.get_table_info() for db_name, db in dbs.items()},
            responses=run_queries)  # The comma at the end of the assign() method call is used to indicate that there may be more keyword arguments or method calls following it
        | prompt
        | llm
        | StrOutputParser()
    )
    
    return chain.invoke({
        "question": user_query,
        "chat_history": chat_history,
    })

# Streamlit app configuration
if "chat_history" not in st.session_state:
    st.session_state.chat_history = [
        AIMessage(content="Hello! I'm a SQL assistant. Ask me anything about your database."),
    ]

st.set_page_config(page_title="Chat with MySQL", page_icon="🛢️")
st.title("Chat with MySQL")

with st.sidebar:
    st.subheader("Settings")
    st.write("This is a simple chat application using MySQL. Connect to the database and start chatting.")
    
    if "db" not in st.session_state:
        st.session_state.Host = st.text_input("Host", value=st.secrets.get("Host", ""))
        st.session_state.Port = st.text_input("Port", value=st.secrets.get("Port", ""))
        st.session_state.User = st.text_input("User", value=st.secrets.get("User", ""))
        st.session_state.Password = st.text_input("Password", type="password", value=st.secrets.get("Password", ""))
        st.session_state.Databases = st.text_input("Databases", placeholder="Enter DB's separated by (,)", value=st.secrets.get("Databases", ""))
        st.session_state.openai_api_key = st.text_input("OpenAI API Key", type="password", help="Get your API key from [OpenAI Website](https://platform.openai.com/api-keys)", value=st.secrets.get("openai_api_key", ""))
        st.session_state.groq_api_key = st.text_input("Groq API Key", type="password", help="Get your API key from [GROQ Console](https://console.groq.com/keys)", value=st.secrets.get("groq_api_key", ""))

        st.info("Note: For interacting multiple databases, GPT-4 Model is recommended for accurate results else proceed with Groq Model")
        
        os.environ["OPENAI_API_KEY"] = str(st.session_state.openai_api_key)

        if st.button("Connect"):
            with st.spinner("Connecting to databases..."):

                # Update secrets.toml with connection details
                update_secrets_file({
                    "Host": st.session_state.Host,
                    "Port": st.session_state.Port,
                    "User": st.session_state.User,
                    "Password": st.session_state.Password,
                    "Databases": st.session_state.Databases
                })

                dbs = init_databases()
                st.session_state.dbs = dbs

                if len(dbs) > 1:
                    st.success(f"Connected to {len(dbs)} databases")
                else:
                    st.success("Connected to database")
                
                

        if st.session_state.openai_api_key == "" and st.session_state.groq_api_key == "":
            st.error("Enter one API Key At least")
        elif st.session_state.openai_api_key:    
            st.session_state.llm = ChatOpenAI(model="gpt-4-turbo", api_key=st.session_state.openai_api_key)
        elif st.session_state.groq_api_key:
            st.session_state.llm = ChatGroq(model="llama3-70b-8192", temperature=0.4, api_key=st.session_state.groq_api_key)
        else:
            pass

# Display chat messages
for message in st.session_state.chat_history:
    if isinstance(message, AIMessage):
        with st.chat_message("AI"):
            st.markdown(message.content)
    elif isinstance(message, HumanMessage):
        with st.chat_message("Human"):
            st.markdown(message.content)

# Handle user input
user_query = st.chat_input("Type a message...")
if user_query is not None and user_query.strip() != "":
    st.session_state.chat_history.append(HumanMessage(content=user_query))
    
    with st.chat_message("Human"):
        st.markdown(user_query)
        
    with st.chat_message("AI"):
        response = get_response(user_query, st.session_state.dbs, st.session_state.chat_history, st.session_state.llm)
        st.markdown(response)
        
    st.session_state.chat_history.append(AIMessage(content=response))