""" Main application entry point for Islamic Scholar Agent """ import sys import os # Add project root to Python path when run directly if __name__ == "__main__" and __package__ is None: # When running as script, add the parent directory to make 'src' importable project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, project_root) from agno.os import AgentOS from src.agents.islamic_scholar_agent import IslamicScholarAgent from src.models.factory import ModelFactory from src.knowledge.rag_pipeline import create_knowledge_base from src.core.logging import logger from src.knowledge.embedding_factory import EmbeddingFactory # from langfuse.openai import openai as langfuse_openai # from langfuse import Langfuse from src.utils.load_settings import get_active_agent_config from dotenv import load_dotenv load_dotenv() def create_app(): """Create and configure the FastAPI application""" # Initialize model model = ModelFactory() embed_factory = EmbeddingFactory() current_embedder = embed_factory.get_embedder() # Try to initialize knowledge base, but handle connection errors gracefully try: knowledge_base = create_knowledge_base( embedder=current_embedder, ) logger.info(f"Knowledge base initialized with: {current_embedder.id}") except Exception as e: logger.warning(f"Could not initialize knowledge base: {e}") logger.warning("Application will start without knowledge base. Some features may not work.") knowledge_base = None print(f"Could not initialize knowledge base: {e}") print("Application will start without knowledge base. Some features may not work.") # Create agent - use fallback if knowledge base is not available if knowledge_base: print(f"Knowledge base initialized with: {current_embedder.id}") agent = IslamicScholarAgent(model.get_model(), knowledge_base) agent_os = AgentOS(agents=[agent.get_agent()]) else: # Create a fallback agent without knowledge base logger.warning("Creating fallback agent without knowledge base") print(f"****************************************************************") print(f"Creating fallback agent without knowledge base") from agno.agent import Agent fallback_agent = Agent( name="Fallback Islamic Scholar Agent", model=model.get_model(), description="Basic Islamic knowledge agent (knowledge base unavailable)", instructions=[ "You are a basic Islamic knowledge assistant.", "The knowledge base is currently unavailable.", "Please inform the user that the full knowledge base is not accessible at the moment.", "You can still provide general guidance about Islamic topics." ], markdown=False, debug_mode=True, ) agent_os = AgentOS(agents=[fallback_agent]) # Get FastAPI app # langfuse_openai.langfuse_public_key = os.getenv("LANGFUSE_PUBLIC_KEY") # langfuse_openai.langfuse_secret_key = os.getenv("LANGFUSE_SECRET_KEY") # langfuse_openai.langfuse_host = os.getenv("LANGFUSE_HOST") app = agent_os.get_app() logger.info("Islamic Scholar Agent application created successfully") return app # Create application instance app = create_app() if __name__ == "__main__": import uvicorn from core.settings import HOST , PORT , DEBUG_MODE logger.info(f"Starting server on {HOST}:{PORT} (debug={DEBUG_MODE})") uvicorn.run( "src.main:app", host=HOST, port=PORT, reload=DEBUG_MODE, log_level="debug" if DEBUG_MODE else "info" )