|
| 1 | +import streamlit as st |
| 2 | +import os |
| 3 | +from praisonaiagents import Agent |
| 4 | +from dotenv import load_dotenv |
| 5 | + |
| 6 | +# Load environment variables |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | +# Page configuration |
| 10 | +st.set_page_config( |
| 11 | + page_title="Agentic RAG with GPT-5", |
| 12 | + page_icon="🧠", |
| 13 | + layout="wide" |
| 14 | +) |
| 15 | + |
| 16 | +# Main title and description |
| 17 | +st.title("🧠 Agentic RAG with GPT-5") |
| 18 | +st.markdown(""" |
| 19 | +This app demonstrates an intelligent AI agent that: |
| 20 | +1. **Answers** your questions clearly and concisely using GPT-5 |
| 21 | +
|
| 22 | +⚠️ **Note**: Knowledge base functionality is temporarily disabled due to a compatibility issue with the current version of PraisonAI Agents. |
| 23 | +
|
| 24 | +Enter your OpenAI API key in the sidebar to get started! |
| 25 | +""") |
| 26 | + |
| 27 | +# Sidebar for API key and settings |
| 28 | +with st.sidebar: |
| 29 | + st.header("🔧 Configuration") |
| 30 | + |
| 31 | + # OpenAI API Key |
| 32 | + openai_key = st.text_input( |
| 33 | + "OpenAI API Key", |
| 34 | + type="password", |
| 35 | + value=os.getenv("OPENAI_API_KEY", ""), |
| 36 | + help="Get your key from https://platform.openai.com/" |
| 37 | + ) |
| 38 | + |
| 39 | + # Add URLs to knowledge base |
| 40 | + st.subheader("🌐 Add Knowledge Sources") |
| 41 | + st.info("⚠️ Knowledge base functionality is temporarily disabled due to compatibility issues.") |
| 42 | + new_url = st.text_input( |
| 43 | + "Add URL", |
| 44 | + placeholder="https://docs.praisonai.com/introduction", |
| 45 | + help="Enter a URL to add to the knowledge base (currently disabled)" |
| 46 | + ) |
| 47 | + |
| 48 | + if st.button("➕ Add URL", type="primary", disabled=True): |
| 49 | + st.info("Knowledge base functionality is temporarily disabled.") |
| 50 | + |
| 51 | +# Check if API key is provided |
| 52 | +if openai_key: |
| 53 | + # Initialize knowledge base (cached to avoid reloading) |
| 54 | + @st.cache_resource(show_spinner="📚 Loading knowledge base...") |
| 55 | + def load_knowledge() -> list: |
| 56 | + """Load and initialize the knowledge base with default URL""" |
| 57 | + return ["https://docs.praisonai.com/introduction/agents.md"] # Default URL |
| 58 | + |
| 59 | + # Initialize agent (cached to avoid reloading) |
| 60 | + @st.cache_resource(show_spinner="🤖 Loading agent...") |
| 61 | + def load_agent(_knowledge: list) -> Agent: |
| 62 | + """Create an agent with reasoning capabilities""" |
| 63 | + # Note: Temporarily removed knowledge parameter to avoid rerank error |
| 64 | + # TODO: Re-enable when PraisonAI Agents knowledge issue is resolved |
| 65 | + return Agent( |
| 66 | + name="Knowledge Agent", |
| 67 | + instructions=[ |
| 68 | + "You are a helpful AI assistant. Answer questions based on your general knowledge.", |
| 69 | + "Provide clear, well-structured answers in markdown format.", |
| 70 | + "Use proper markdown formatting with headers, lists, and emphasis where appropriate.", |
| 71 | + "Structure your response with clear sections and bullet points when helpful.", |
| 72 | + ], |
| 73 | + llm="gpt-5-nano |
| 74 | + markdown=True, |
| 75 | + verbose=True |
| 76 | + ) |
| 77 | + |
| 78 | + # Load knowledge and agent |
| 79 | + knowledge = load_knowledge() |
| 80 | + agent = load_agent(knowledge) |
| 81 | + |
| 82 | + # Display current URLs in knowledge base |
| 83 | + if knowledge: |
| 84 | + st.sidebar.subheader("📚 Current Knowledge Sources") |
| 85 | + for i, url in enumerate(knowledge, 1): |
| 86 | + st.sidebar.markdown(f"{i}. {url}") |
| 87 | + |
| 88 | + # Handle URL additions |
| 89 | + if hasattr(st.session_state, 'urls_to_add') and st.session_state.urls_to_add: |
| 90 | + with st.spinner("📥 Loading new documents..."): |
| 91 | + knowledge.append(st.session_state.urls_to_add) |
| 92 | + # Reinitialize agent with new knowledge |
| 93 | + agent = load_agent(knowledge) |
| 94 | + st.success(f"✅ Added: {st.session_state.urls_to_add}") |
| 95 | + del st.session_state.urls_to_add |
| 96 | + st.rerun() |
| 97 | + |
| 98 | + # Main query section |
| 99 | + st.divider() |
| 100 | + st.subheader("🤔 Ask a Question") |
| 101 | + |
| 102 | + # Suggested prompts |
| 103 | + st.markdown("**Try these prompts:**") |
| 104 | + col1, col2, col3 = st.columns(3) |
| 105 | + with col1: |
| 106 | + if st.button("What is PraisonAI?", use_container_width=True): |
| 107 | + st.session_state.query = "What is PraisonAI and how do Agents work?" |
| 108 | + with col2: |
| 109 | + if st.button("Teams in PraisonAI", use_container_width=True): |
| 110 | + st.session_state.query = "What are Teams in PraisonAI and how do they work?" |
| 111 | + with col3: |
| 112 | + if st.button("Build RAG system", use_container_width=True): |
| 113 | + st.session_state.query = "Give me a step-by-step guide to building a RAG system." |
| 114 | + |
| 115 | + # Query input |
| 116 | + query = st.text_area( |
| 117 | + "Your question:", |
| 118 | + value=st.session_state.get("query", "What are AI Agents?"), |
| 119 | + height=100, |
| 120 | + help="Ask anything about the loaded knowledge sources" |
| 121 | + ) |
| 122 | + |
| 123 | + # Run button |
| 124 | + if st.button("🚀 Get Answer", type="primary"): |
| 125 | + if query: |
| 126 | + # Create container for answer |
| 127 | + st.markdown("### 💡 Answer") |
| 128 | + answer_container = st.container() |
| 129 | + answer_placeholder = answer_container.empty() |
| 130 | + |
| 131 | + # Get the agent's response |
| 132 | + with st.spinner("🔍 Searching and generating answer..."): |
| 133 | + try: |
| 134 | + st.info("🤖 Agent is processing your question...") |
| 135 | + response = agent.start(query) |
| 136 | + answer_placeholder.markdown( |
| 137 | + response, |
| 138 | + unsafe_allow_html=True |
| 139 | + ) |
| 140 | + except Exception as e: |
| 141 | + st.error(f"Error getting response: {str(e)}") |
| 142 | + st.error(f"Error type: {type(e).__name__}") |
| 143 | + st.error(f"Full error details: {repr(e)}") |
| 144 | + # Try to provide a helpful response |
| 145 | + answer_placeholder.markdown(""" |
| 146 | + **⚠️ Error occurred while processing your question.** |
| 147 | + |
| 148 | + This might be due to: |
| 149 | + - Knowledge base configuration issues |
| 150 | + - Model access problems |
| 151 | + - Network connectivity issues |
| 152 | + |
| 153 | + Please try again or check your OpenAI API key. |
| 154 | + """) |
| 155 | + else: |
| 156 | + st.error("Please enter a question") |
| 157 | + |
| 158 | +else: |
| 159 | + # Show instructions if API key is missing |
| 160 | + st.info(""" |
| 161 | + 👋 **Welcome! To use this app, you need:** |
| 162 | + |
| 163 | + - **OpenAI API Key** (set it in the sidebar) |
| 164 | + - Sign up at [platform.openai.com](https://platform.openai.com/) |
| 165 | + - Generate a new API key |
| 166 | + |
| 167 | + Once you enter the key, the app will load the knowledge base and agent. |
| 168 | + """) |
| 169 | + |
| 170 | +# Footer with explanation |
| 171 | +st.divider() |
| 172 | +with st.expander("📖 How This Works"): |
| 173 | + st.markdown(""" |
| 174 | + **This app uses the PraisonAI Agents framework to create an intelligent Q&A system:** |
| 175 | + |
| 176 | + ⚠️ **Current Status**: Knowledge base functionality is temporarily disabled due to compatibility issues with the current version of PraisonAI Agents (v0.0.157). |
| 177 | + |
| 178 | + **What Works Now:** |
| 179 | + 1. **GPT-5 Integration**: OpenAI's GPT-5 model for generating intelligent responses |
| 180 | + 2. **Markdown Formatting**: Beautiful, structured responses |
| 181 | + 3. **Agent Framework**: PraisonAI Agents for orchestration |
| 182 | + |
| 183 | + **What's Temporarily Disabled:** |
| 184 | + - URL-based knowledge base |
| 185 | + - Vector search and retrieval |
| 186 | + - Document processing |
| 187 | + |
| 188 | + **Key Components:** |
| 189 | + - `Agent`: PraisonAI Agents framework for creating intelligent agents |
| 190 | + - `llm`: OpenAI GPT-5-nano for generating responses |
| 191 | + |
| 192 | + **Why PraisonAI Agents?** |
| 193 | + - Easy-to-use agent framework |
| 194 | + - Built-in error handling |
| 195 | + - Clean API design |
| 196 | + - Perfect for prototyping and production applications |
| 197 | + |
| 198 | + **Next Steps:** |
| 199 | + - Monitor PraisonAI Agents updates for knowledge base fixes |
| 200 | + - Re-enable knowledge functionality when compatible version is available |
| 201 | + """) |
0 commit comments