logeswari commited on
Commit
5780b29
Β·
1 Parent(s): 3240abf

changes made

Browse files
Files changed (1) hide show
  1. chatbot.py +15 -25
chatbot.py CHANGED
@@ -4,16 +4,16 @@ from langchain_community.utilities import SQLDatabase
4
  from langchain.chat_models import init_chat_model
5
  from langchain_community.agent_toolkits import create_sql_agent
6
  from langchain.callbacks.base import BaseCallbackHandler
 
7
 
8
  # Database connection string
9
  url = "postgresql://postgres.qxvpaoeakhddzabctekw:8&CiDRpTFbRRBrT@aws-0-ap-south-1.pooler.supabase.com:5432/postgres"
10
 
11
- # βœ… Initialize session state variables if they don't exist
12
  if "db" not in st.session_state:
13
- st.session_state.db = SQLDatabase.from_uri(url)
14
-
15
- if "chat_history" not in st.session_state:
16
- st.session_state.chat_history = [] # Stores past queries and responses
17
 
18
  if "agent_chain_output" not in st.session_state:
19
  st.session_state.agent_chain_output = ""
@@ -83,18 +83,6 @@ def main():
83
  schema = st.session_state.db.get_table_info()
84
  st.code(schema)
85
 
86
- # Display Chat History
87
- st.sidebar.subheader("πŸ“œ Chat History")
88
- for entry in st.session_state.chat_history:
89
- with st.sidebar.expander(f"Query: {entry['query']}"):
90
- st.write(f"**Agent Response:** {entry['response']}")
91
- if show_thinking and "thinking" in entry:
92
- st.write(f"**Agent Thinking:**\n{entry['thinking']}")
93
-
94
- if st.sidebar.button("πŸ—‘ Clear Chat History"):
95
- st.session_state.chat_history = []
96
- st.experimental_rerun()
97
-
98
  # Query input section
99
  query_options = ["Free-form query", "Get employee details"]
100
  query_type = st.radio("Query Type:", query_options)
@@ -107,7 +95,15 @@ def main():
107
  query = f"Give details of employee ID {employee_id}" if employee_id else ""
108
 
109
  # Process button
110
- process_button = st.button("Run Query")
 
 
 
 
 
 
 
 
111
 
112
  # Results section
113
  st.header("Results")
@@ -130,6 +126,7 @@ def main():
130
 
131
  # Initialize LLM
132
  try:
 
133
  api_key = "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"
134
 
135
  # Create a new LLM instance
@@ -154,13 +151,6 @@ def main():
154
  with st.spinner("Processing your query..."):
155
  result = agent_executor.invoke(query)
156
 
157
- # Store chat history
158
- st.session_state.chat_history.append({
159
- "query": query,
160
- "response": result["output"],
161
- "thinking": stream_handler.text if show_thinking else ""
162
- })
163
-
164
  # Display the result
165
  result_container.success("Query processed successfully!")
166
  result_output.markdown("### Answer")
 
4
  from langchain.chat_models import init_chat_model
5
  from langchain_community.agent_toolkits import create_sql_agent
6
  from langchain.callbacks.base import BaseCallbackHandler
7
+ import time
8
 
9
  # Database connection string
10
  url = "postgresql://postgres.qxvpaoeakhddzabctekw:8&CiDRpTFbRRBrT@aws-0-ap-south-1.pooler.supabase.com:5432/postgres"
11
 
12
+ # Initialize session state variables if they don't exist
13
  if "db" not in st.session_state:
14
+ st.session_state.db = SQLDatabase.from_uri(
15
+ "postgresql://postgres.qxvpaoeakhddzabctekw:8&CiDRpTFbRRBrT@aws-0-ap-south-1.pooler.supabase.com:5432/postgres"
16
+ )
 
17
 
18
  if "agent_chain_output" not in st.session_state:
19
  st.session_state.agent_chain_output = ""
 
83
  schema = st.session_state.db.get_table_info()
84
  st.code(schema)
85
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  # Query input section
87
  query_options = ["Free-form query", "Get employee details"]
88
  query_type = st.radio("Query Type:", query_options)
 
95
  query = f"Give details of employee ID {employee_id}" if employee_id else ""
96
 
97
  # Process button
98
+ col1, col2 = st.columns([1, 5])
99
+ with col1:
100
+ process_button = st.button("Run Query", type="primary", use_container_width=True)
101
+ with col2:
102
+ if process_button:
103
+ clear_button = st.button("Clear Results", use_container_width=True)
104
+ if clear_button:
105
+ st.session_state.agent_chain_output = ""
106
+ st.experimental_rerun()
107
 
108
  # Results section
109
  st.header("Results")
 
126
 
127
  # Initialize LLM
128
  try:
129
+ # Define a fixed API key (hardcoded for simplicity)
130
  api_key = "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"
131
 
132
  # Create a new LLM instance
 
151
  with st.spinner("Processing your query..."):
152
  result = agent_executor.invoke(query)
153
 
 
 
 
 
 
 
 
154
  # Display the result
155
  result_container.success("Query processed successfully!")
156
  result_output.markdown("### Answer")