Update app.py
Browse files
app.py
CHANGED
@@ -106,8 +106,6 @@ def clear_conversation_context():
|
|
106 |
# Clear the context memory
|
107 |
st.session_state.context_memory = []
|
108 |
|
109 |
-
st.session_state.context_cleared = True
|
110 |
-
|
111 |
def initialize_pipeline():
|
112 |
"""Initialize RAG pipeline with conversation memory support"""
|
113 |
try:
|
@@ -412,49 +410,45 @@ def handle_submit(user_query: str):
|
|
412 |
|
413 |
# Convert the Streamlit chat history format to RAG format
|
414 |
rag_conversation_history = []
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
|
|
|
|
|
|
|
|
|
|
420 |
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
|
|
|
|
|
|
|
|
|
|
428 |
|
429 |
-
#
|
430 |
-
|
431 |
-
|
|
|
|
|
432 |
|
433 |
-
#
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
"answer": result.get("answer", ""),
|
443 |
-
"documents": result.get("relevant_docs", [])
|
444 |
-
}
|
445 |
-
|
446 |
-
# Update chat history and context
|
447 |
-
add_to_history("assistant", response_dict)
|
448 |
-
|
449 |
-
# Add this exchange to context memory for future reference
|
450 |
-
st.session_state.context_memory.append({
|
451 |
-
"query": user_query,
|
452 |
-
"response": response_dict["answer"],
|
453 |
-
"timestamp": datetime.now().isoformat()
|
454 |
-
})
|
455 |
-
else:
|
456 |
-
# If context was cleared, only include the current query
|
457 |
-
st.session_state.context_cleared = False # Reset flag
|
458 |
|
459 |
except Exception as e:
|
460 |
error_msg = f"❌ เกิดข้อผิดพลาด: {str(e)}"
|
@@ -482,7 +476,7 @@ def create_chat_input():
|
|
482 |
placeholder="เช่น: วิชาเลือกมีอะไรบ้าง?"
|
483 |
)
|
484 |
|
485 |
-
col1, col2
|
486 |
|
487 |
with col1:
|
488 |
submitted = st.form_submit_button(
|
@@ -498,23 +492,12 @@ def create_chat_input():
|
|
498 |
use_container_width=True
|
499 |
)
|
500 |
|
501 |
-
with col3:
|
502 |
-
clear_context_button = st.form_submit_button(
|
503 |
-
"🧠 ล้างบริบทสนทนา",
|
504 |
-
type="secondary",
|
505 |
-
use_container_width=True
|
506 |
-
)
|
507 |
-
|
508 |
if submitted:
|
509 |
handle_submit(query)
|
510 |
|
511 |
if clear_button:
|
512 |
st.session_state.chat_history = []
|
513 |
st.rerun()
|
514 |
-
|
515 |
-
if clear_context_button:
|
516 |
-
clear_conversation_context()
|
517 |
-
st.info("ล้างบริบทสนทนาแล้ว แต่ยังคงประวัติการแสดงสนทนา")
|
518 |
|
519 |
def main():
|
520 |
# Page config
|
|
|
106 |
# Clear the context memory
|
107 |
st.session_state.context_memory = []
|
108 |
|
|
|
|
|
109 |
def initialize_pipeline():
|
110 |
"""Initialize RAG pipeline with conversation memory support"""
|
111 |
try:
|
|
|
410 |
|
411 |
# Convert the Streamlit chat history format to RAG format
|
412 |
rag_conversation_history = []
|
413 |
+
|
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 |
+
|
418 |
+
# Handle content based on type
|
419 |
+
if isinstance(content, dict) and "answer" in content:
|
420 |
+
rag_content = content["answer"]
|
421 |
+
else:
|
422 |
+
rag_content = content
|
423 |
|
424 |
+
rag_conversation_history.append({"role": rag_role, "content": rag_content})
|
425 |
+
|
426 |
+
# Store conversation context in session state
|
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
|
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 |
+
|
446 |
+
# Add this exchange to context memory for future reference
|
447 |
+
st.session_state.context_memory.append({
|
448 |
+
"query": user_query,
|
449 |
+
"response": response_dict["answer"],
|
450 |
+
"timestamp": datetime.now().isoformat()
|
451 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
452 |
|
453 |
except Exception as e:
|
454 |
error_msg = f"❌ เกิดข้อผิดพลาด: {str(e)}"
|
|
|
476 |
placeholder="เช่น: วิชาเลือกมีอะไรบ้าง?"
|
477 |
)
|
478 |
|
479 |
+
col1, col2 = st.columns([7, 3])
|
480 |
|
481 |
with col1:
|
482 |
submitted = st.form_submit_button(
|
|
|
492 |
use_container_width=True
|
493 |
)
|
494 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
495 |
if submitted:
|
496 |
handle_submit(query)
|
497 |
|
498 |
if clear_button:
|
499 |
st.session_state.chat_history = []
|
500 |
st.rerun()
|
|
|
|
|
|
|
|
|
501 |
|
502 |
def main():
|
503 |
# Page config
|