RustSa/linux_documentation_support_chatbot
0
1import streamlit as st2from doc_loader import load_and_index_documents3from qa_system import create_conversational_chain4from ticket_system import create_github_issue5 6# Hardcoded company info for answer signature7COMPANY_NAME = "OpenSource Corp"8COMPANY_EMAIL = "support@example.com"9COMPANY_PHONE = "123-456-7890"10 11st.set_page_config(page_title="Linux Docs Support Bot")12st.title("🐧 Linux Documentation Support Chatbot")13 14# Initialize vector store and QA chain once15if "qa_chain" not in st.session_state:16 with st.spinner("Loading and indexing documents..."):17 vector_store = load_and_index_documents(data_dir="data")18 st.session_state.qa_chain = create_conversational_chain(vector_store)19 if "chat_history" not in st.session_state:20 st.session_state.chat_history = [] # will hold past messages21 22# Chat input23if user_input := st.chat_input("Ask a question about Linux docs:"):24 # Display user message25 st.chat_message("user").write(user_input)26 27 # Get answer from QA chain28 result = st.session_state.qa_chain(29 {30 "question": user_input,31 "chat_history": st.session_state.chat_history32 }33 )34 answer = result["answer"]35 source_docs = result.get("source_documents", [])36 37 # Display assistant answer38 st.chat_message("assistant").write(answer)39 st.write(f"**{COMPANY_NAME}** • {COMPANY_PHONE} • {COMPANY_EMAIL}")40 41 # Show citations under the message42 if source_docs:43 st.markdown("**Citations:**")44 for doc in source_docs:45 src = doc.metadata.get("source", "unknown")46 page = doc.metadata.get("page", "?")47 st.markdown(f"- [source: {src}, page {page}]")48 49 # Append the turn to chat history50 st.session_state.chat_history.append((user_input, answer))51 52 # If answer indicates no relevant info, offer ticket creation53 if any(phrase in answer.lower() for phrase in ["i don’t know", "i don't know", "can't find", "cannot find"]):54 st.warning("It seems this question was not answered from the documentation.")55 with st.expander("Submit a Support Ticket"):56 with st.form("ticket_form", clear_on_submit=True):57 st.write("Please fill out the following fields to create a support ticket:")58 ticket_title = st.text_input("Ticket Title", placeholder="Short summary of the issue")59 user_name = st.text_input("Your Name")60 user_email = st.text_input("Your Email")61 ticket_description = st.text_area("Detailed Description of the Issue")62 submitted = st.form_submit_button("Submit Ticket")63 64 if submitted:65 if not ticket_title or not ticket_description:66 st.error("Title and description are required to submit a ticket.")67 else:68 # Build GitHub issue body69 issue_body = (70 f"**Question:** {user_input}\n"71 f"**Name:** {user_name or 'Anonymous'}\n"72 f"**Email:** {user_email or 'Not provided'}\n"73 f"**Description:**\n{ticket_description}"74 )75 try:76 issue_url = create_github_issue(ticket_title, issue_body)77 st.success(f"Ticket submitted successfully! [View Issue]({issue_url})")78 except Exception as e:79 st.error(f"Failed to create ticket: {e}")