File size: 1,003 Bytes
9026f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from engine import load_system, get_llm_response


app=FastAPI(
    title="Medical RAG API",
    description="Backend API for IBM Granite chat model",
    version="1.0.0")

#gloabal model loading 
print("Loading Medical AI Engine... Please wait.")
retriever, llm = load_system()
print("AI loaded successfully.")

class ChatRequest(BaseModel):
    query: str

class ChatResponse(BaseModel):
    answer: str

@app.get("/")
def read_root():
    return {"status":"online","message": "Medical API is Ready"}

@app.post("/ask", response_model=ChatResponse)
async def ask_question(request: ChatRequest):
    try:
        # Using the globally loaded retriever and llm
        response = get_llm_response(request.query, retriever, llm)
        return ChatResponse(answer=response)
    except Exception as e:
        # Standard API error handling
        raise HTTPException(status_code=500, detail=str(e))