from fastapi import FastAPI from contextlib import asynccontextmanager from typing import List # Import centralized settings and other components from app.config import settings from app.core.vector_store import FaissVectorStore, get_embedder_from_config from app.core.retrievers import FaissDBRetriever, Retriever from app.core.services import RAGService from app.db.session import create_db_and_tables from app.api.routes import create_api_router from app.utils import print_config # Note: The llm_clients import and initialization are removed as they # are not used in RAGService's constructor based on your services.py # from app.core.llm_clients import DeepSeekClient, GeminiClient @asynccontextmanager async def lifespan(app: FastAPI): """ Manages application startup and shutdown events. - On startup, it creates database tables. - On shutdown, it saves the FAISS index to disk. """ print("Application startup...") print_config(settings) create_db_and_tables() yield print("Application shutdown...") # Access the vector_store from the application state to save it if hasattr(app.state, 'vector_store'): app.state.vector_store.save_index() def create_app() -> FastAPI: """ Factory function to create and configure the FastAPI application. This encapsulates all setup logic, making the main entry point clean. """ app = FastAPI( # Use metadata from the central settings title=settings.PROJECT_NAME, version=settings.VERSION, description="A modular API to route requests to various LLMs with RAG capabilities.", lifespan=lifespan ) # --- Initialize Core Services using settings --- # 1. Use the new, more flexible factory function to create the embedder instance # This decouples the application from a specific embedding provider. embedder = get_embedder_from_config( provider=settings.EMBEDDING_PROVIDER, dimension=settings.EMBEDDING_DIMENSION, model_name=settings.EMBEDDING_MODEL_NAME, api_key=settings.EMBEDDING_API_KEY ) # 2. Initialize the FaissVectorStore with the chosen embedder app.state.vector_store = FaissVectorStore( index_file_path=settings.FAISS_INDEX_PATH, dimension=settings.EMBEDDING_DIMENSION, embedder=embedder # Pass the instantiated embedder object, ) # 3. Create the FaissDBRetriever, regardless of the embedder type retrievers: List[Retriever] = [ FaissDBRetriever(vector_store=app.state.vector_store), ] # 4. Initialize the RAGService with the created retriever list # The llm_clients are no longer passed here, as per your services.py rag_service = RAGService( vector_store=app.state.vector_store, retrievers=retrievers ) # Create and include the API router, injecting the service api_router = create_api_router(rag_service=rag_service) app.include_router(api_router) return app