santu24 commited on
Commit
3c59d8a
·
verified ·
1 Parent(s): 5ffa752

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -226,11 +226,7 @@ def rag_index():
226
  @app.route("/rag/query", methods=["POST"])
227
  def rag_query():
228
  """
229
- Process user input with an in-depth approach:
230
- - Dynamically extract brands, product types, and price limits from the query.
231
- - Perform precise DB filtering based on extracted parameters.
232
- - Construct a structured prompt for Gemini using the filtered results.
233
- - Parse Gemini's response and update the conversation history.
234
  """
235
  if "rag_chat" not in session:
236
  session["rag_chat"] = []
@@ -239,23 +235,29 @@ def rag_query():
239
  if not user_input:
240
  return redirect(url_for("rag_index"))
241
 
 
242
  session["rag_chat"].append(("user", user_input))
243
 
 
244
  brand_keyword, product_type, price_val = extract_query_parameters(user_input)
245
-
246
  matched_items = filter_database(brand_keyword, product_type, price_val)
247
-
248
  db_context = build_db_context(matched_items, brand_keyword, product_type, price_val)
249
-
250
  conversation_text = construct_prompt(session["rag_chat"], db_context)
251
-
 
252
  gemini_response = gemini_generate_content(
253
  api_key=GEMINI_API_KEY,
254
  conversation_text=conversation_text
255
  )
256
 
 
257
  session["rag_chat"].append(("assistant", gemini_response))
258
- return redirect(url_for("rag_index"))
 
 
 
 
 
259
 
260
  def extract_query_parameters(user_query):
261
  """
 
226
  @app.route("/rag/query", methods=["POST"])
227
  def rag_query():
228
  """
229
+ Process user input with an in-depth approach.
 
 
 
 
230
  """
231
  if "rag_chat" not in session:
232
  session["rag_chat"] = []
 
235
  if not user_input:
236
  return redirect(url_for("rag_index"))
237
 
238
+ # Add user query to chat history
239
  session["rag_chat"].append(("user", user_input))
240
 
241
+ # Extract and process the query
242
  brand_keyword, product_type, price_val = extract_query_parameters(user_input)
 
243
  matched_items = filter_database(brand_keyword, product_type, price_val)
 
244
  db_context = build_db_context(matched_items, brand_keyword, product_type, price_val)
 
245
  conversation_text = construct_prompt(session["rag_chat"], db_context)
246
+
247
+ # Get response from Gemini API
248
  gemini_response = gemini_generate_content(
249
  api_key=GEMINI_API_KEY,
250
  conversation_text=conversation_text
251
  )
252
 
253
+ # Add assistant's response to chat history
254
  session["rag_chat"].append(("assistant", gemini_response))
255
+
256
+ # Save session to persist the chat history
257
+ session.modified = True
258
+
259
+ # Render the chat page with updated history
260
+ return render_template("rag.html", chat_history=session["rag_chat"])
261
 
262
  def extract_query_parameters(user_query):
263
  """