# main.py # This is the main file that runs the Sanic web server. from sanic import Sanic, response from retriever import get_ensemble_retriever from llm_chain import create_rag_chain app = Sanic("VibbaBackend") @app.before_server_start async def setup_model(app_instance, loop): """ Initializes the retriever and RAG chain and attaches them to the application context before the server starts. """ print("Server starting up... Initializing model pipeline.") retriever = get_ensemble_retriever() rag_chain = create_rag_chain(retriever) app_instance.ctx.rag_chain = rag_chain print("Model pipeline is ready.") @app.get("/") async def home(request): """ Root endpoint showing app name and description. """ html_content = """ VibbaBackend

VibbaBackend

Welcome to the VibbaBackend service! 🚀

This backend powers a Retrieval-Augmented Generation (RAG) pipeline using an ensemble retriever and a large language model.

Available endpoints:

""" return response.html(html_content) @app.get("/getResponse") async def get_response_endpoint(request): """ Endpoint to get an answer to a question using the RAG chain. Expects a 'question' query parameter. """ question = request.args.get("question") if not question: return response.json( {"error": "Please provide a 'question' query parameter."}, status=400 ) try: chain = request.app.ctx.rag_chain result = chain.invoke(question) return response.text(result) except Exception as e: print(f"An error occurred during invocation: {e}") return response.json( {"error": "An internal error occurred while processing your request."}, status=500 ) if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)