Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +47 -8
app/main.py
CHANGED
|
@@ -1,20 +1,59 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from app.rag import find_top_5_ayahs_qdrant
|
| 3 |
from app.models.schemas import QuestionRequest, AnswerListResponse
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.post("/ask/", response_model=AnswerListResponse)
|
| 12 |
-
def ask(payload: QuestionRequest):
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.get("/")
|
| 17 |
def read_root():
|
| 18 |
return {"message": "Hello, Mustafa! FastAPI is running 🚀"}
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from app.rag import find_top_5_ayahs_qdrant
|
| 3 |
from app.models.schemas import QuestionRequest, AnswerListResponse
|
| 4 |
+
from openai import AzureOpenAI
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# --- Azure OpenAI setup ---
|
| 10 |
+
client = AzureOpenAI(
|
| 11 |
+
api_key=os.getenv("AZURE_LLM_KEY"),
|
| 12 |
+
api_version="2024-05-01-preview",
|
| 13 |
+
azure_endpoint=os.getenv("AZURE_LLM_ENDPOINT")
|
| 14 |
+
)
|
| 15 |
+
deployment_name = os.getenv("AZURE_LLM_DEPLOYMENT")
|
| 16 |
|
| 17 |
@app.post("/ask/", response_model=AnswerListResponse)
|
| 18 |
+
async def ask(payload: QuestionRequest):
|
| 19 |
+
question = payload.question
|
| 20 |
+
|
| 21 |
+
# 1️⃣ Find top 5 related ayahs using Qdrant
|
| 22 |
+
verses = find_top_5_ayahs_qdrant(question)
|
| 23 |
+
|
| 24 |
+
# 2️⃣ Generate reflection using Azure OpenAI
|
| 25 |
+
messages = [
|
| 26 |
+
{
|
| 27 |
+
"role": "system",
|
| 28 |
+
"content": (
|
| 29 |
+
"You are a compassionate Islamic guide who offers short (≤3 lines) spiritual reflections. "
|
| 30 |
+
"Speak with empathy, hope, and gentle wisdom. "
|
| 31 |
+
"Do NOT quote or refer to Qur’an verses, as they are provided separately."
|
| 32 |
+
),
|
| 33 |
+
},
|
| 34 |
+
{"role": "user", "content": question},
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
completion = client.chat.completions.create(
|
| 39 |
+
model=deployment_name,
|
| 40 |
+
messages=messages,
|
| 41 |
+
max_tokens=120,
|
| 42 |
+
temperature=0.8
|
| 43 |
+
)
|
| 44 |
+
reflection = completion.choices[0].message.content
|
| 45 |
+
except Exception as e:
|
| 46 |
+
reflection = f"⚠️ Error generating reflection: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# 3️⃣ Combine reflection + verses
|
| 49 |
+
return {
|
| 50 |
+
"results": [
|
| 51 |
+
{"type": "reflection", "answer": reflection},
|
| 52 |
+
*verses # Append ayahs from Qdrant
|
| 53 |
+
]
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
|
| 57 |
@app.get("/")
|
| 58 |
def read_root():
|
| 59 |
return {"message": "Hello, Mustafa! FastAPI is running 🚀"}
|
|
|
|
|
|