File size: 3,004 Bytes
92e030f
 
c22aebc
92e030f
 
 
c22aebc
92e030f
c22aebc
92e030f
 
 
 
 
 
 
 
 
 
 
c22aebc
92e030f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c22aebc
92e030f
 
 
 
 
 
 
 
 
 
 
 
c22aebc
92e030f
 
 
 
 
 
 
 
 
 
c22aebc
92e030f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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 = """
    <html>
        <head>
            <title>VibbaBackend</title>
            <style>
                body { 
                    font-family: Arial, sans-serif; 
                    margin: 40px; 
                    background-color: #f9f9f9; 
                    color: #333; 
                }
                h1 { color: #0073e6; }
                .container {
                    max-width: 800px;
                    margin: auto;
                    padding: 20px;
                    background: #fff;
                    border-radius: 8px;
                    box-shadow: 0 2px 6px rgba(0,0,0,0.1);
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h1>VibbaBackend</h1>
                <p>
                    Welcome to the <strong>VibbaBackend</strong> service! 🚀<br><br>
                    This backend powers a Retrieval-Augmented Generation (RAG) pipeline 
                    using an ensemble retriever and a large language model. 
                </p>
                <p>
                    <strong>Available endpoints:</strong>
                    <ul>
                        <li><code>/getResponse?question=Your+query</code> – Get an answer to your question.</li>
                    </ul>
                </p>
            </div>
        </body>
    </html>
    """
    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)