-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (41 loc) · 1.7 KB
/
main.py
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
import logging
import os
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from rag.rag_pipeline import RAGPipeline
from templates.response import QueryResponseModel
from templates.request import QueryRequestModel
from dotenv import load_dotenv
load_dotenv()
data_path = os.getenv("DATA_PATH")
logging.basicConfig(
filename='log/app.log',
level=logging.INFO,
format='%(asctime)s %(levelname)s:%(message)s'
)
app = FastAPI()
pipe = RAGPipeline(data_path)
logger = logging.getLogger(__name__)
@app.post("/query", response_model=QueryResponseModel)
async def query(request: QueryRequestModel):
"""
This function is the entry point for the API. It receives a request packet, extracts the question and product name
and invokes the RAG pipeline to get the answer. The answer is then returned in the response packet.
:param request: request packet received from the client
:return: response packet as json
"""
logger.log(logging.INFO, "Received a request")
question = request.question
product_name = request.product_name
if not question:
logger.log(logging.ERROR, "Question is empty in the request packet")
raise HTTPException(status_code=500, detail="Question cannot be empty")
logger.log(logging.INFO, f"Invoking RAG pipeline for the question: {question}")
answer = pipe.get_answer(product_name, question)
response = {"answer": answer, "status": True, "message": "Success"}
logger.log(logging.INFO, f"Returning response: {response}")
return JSONResponse(content=response)
if __name__ == "__main__":
logger.log(logging.INFO, "Starting the server")
uvicorn.run(app, host="0.0.0.0", port=8080)