Update app.py
Browse files
app.py
CHANGED
@@ -106,7 +106,7 @@ def clear_conversation_context():
|
|
106 |
# Clear the context memory
|
107 |
st.session_state.context_memory = []
|
108 |
|
109 |
-
|
110 |
|
111 |
def initialize_pipeline():
|
112 |
"""Initialize RAG pipeline with conversation memory support"""
|
@@ -412,44 +412,49 @@ 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 |
-
if isinstance(content, dict) and "answer" in content:
|
421 |
-
rag_content = content["answer"]
|
422 |
-
else:
|
423 |
-
rag_content = content
|
424 |
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
433 |
-
result = st.session_state.pipeline.process_query(
|
434 |
-
query=user_query,
|
435 |
-
conversation_history=rag_conversation_history
|
436 |
-
)
|
437 |
-
|
438 |
-
# Create response with same structure as main()
|
439 |
-
response_dict = {
|
440 |
-
"answer": result.get("answer", ""),
|
441 |
-
"documents": result.get("relevant_docs", [])
|
442 |
-
}
|
443 |
|
444 |
-
#
|
445 |
-
|
|
|
446 |
|
447 |
-
#
|
448 |
-
st.
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
453 |
|
454 |
except Exception as e:
|
455 |
error_msg = f"❌ เกิดข้อผิดพลาด: {str(e)}"
|
|
|
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"""
|
|
|
412 |
|
413 |
# Convert the Streamlit chat history format to RAG format
|
414 |
rag_conversation_history = []
|
415 |
+
|
416 |
+
if not st.session_state.get('context_cleared', False):
|
417 |
+
for role, content in st.session_state.chat_history:
|
418 |
+
# Map Streamlit's role names to the format used in the RAG system
|
419 |
+
rag_role = "user" if role == "user" else "assistant"
|
|
|
|
|
|
|
|
|
420 |
|
421 |
+
# Handle content based on type
|
422 |
+
if isinstance(content, dict) and "answer" in content:
|
423 |
+
rag_content = content["answer"]
|
424 |
+
else:
|
425 |
+
rag_content = content
|
426 |
+
|
427 |
+
rag_conversation_history.append({"role": rag_role, "content": rag_content})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
|
429 |
+
# Store conversation context in session state
|
430 |
+
if 'context_memory' not in st.session_state:
|
431 |
+
st.session_state.context_memory = []
|
432 |
|
433 |
+
# Process query with conversation history
|
434 |
+
with st.spinner("🔍 กำลังค้นหาคำตอบ..."):
|
435 |
+
result = st.session_state.pipeline.process_query(
|
436 |
+
query=user_query,
|
437 |
+
conversation_history=rag_conversation_history
|
438 |
+
)
|
439 |
+
|
440 |
+
# Create response with same structure as main()
|
441 |
+
response_dict = {
|
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)}"
|