from langchain_community.document_loaders import WebBaseLoader from langchain_groq import ChatGroq from langchain_core.prompts import PromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda from dotenv import load_dotenv import streamlit as st import os import validators import time from urllib.parse import urlparse # Load environment variables load_dotenv() # Page configuration st.set_page_config( page_title="Web Q&A ChatBot", page_icon="🤖", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) def initialize_model(): """Initialize the ChatGroq model""" try: model = ChatGroq( model="llama3-8b-8192", temperature=0.7, max_tokens=1000 ) return model except Exception as e: st.error(f"Error initializing model: {str(e)}") return None def create_prompt_template(): """Create the prompt template""" return PromptTemplate( template="""You are a helpful AI assistant that answers questions based on web content. Question: {question} Web Content: {data} Instructions: - Provide a clear, accurate answer based on the web content - If the information is not available in the content, mention that - Be concise but comprehensive - Use bullet points when listing multiple items Answer:""", input_variables=["question", "data"] ) def extract_data(inputs): """Extract data from URL with better error handling""" try: url = inputs["url"] question = inputs["question"] # Validate URL if not validators.url(url): return {"question": question, "data": "Invalid URL format. Please provide a valid URL."} # Load web content with st.spinner("Loading web content..."): loader = WebBaseLoader(web_path=url) documents = loader.load() if not documents: return {"question": question, "data": "No content found at the provided URL."} content = documents[0].page_content # Limit content length to avoid token limits if len(content) > 12000: content = content[:10000] + "... (content truncated)" return {"question": question, "data": content} except Exception as e: error_msg = f"Error loading URL: {str(e)}" return {"question": inputs.get("question", ""), "data": error_msg} def main(): # Header st.markdown("
Ask questions about any webpage content
", unsafe_allow_html=True) # Sidebar for configuration with st.sidebar: st.header("⚙️ Configuration") # Model settings st.subheader("Model Settings") temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1, help="Controls randomness in responses") max_tokens = st.slider("Max Tokens", 100, 2000, 1000, 100, help="Maximum response length") # API Key status st.subheader("API Status") groq_api_key = os.getenv("GROQ_API_KEY") if groq_api_key: st.success("✅ GROQ API Key loaded") else: st.error("❌ GROQ API Key not found") st.info("Please set your GROQ_API_KEY in the .env file") # Instructions st.subheader("📖 How to Use") st.markdown(""" 1. Enter a valid webpage URL 2. Type your question about the content 3. Click 'Get Answer' to analyze 4. View the AI-generated response """) # Example URLs st.subheader("📝 Example URLs") example_urls = [ "https://www.wikipedia.org/wiki/Artificial_intelligence", "https://news.ycombinator.com/", "https://www.github.com/", ] for url in example_urls: if st.button(f"Use: {urlparse(url).netloc}", key=url): st.session_state.example_url = url # Main content area col1, col2 = st.columns([3, 1]) with col1: # URL input url_input = st.text_input( "🔗 Enter Website URL:", value=st.session_state.get('example_url', ''), placeholder="https://example.com", help="Enter the complete URL including https://" ) # Question input question_input = st.text_area( "❓ Enter Your Question:", placeholder="What is this webpage about? What are the main features? etc.", height=100, help="Ask specific questions about the webpage content" ) with col2: st.markdown("Built with ❤️ using Streamlit, LangChain, and GROQ
", unsafe_allow_html=True ) # Store chat history in session state if 'chat_history' not in st.session_state: st.session_state.chat_history = [] # Add chat history section if st.session_state.chat_history: st.markdown("## 📚 Recent Queries") for i, (url, question, answer) in enumerate(st.session_state.chat_history[-3:]): with st.expander(f"Query {len(st.session_state.chat_history) - i}: {question[:50]}..."): st.write(f"**URL:** {url}") st.write(f"**Question:** {question}") st.write(f"**Answer:** {answer[:200]}...") if __name__ == "__main__": main()