import streamlit as st from utils.retriever import retrieve_paragraphs from utils.generator import build_messages, _call_llm from utils.utils import meetings_list, countries_list, projects_list import ast import time import asyncio import re import logging logging.basicConfig(level=logging.INFO) # st.set_page_config(layout="wide") st.markdown( """
""", unsafe_allow_html=True ) st.markdown( """ """, unsafe_allow_html=True ) # Add vertical spacing between banner and help text st.markdown("", unsafe_allow_html=True) # Help text (static) st.markdown("""Welcome to your chatbot research assistant. It helps you find and summarize specific decisions and annexes, and can also answer general questions about the text. For transparency, it provides references with links to the relevant decisions and annexes, so you can easily verify the sources. \n While this chatbot was developed with care, we recommend double-checking the links to gain a deeper understanding of the material.
""", unsafe_allow_html=True) # Add vertical spacing between help text and question input st.markdown("", unsafe_allow_html=True) ########### Function for getting response ####################### def chat_response(query, filter_metadata=None): """Generate chat response based on method and inputs""" try: retrieved_paragraphs = retrieve_paragraphs(query, filter_metadata=filter_metadata) context_retrieved = ast.literal_eval(retrieved_paragraphs) # Build list of only content, no metadata context_retrieved_formatted = "||".join(doc['answer'] for doc in context_retrieved) context_retrieved_lst = [doc['answer'] for doc in context_retrieved] logging.info("Context Retrieval done") logging.info(f"Content {context_retrieved}") messages = build_messages(query, context_retrieved_lst) answer = asyncio.run(_call_llm(messages)) return answer, context_retrieved except Exception as e: error_message = f"Error processing request: {str(e)}" return error_message ############## UI related functions ##################### def reset_page(): """ Reset pagination back to the first page; used as on_change callback. """ st.session_state["page"] = 1 def contruct_metadata_filter(): filter_metadata = {} if st.session_state['meetings_filter'] != 'All': filter_metadata['meeting_id'] = st.session_state['meetings_filter'] ## need to change the filter for coutnry and project tolist if st.session_state['country_filter'] != 'All': filter_metadata['Countries'] = st.session_state['country_filter'] if st.session_state['project_filter'] != 'All': filter_metadata['Projects'] = st.session_state['project_filter'] logging.info(f"contructed metadata_filter {filter_metadata}") return filter_metadata def render_sources(chunks, query): # 11.7. Render each result chunk st.subheader("Sources") st.write("======================================") start_idx = 0 for idx, doc in enumerate(chunks, start=start_idx + 1): meta = doc.get('answer_metadata', {}) title = meta.get('Decision Number', 'Unknown Project') agencies = meta.get('Agencies', 'Unknown Agencies') country = meta.get('country', 'Unknown Country') snippet = doc.get('answer', '') preview = snippet.split(maxsplit=90)[:90] remainder = snippet[len(" ".join(preview)):] # Wrap markdown in a div with limited width st.markdown(f"""Agencies: {agencies} | Country: {country}
{" ".join(preview)}