Spaces:
Runtime error
Runtime error
File size: 1,861 Bytes
6fb30f2 bd0d6c7 96c8df1 6fb30f2 96c8df1 4dcc72c 96c8df1 94070e1 d193279 96c8df1 94070e1 6fb30f2 96c8df1 1043f8b 96c8df1 1043f8b 96c8df1 1043f8b 96c8df1 1043f8b 96c8df1 1043f8b 96c8df1 1043f8b 96c8df1 6fb30f2 |
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 |
from fastapi import FastAPI
from app.rag import find_top_5_ayahs_qdrant
from app.models.schemas import QuestionRequest, AnswerListResponse, ReflectionResponse
from openai import AzureOpenAI
import os
app = FastAPI()
# --- Azure OpenAI setup ---
print(f"endpoint : {os.getenv('VITE_AZURE_LLM_ENDPOINT')}")
client = AzureOpenAI(
api_key=os.getenv("VITE_AZURE_LLM_KEY"),
azure_endpoint=os.getenv("VITE_AZURE_LLM_ENDPOINT"),
api_version="2024-08-01-preview",
)
deployment_name = os.getenv("VITE_AZURE_LLM_DEPLOYMENT")
@app.post("/ask/", response_model=AnswerListResponse)
async def ask(payload: QuestionRequest):
question = payload.question
# 1️⃣ Get ayahs from Qdrant
ayah_results = find_top_5_ayahs_qdrant(question)
# 2️⃣ Generate reflection using Azure LLM
messages = [
{
"role": "system",
"content": (
"You are a compassionate Islamic guide who offers short (≤3 lines) "
"spiritual reflections. Speak with empathy, hope, and gentle wisdom. "
"Do NOT quote or refer to Qur’an verses, as they are provided separately."
),
},
{"role": "user", "content": question},
]
try:
completion = client.chat.completions.create(
model=deployment_name,
messages=messages,
max_tokens=120,
temperature=0.8,
)
reflection_text = completion.choices[0].message.content
except Exception as e:
reflection_text = f"⚠️ Error generating reflection: {str(e)}"
# 3️⃣ Return structured response
return AnswerListResponse(
reflection=ReflectionResponse(reflection=reflection_text),
ayahs=ayah_results,
)
@app.get("/")
def read_root():
return {"message": "Hello, Mustafa! FastAPI is running 🚀"}
|