JirasakJo commited on
Commit
9314e12
·
verified ·
1 Parent(s): 4626d8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -394,7 +394,7 @@ if 'context_memory' not in st.session_state:
394
  st.session_state.context_memory = []
395
 
396
  def handle_submit(user_query: str):
397
- """Enhanced query handling with conversation history"""
398
  if not user_query:
399
  st.warning("⚠️ กรุณาระบุคำถาม")
400
  return
@@ -410,8 +410,11 @@ def handle_submit(user_query: str):
410
  add_to_history("user", user_query)
411
 
412
  # Convert the Streamlit chat history format to RAG format
 
413
  rag_conversation_history = []
414
- for role, content in st.session_state.chat_history:
 
 
415
  # Map Streamlit's role names to the format used in the RAG system
416
  rag_role = "user" if role == "user" else "assistant"
417
 
@@ -427,8 +430,29 @@ def handle_submit(user_query: str):
427
  if 'context_memory' not in st.session_state:
428
  st.session_state.context_memory = []
429
 
430
- # Process query with conversation history
431
  with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  result = st.session_state.pipeline.process_query(
433
  query=user_query,
434
  conversation_history=rag_conversation_history
 
394
  st.session_state.context_memory = []
395
 
396
  def handle_submit(user_query: str):
397
+ """Enhanced query handling with improved conversation history tracking"""
398
  if not user_query:
399
  st.warning("⚠️ กรุณาระบุคำถาม")
400
  return
 
410
  add_to_history("user", user_query)
411
 
412
  # Convert the Streamlit chat history format to RAG format
413
+ # Include more context history (up to 5 previous exchanges)
414
  rag_conversation_history = []
415
+ history_to_include = st.session_state.chat_history[-11:] if len(st.session_state.chat_history) > 10 else st.session_state.chat_history
416
+
417
+ for role, content in history_to_include:
418
  # Map Streamlit's role names to the format used in the RAG system
419
  rag_role = "user" if role == "user" else "assistant"
420
 
 
430
  if 'context_memory' not in st.session_state:
431
  st.session_state.context_memory = []
432
 
433
+ # Process query with improved conversation history
434
  with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
435
+ # Add debug logging to verify context
436
+ print(f"Processing query with {len(rag_conversation_history)} context messages")
437
+
438
+ # Add special handling for reference questions
439
+ reference_keywords = ["ก่อนหน้านี้", "ก่อนหน้า", "ที่ผ่านมา", "คำถามก่อนหน้า", "คำถามที่แล้ว",
440
+ "previous", "earlier", "before", "last time", "last question"]
441
+
442
+ is_reference_question = any(keyword in user_query.lower() for keyword in reference_keywords)
443
+
444
+ # If this is a reference question, emphasize context in the query
445
+ if is_reference_question and len(rag_conversation_history) >= 3:
446
+ # Extract the previous user question (should be 2 positions back)
447
+ previous_questions = [msg["content"] for msg in rag_conversation_history[:-2]
448
+ if msg["role"] == "user"]
449
+
450
+ if previous_questions:
451
+ prev_question = previous_questions[-1]
452
+ enhanced_query = f"คำถามนี้อ้างอิงถึงคำถามก่อนหน้า '{prev_question}' โปรดพิจารณาบริบทนี้ในการตอบ: {user_query}"
453
+ print(f"Enhanced reference query: {enhanced_query}")
454
+ user_query = enhanced_query
455
+
456
  result = st.session_state.pipeline.process_query(
457
  query=user_query,
458
  conversation_history=rag_conversation_history