Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -225,21 +225,18 @@ answer = "" # Initialize ai_response with a default value
|
|
225 |
|
226 |
|
227 |
|
228 |
-
# Update the ask_alans_ai function to handle Claude Sonnet
|
229 |
def ask_alans_ai(query, vectordb, chat_history, aoc_qa):
|
230 |
try:
|
231 |
# Use the ConversationalRetrievalChain directly
|
232 |
result = aoc_qa.invoke({"question": query})
|
233 |
answer = result["answer"]
|
234 |
-
source_documents = result.get("source_documents", [])
|
235 |
-
|
236 |
-
# You can use source_documents if needed, e.g., to display sources
|
237 |
|
238 |
chat_history.append((query, answer))
|
239 |
-
return answer
|
240 |
except Exception as e:
|
241 |
st.error(f"An error occurred: {str(e)}")
|
242 |
-
return "I'm sorry, but I encountered an error while processing your request. Please try again later."
|
243 |
|
244 |
|
245 |
|
@@ -647,11 +644,13 @@ def main():
|
|
647 |
# Submit button
|
648 |
submitted = st.form_submit_button(label="Ask", use_container_width=True)
|
649 |
|
|
|
|
|
|
|
650 |
if submitted and message:
|
651 |
-
# Process the query and get the response
|
652 |
with st.spinner('Thinking...'):
|
653 |
-
response = ask_alans_ai(message, vectordb, st.session_state.chat_history, aoc_qa)
|
654 |
-
|
655 |
|
656 |
|
657 |
|
@@ -669,14 +668,32 @@ def main():
|
|
669 |
</div>
|
670 |
""", unsafe_allow_html=True)
|
671 |
|
672 |
-
#
|
673 |
st.markdown(f"""
|
674 |
-
|
675 |
<img src='data:image/png;base64,{ci_icon}' style='width: 50px; height: 50px; margin-right: 10px;'>
|
676 |
-
<span>{
|
677 |
</div>
|
678 |
""", unsafe_allow_html=True)
|
679 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
680 |
# JavaScript to scroll to the latest answer
|
681 |
if st.session_state.chat_history:
|
682 |
latest_answer_id = f"answer-{len(st.session_state.chat_history) - 1}"
|
@@ -693,8 +710,10 @@ def main():
|
|
693 |
|
694 |
|
695 |
# Your combined string with the current date included
|
696 |
-
combined_string = f"Question: {message}\n\nAnswer: {answer}\n\nDate: {date_string}\n\
|
697 |
-
|
|
|
|
|
698 |
|
699 |
message_clean = clean_string(message)
|
700 |
answer_clean = clean_string(answer)
|
@@ -706,9 +725,19 @@ def main():
|
|
706 |
answer_clean = answer_clean[:max_length]
|
707 |
date_string_clean = date_string_clean[:max_length]
|
708 |
|
709 |
-
|
710 |
-
|
711 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
712 |
|
713 |
# Create and display the copy button only if answer has content
|
714 |
if answer:
|
|
|
225 |
|
226 |
|
227 |
|
|
|
228 |
def ask_alans_ai(query, vectordb, chat_history, aoc_qa):
|
229 |
try:
|
230 |
# Use the ConversationalRetrievalChain directly
|
231 |
result = aoc_qa.invoke({"question": query})
|
232 |
answer = result["answer"]
|
233 |
+
source_documents = result.get("source_documents", []) # Use .get() with a default empty list
|
|
|
|
|
234 |
|
235 |
chat_history.append((query, answer))
|
236 |
+
return answer, source_documents # Return both answer and source_documents
|
237 |
except Exception as e:
|
238 |
st.error(f"An error occurred: {str(e)}")
|
239 |
+
return "I'm sorry, but I encountered an error while processing your request. Please try again later.", [] # Return empty list as source_documents
|
240 |
|
241 |
|
242 |
|
|
|
644 |
# Submit button
|
645 |
submitted = st.form_submit_button(label="Ask", use_container_width=True)
|
646 |
|
647 |
+
answer = "" # Initialize answer
|
648 |
+
source_documents = [] # Initialize source_documents as an empty list
|
649 |
+
|
650 |
if submitted and message:
|
|
|
651 |
with st.spinner('Thinking...'):
|
652 |
+
response, source_documents = ask_alans_ai(message, vectordb, st.session_state.chat_history, aoc_qa)
|
653 |
+
|
654 |
|
655 |
|
656 |
|
|
|
668 |
</div>
|
669 |
""", unsafe_allow_html=True)
|
670 |
|
671 |
+
# Display the response
|
672 |
st.markdown(f"""
|
673 |
+
<div id="{answer_id}" style="display: flex; align-items: flex-start; margin-bottom: 20px;">
|
674 |
<img src='data:image/png;base64,{ci_icon}' style='width: 50px; height: 50px; margin-right: 10px;'>
|
675 |
+
<span>{response}</span>
|
676 |
</div>
|
677 |
""", unsafe_allow_html=True)
|
678 |
|
679 |
+
# Display metadata for unique source documents
|
680 |
+
if source_documents:
|
681 |
+
unique_sources = []
|
682 |
+
seen_urls = set()
|
683 |
+
|
684 |
+
for doc in source_documents:
|
685 |
+
url = doc.metadata.get('url', 'N/A')
|
686 |
+
if url not in seen_urls:
|
687 |
+
unique_sources.append(doc)
|
688 |
+
seen_urls.add(url)
|
689 |
+
|
690 |
+
if unique_sources:
|
691 |
+
st.markdown("### Sources:")
|
692 |
+
for doc in unique_sources:
|
693 |
+
st.markdown(f"""
|
694 |
+
- [{doc.metadata.get('title', 'No title available')}]({doc.metadata.get('url', '#')})
|
695 |
+
""")
|
696 |
+
|
697 |
# JavaScript to scroll to the latest answer
|
698 |
if st.session_state.chat_history:
|
699 |
latest_answer_id = f"answer-{len(st.session_state.chat_history) - 1}"
|
|
|
710 |
|
711 |
|
712 |
# Your combined string with the current date included
|
713 |
+
combined_string = f"Question: {message}\n\nAnswer: {answer}\n\nDate: {date_string}\n\n"
|
714 |
+
for doc in source_documents:
|
715 |
+
combined_string += f"Source: {doc.metadata.get('title', 'N/A')} - {doc.metadata.get('url', 'N/A')}\n"
|
716 |
+
combined_string += "https://www.citizensinformation.ie/"
|
717 |
|
718 |
message_clean = clean_string(message)
|
719 |
answer_clean = clean_string(answer)
|
|
|
725 |
answer_clean = answer_clean[:max_length]
|
726 |
date_string_clean = date_string_clean[:max_length]
|
727 |
|
728 |
+
data_to_append = [
|
729 |
+
message_clean,
|
730 |
+
answer_clean,
|
731 |
+
date_string,
|
732 |
+
str(ai_temp),
|
733 |
+
st.session_state['session_id'],
|
734 |
+
st.session_state['selected_model'],
|
735 |
+
str(k_value),
|
736 |
+
selected_directory,
|
737 |
+
selected_search_type,
|
738 |
+
", ".join([doc.metadata.get('title', 'N/A') for doc in source_documents]),
|
739 |
+
", ".join([doc.metadata.get('url', 'N/A') for doc in source_documents])
|
740 |
+
]
|
741 |
|
742 |
# Create and display the copy button only if answer has content
|
743 |
if answer:
|