JirasakJo commited on
Commit
7e11155
·
verified ·
1 Parent(s): 134477c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -429,17 +429,27 @@ def handle_submit(user_query: str):
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
435
- )
 
 
 
 
 
 
436
 
437
- # Create response with same structure as main()
438
  response_dict = {
439
  "answer": result.get("answer", ""),
440
- "documents": result.get("relevant_docs", [])
441
  }
442
 
 
 
 
 
443
  # Update chat history and context
444
  add_to_history("assistant", response_dict)
445
 
@@ -496,6 +506,7 @@ def create_chat_input():
496
  handle_submit(query)
497
 
498
  if clear_button:
 
499
  st.session_state.chat_history = []
500
  st.rerun()
501
 
 
429
 
430
  # Process query with conversation history
431
  with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
432
+
433
+ if len(st.session_state.context_memory) > 5:
434
+ st.session_state.context_memory.pop(0)
435
+
436
+ # Build query with context
437
+ query_with_context = "\n".join(
438
+ [f"Q: {qa['query']}\nA: {qa['answer']}" for qa in st.session_state.context_memory]
439
+ ) + f"\nQ: {user_query}"
440
+
441
+ result = st.session_state.pipeline.process_query(query_with_context)
442
 
443
+ # Create response dictionary with answer and documents
444
  response_dict = {
445
  "answer": result.get("answer", ""),
446
+ "documents": result.get("documents", [])
447
  }
448
 
449
+ # Update chat history and context
450
+ st.session_state.chat_history.append(("assistant", response_dict))
451
+ st.session_state.context_memory.append({"query": user_query, "answer": response_dict})
452
+
453
  # Update chat history and context
454
  add_to_history("assistant", response_dict)
455
 
 
506
  handle_submit(query)
507
 
508
  if clear_button:
509
+ st.session_state.context_memory = []
510
  st.session_state.chat_history = []
511
  st.rerun()
512