File size: 8,786 Bytes
c34a261
f850ef1
 
c34a261
 
f850ef1
 
c34a261
f850ef1
 
c34a261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00d3899
0c1c745
 
 
 
 
 
 
 
c34a261
 
f850ef1
c34a261
00d3899
f850ef1
00d3899
 
 
0c1c745
 
 
f850ef1
00d3899
 
f850ef1
0c1c745
00d3899
 
 
 
 
 
 
0c1c745
f850ef1
 
 
c34a261
f850ef1
 
 
c34a261
 
 
f850ef1
 
 
c34a261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00d3899
c34a261
 
 
 
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
import streamlit as st
from langchain import OpenAI, SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain
from langchain_openai import ChatOpenAI
import os
import time
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.prompts import PromptTemplate
from langchain_community.callbacks import get_openai_callback

# Custom CSS for enhanced styling
def local_css():
    st.markdown("""
    <style>
    .main-container {
        background-color: #f0f2f6;
        padding: 2rem;
        border-radius: 15px;
    }
    .stApp {
        background-color: #ffffff;
    }
    .stChatInput {
        border-radius: 15px !important;
        border: 2px solid #3366cc !important;
    }
    .chat-header {
        background-color: #3366cc;
        color: white;
        padding: 15px;
        border-radius: 10px;
        margin-bottom: 20px;
    }
    .chat-message {
        margin-bottom: 10px;
        padding: 10px;
        border-radius: 10px;
    }
    .human-message {
        background-color: #e6f2ff;
        border-left: 4px solid #3366cc;
    }
    .ai-message {
        background-color: #f0f0f0;
        border-left: 4px solid #666666;
    }
    .sidebar .stTextInput > div > div > input {
        border-radius: 10px !important;
    }
    .sidebar {
        background-color: #f8f9fa;
        border-radius: 15px;
        padding: 15px;
    }
    </style>
    """, unsafe_allow_html=True)

def init_database(user: str, password: str, host: str, port: str, database: str, sslmode: str = None):
    """Initialize a connection to the PostgreSQL database."""
    try:
        db_uri = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
        if sslmode:
            db_uri += f"?sslmode={sslmode}"
        
        db = SQLDatabase.from_uri(db_uri)
        return db
    except Exception as e:
        st.error(f"Unable to connect to the database: {e}")
        return None

def answer_sql(question: str, db, chat_history: list, llm):
    """Generate SQL answer based on the user's question and database content."""
    try:
        prompt = PromptTemplate(
            input_variables=['input', 'table_info', 'top_k'],
            template="""You are a PostgreSQL expert. Given an input question,
                        first create a syntactically correct PostgreSQL query to run,
                        then look at the results of the query and return the answer to the input question.
                        Unless the user specifies in the question a specific number of records to obtain, query for at most {top_k} results using the LIMIT clause as per PostgreSQL.
                        Wrap each column name in double quotes (") to denote them as delimited identifiers.
                        Only use the following tables:\n{table_info}\n\nQuestion: {input}')"""
        )

        db_chain = SQLDatabaseChain(
            llm=llm, 
            database=db, 
            top_k=100, 
            verbose=True, 
            use_query_checker=True, 
            prompt=prompt, 
            return_intermediate_steps=True
        )

        with get_openai_callback() as cb:
            response = db_chain.invoke({
                "query": question,
                "chat_history": chat_history,
            })["result"]

            # Optional: Log token usage (you can remove this or add logging as needed)
            print(f"Total Tokens: {cb.total_tokens}")
            print(f"Total Cost (USD): ${cb.total_cost}")

        return response
    except Exception as e:
        st.error(f"An error occurred: {e}")
        return "Sorry, I couldn't process your request."

def main():
    # Apply custom CSS
    local_css()

    # Set page configuration
    st.set_page_config(
        page_title="PostgreSQL Query Assistant", 
        page_icon="πŸ€–", 
        layout="wide"
    )

    # Main container
    with st.container():
        # Header
        st.markdown("<div class='chat-header'><h1 style='text-align: center;'>πŸ€– PostgreSQL Query Assistant</h1></div>", unsafe_allow_html=True)

        # Sidebar for connection
        with st.sidebar:
            st.image("https://www.postgresql.org/media/img/about/press/elephant.png", use_container_width=True)
            st.header("Database Connection")
            
            # Connection details
            with st.expander("Database Credentials", expanded=True):
                openai_api_key = st.text_input("OpenAI API Key", type="password", help="Required for natural language to SQL conversion")
                
                db_type = st.radio("Database Type", ("Local", "Cloud"))
                
                if db_type == "Local":
                    host = st.text_input("Host", value="localhost")
                    port = st.text_input("Port", value="5432")
                    user = st.text_input("Username", value="postgres")
                    password = st.text_input("Password", type="password")
                    database = st.text_input("Database Name", value="testing_3")
                    sslmode = None
                else:
                    host = st.text_input("Host (e.g., your-db-host.aws.com)")
                    port = st.text_input("Port", value="5432")
                    user = st.text_input("Username")
                    password = st.text_input("Password", type="password")
                    database = st.text_input("Database Name")
                    sslmode = st.selectbox("SSL Mode", ["require", "verify-ca", "verify-full", "disable"])

                connect_btn = st.button("πŸ”Œ Connect to Database")

        # Main chat area
        chat_container = st.container()

        # Initialize or load session state
        if 'chat_history' not in st.session_state:
            st.session_state.chat_history = [
                AIMessage(content="πŸ‘‹ Hi there! I'm your PostgreSQL Query Assistant. Connect to your database and ask me anything!")
            ]
        
        if 'db_connected' not in st.session_state:
            st.session_state.db_connected = False

        # Connection handling
        if connect_btn:
            if not openai_api_key:
                st.error("Please provide an OpenAI API Key")
            else:
                os.environ["OPENAI_API_KEY"] = openai_api_key
                llm = ChatOpenAI(temperature=0.7, model="gpt-3.5-turbo")
                
                db = init_database(user, password, host, port, database, sslmode)
                
                if db:
                    st.session_state.db = db
                    st.session_state.llm = llm
                    st.session_state.db_connected = True
                    st.success("πŸŽ‰ Successfully connected to the database!")

        # Display chat history
        with chat_container:
            for message in st.session_state.chat_history:
                if isinstance(message, AIMessage):
                    with st.chat_message("assistant", avatar="πŸ€–"):
                        st.markdown(f"<div class='chat-message ai-message'>{message.content}</div>", unsafe_allow_html=True)
                elif isinstance(message, HumanMessage):
                    with st.chat_message("user", avatar="πŸ‘€"):
                        st.markdown(f"<div class='chat-message human-message'>{message.content}</div>", unsafe_allow_html=True)

        # Chat input and processing
        if st.session_state.db_connected:
            user_query = st.chat_input("Ask a question about your database...")
            
            if user_query:
                # Add user message to chat history
                st.session_state.chat_history.append(HumanMessage(content=user_query))
                
                # Display user message
                with st.chat_message("user", avatar="πŸ‘€"):
                    st.markdown(f"<div class='chat-message human-message'>{user_query}</div>", unsafe_allow_html=True)
                
                # Generate and display AI response
                with st.chat_message("assistant", avatar="πŸ€–"):
                    with st.spinner("Generating response..."):
                        response = answer_sql(
                            user_query, 
                            st.session_state.db, 
                            st.session_state.chat_history, 
                            st.session_state.llm
                        )
                    st.markdown(f"<div class='chat-message ai-message'>{response}</div>", unsafe_allow_html=True)
                
                # Add AI response to chat history
                st.session_state.chat_history.append(AIMessage(content=response))
        else:
            st.warning("Please connect to a database to start querying.")

if __name__ == "__main__":
    main()