masadonline commited on
Commit
f3d49d2
·
verified ·
1 Parent(s): c2915ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -100
app.py CHANGED
@@ -116,33 +116,25 @@ def create_vector_store(_documents, _embedding_model_name: str):
116
  return None
117
 
118
  @st.cache_resource(show_spinner="Initializing LLM...")
119
- def get_llm(api_key: str, model_name: str = "mixtral-8x7b-32768"): # "llama3-70b-8192" is another option
120
  """Initializes the Groq LLM."""
121
  if not api_key:
122
  st.error("GROQ_API_KEY not found! Please set it in your environment variables or a .env file.")
123
  return None
124
  try:
 
 
 
 
125
  llm = ChatGroq(temperature=0, groq_api_key=api_key, model_name=model_name)
 
126
  return llm
127
  except Exception as e:
128
  st.error(f"Error initializing Groq LLM: {e}")
129
  return None
130
 
131
  # --- RAG Chain Setup ---
132
- def get_rag_chain(llm, retriever, prompt_template_str):
133
- """Creates a RAG chain with the given LLM, retriever, and prompt template."""
134
- prompt = PromptTemplate(
135
- template=prompt_template_str,
136
- input_variables=["context", "question"]
137
- )
138
-
139
- rag_chain = (
140
- {"context": retriever, "question": RunnablePassthrough()}
141
- | prompt
142
- | llm
143
- | StrOutputParser()
144
- )
145
- return rag_chain
146
 
147
  # --- Main Application Logic ---
148
  def main():
@@ -152,86 +144,35 @@ def main():
152
  # --- UI Setup ---
153
  st.set_page_config(page_title="Internal Knowledge Base AI", layout="wide", initial_sidebar_state="expanded")
154
 
155
- # Custom CSS for a "catchy and elegant" design
156
  st.markdown("""
157
  <style>
158
- /* General body style */
159
- body {
160
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
161
- background-color: #f0f2f6; /* Light gray background */
162
- }
163
- /* Main content area */
164
- .main .block-container {
165
- padding-top: 2rem;
166
- padding-bottom: 2rem;
167
- padding-left: 3rem;
168
- padding-right: 3rem;
169
- background-color: #ffffff; /* White content background */
170
- border-radius: 10px;
171
- box-shadow: 0 4px 12px rgba(0,0,0,0.1); /* Subtle shadow */
172
- }
173
- /* Title style */
174
- h1 {
175
- color: #1E88E5; /* Catchy blue */
176
- text-align: center;
177
- font-weight: 600;
178
- }
179
- /* Sidebar style */
180
- .stSidebar {
181
- background-color: #E3F2FD; /* Light blue sidebar */
182
- padding: 10px;
183
- }
184
- .stSidebar .sidebar-content {
185
- background-color: #E3F2FD;
186
- }
187
- /* Input box style */
188
- .stTextInput > div > div > input {
189
- background-color: #f8f9fa;
190
- border-radius: 5px;
191
- border: 1px solid #ced4da;
192
- }
193
- /* Button style */
194
- .stButton > button {
195
- background-color: #1E88E5; /* Catchy blue */
196
- color: white;
197
- border-radius: 5px;
198
- padding: 0.5rem 1rem;
199
- font-weight: 500;
200
- border: none;
201
- transition: background-color 0.3s ease;
202
- }
203
- .stButton > button:hover {
204
- background-color: #1565C0; /* Darker blue on hover */
205
- }
206
- /* Status messages */
207
- .stAlert { /* For st.info, st.success, st.warning, st.error */
208
- border-radius: 5px;
209
- }
210
- /* Response area */
211
- .response-area {
212
- background-color: #f8f9fa;
213
- padding: 1rem;
214
- border-radius: 5px;
215
- border: 1px solid #e0e0e0;
216
- margin-top: 1rem;
217
- min-height: 100px;
218
- }
219
  </style>
220
  """, unsafe_allow_html=True)
221
 
222
  st.title("📚 Internal Knowledge Base AI 💡")
223
 
224
- # Sidebar for status and information
 
 
 
 
 
 
 
 
 
225
  st.sidebar.header("System Status")
226
  status_placeholder = st.sidebar.empty()
227
  status_placeholder.info("Initializing...")
228
 
 
229
  if not groq_api_key:
230
  status_placeholder.error("GROQ API Key not configured. Application cannot start.")
231
  st.stop()
232
 
233
  # --- Knowledge Base Loading ---
234
- # This will be cached after the first run
235
  with st.spinner("Knowledge Base is loading... Please wait."):
236
  start_time = time.time()
237
  processed_documents = load_and_process_documents(DOCS_DIR)
@@ -244,17 +185,22 @@ def main():
244
  status_placeholder.error("Failed to create vector store. Application cannot proceed.")
245
  st.stop()
246
 
247
- llm = get_llm(groq_api_key)
 
248
  if not llm:
 
249
  status_placeholder.error("Failed to initialize LLM. Application cannot proceed.")
250
  st.stop()
251
 
252
  end_time = time.time()
 
253
  status_placeholder.success(f"Application Ready! (Loaded in {end_time - start_time:.2f}s)")
254
 
255
- retriever = vector_store.as_retriever(search_kwargs={"k": 5}) # Retrieve top 5 relevant chunks
256
 
257
  # --- Query Input and Response ---
 
 
258
  st.markdown("---")
259
  st.subheader("Ask a question about our documents:")
260
 
@@ -293,7 +239,6 @@ def main():
293
  Answer:
294
  """
295
 
296
- # Use session state to store conversation history if desired, or just last query/response
297
  if "messages" not in st.session_state:
298
  st.session_state.messages = []
299
 
@@ -303,14 +248,14 @@ def main():
303
  if query:
304
  st.session_state.messages.append({"role": "user", "content": query})
305
 
306
- # Determine prompt based on query type (simple keyword check)
307
- # A more sophisticated intent detection could be used here (e.g., another LLM call, classifier)
308
- if "order" in query.lower() and ("status" in query.lower() or "track" in query.lower() or "update" in query.lower() or any(name_part.lower() in query.lower() for name_part in ["customer", "client", "name"])): # Basic check for order status
309
  active_prompt_template = ORDER_STATUS_PROMPT
310
- st.sidebar.info("Using: Order Status Query Mode")
311
  else:
312
  active_prompt_template = GENERAL_QA_PROMPT
313
- st.sidebar.info("Using: General Query Mode")
314
 
315
  rag_chain = get_rag_chain(llm, retriever, active_prompt_template)
316
 
@@ -325,22 +270,16 @@ def main():
325
  else:
326
  st.warning("Please enter a question.")
327
 
328
- # Display chat messages
329
  st.markdown("---")
330
  st.subheader("Response:")
331
  response_area = st.container()
332
- response_area.add_rows([ # Create a container with fixed height and scroll
333
- st.markdown(f"<div class='response-area'>{st.session_state.messages[-1]['content'] if st.session_state.messages and st.session_state.messages[-1]['role'] == 'assistant' else 'Ask a question to see the answer here.'}</div>", unsafe_allow_html=True)
334
- ])
335
-
336
-
337
- # Optional: Display retrieved context for debugging or transparency
338
- # if st.sidebar.checkbox("Show Retrieved Context (for debugging)"):
339
- # if query and vector_store: # Check if query and vector_store exist
340
- # docs = retriever.get_relevant_documents(query)
341
- # st.sidebar.subheader("Retrieved Context:")
342
- # for i, doc in enumerate(docs):
343
- # st.sidebar.text_area(f"Chunk {i+1} (Source: {doc.metadata.get('source', 'N/A')})", doc.page_content, height=150)
344
 
345
  st.sidebar.markdown("---")
346
  st.sidebar.markdown("Built with ❤️ using Streamlit & Langchain & Groq")
 
116
  return None
117
 
118
  @st.cache_resource(show_spinner="Initializing LLM...")
119
+ def get_llm(api_key: str, model_name: str = "llama3-8b-8192"): # UPDATED MODEL
120
  """Initializes the Groq LLM."""
121
  if not api_key:
122
  st.error("GROQ_API_KEY not found! Please set it in your environment variables or a .env file.")
123
  return None
124
  try:
125
+ # Available models (check Groq documentation for the latest):
126
+ # "llama3-8b-8192" (good balance of speed and capability)
127
+ # "llama3-70b-8192" (more powerful, potentially slower)
128
+ # "gemma-7b-it"
129
  llm = ChatGroq(temperature=0, groq_api_key=api_key, model_name=model_name)
130
+ st.sidebar.info(f"LLM Initialized: {model_name}") # Add info about which model is used
131
  return llm
132
  except Exception as e:
133
  st.error(f"Error initializing Groq LLM: {e}")
134
  return None
135
 
136
  # --- RAG Chain Setup ---
137
+ # ... (get_rag_chain function remains the same) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
  # --- Main Application Logic ---
140
  def main():
 
144
  # --- UI Setup ---
145
  st.set_page_config(page_title="Internal Knowledge Base AI", layout="wide", initial_sidebar_state="expanded")
146
 
147
+ # Custom CSS (remains the same)
148
  st.markdown("""
149
  <style>
150
+ # ... (CSS content remains the same) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  </style>
152
  """, unsafe_allow_html=True)
153
 
154
  st.title("📚 Internal Knowledge Base AI 💡")
155
 
156
+ st.sidebar.header("System Settings") # Changed from System Status for clarity
157
+
158
+ # Model selection in sidebar (New Feature)
159
+ available_models = ["llama3-8b-8192", "llama3-70b-8192", "gemma-7b-it"] # Add more as Groq supports them
160
+ selected_model = st.sidebar.selectbox(
161
+ "Select LLM Model:",
162
+ available_models,
163
+ index=available_models.index("llama3-8b-8192") # Default selection
164
+ )
165
+ st.sidebar.markdown("---")
166
  st.sidebar.header("System Status")
167
  status_placeholder = st.sidebar.empty()
168
  status_placeholder.info("Initializing...")
169
 
170
+
171
  if not groq_api_key:
172
  status_placeholder.error("GROQ API Key not configured. Application cannot start.")
173
  st.stop()
174
 
175
  # --- Knowledge Base Loading ---
 
176
  with st.spinner("Knowledge Base is loading... Please wait."):
177
  start_time = time.time()
178
  processed_documents = load_and_process_documents(DOCS_DIR)
 
185
  status_placeholder.error("Failed to create vector store. Application cannot proceed.")
186
  st.stop()
187
 
188
+ # Pass the selected model to get_llm
189
+ llm = get_llm(groq_api_key, model_name=selected_model)
190
  if not llm:
191
+ # Error is already shown by get_llm, but update status_placeholder too
192
  status_placeholder.error("Failed to initialize LLM. Application cannot proceed.")
193
  st.stop()
194
 
195
  end_time = time.time()
196
+ # status_placeholder is updated by get_llm or on success below
197
  status_placeholder.success(f"Application Ready! (Loaded in {end_time - start_time:.2f}s)")
198
 
199
+ retriever = vector_store.as_retriever(search_kwargs={"k": 5})
200
 
201
  # --- Query Input and Response ---
202
+ # ... (rest of the main function remains the same, including prompt templates, query input, button, and response display logic) ...
203
+
204
  st.markdown("---")
205
  st.subheader("Ask a question about our documents:")
206
 
 
239
  Answer:
240
  """
241
 
 
242
  if "messages" not in st.session_state:
243
  st.session_state.messages = []
244
 
 
248
  if query:
249
  st.session_state.messages.append({"role": "user", "content": query})
250
 
251
+ current_model_info = st.sidebar.empty() # Placeholder for current mode info
252
+
253
+ if "order" in query.lower() and ("status" in query.lower() or "track" in query.lower() or "update" in query.lower() or any(name_part.lower() in query.lower() for name_part in ["customer", "client", "name"])):
254
  active_prompt_template = ORDER_STATUS_PROMPT
255
+ current_model_info.info("Mode: Order Status Query")
256
  else:
257
  active_prompt_template = GENERAL_QA_PROMPT
258
+ current_model_info.info("Mode: General Query")
259
 
260
  rag_chain = get_rag_chain(llm, retriever, active_prompt_template)
261
 
 
270
  else:
271
  st.warning("Please enter a question.")
272
 
 
273
  st.markdown("---")
274
  st.subheader("Response:")
275
  response_area = st.container()
276
+ # Ensure response_area is robust against empty messages or incorrect last role
277
+ last_assistant_message = "Ask a question to see the answer here."
278
+ if st.session_state.messages and st.session_state.messages[-1]['role'] == 'assistant':
279
+ last_assistant_message = st.session_state.messages[-1]['content']
280
+
281
+ response_area.markdown(f"<div class='response-area'>{last_assistant_message}</div>", unsafe_allow_html=True)
282
+
 
 
 
 
 
283
 
284
  st.sidebar.markdown("---")
285
  st.sidebar.markdown("Built with ❤️ using Streamlit & Langchain & Groq")