Update api.py
Browse files
api.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
api.py β CovidKosha RAG Backend (
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os, json, logging, re
|
|
@@ -21,7 +21,12 @@ if not GROQ_API_KEY:
|
|
| 21 |
EMBED_MODEL = "all-MiniLM-L6-v2"
|
| 22 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 23 |
TOP_K = 5
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
COVIDKOSHA_SITE = "covidkosha.com"
|
| 26 |
|
| 27 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -104,10 +109,21 @@ def retrieve(question: str, k: int = TOP_K):
|
|
| 104 |
|
| 105 |
|
| 106 |
def is_satisfactory(results: list) -> bool:
|
| 107 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if not results:
|
| 109 |
return False
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
def build_context(results: list) -> str:
|
|
@@ -284,6 +300,49 @@ def health():
|
|
| 284 |
return {"status": "ok", "chunks": faiss_index.ntotal}
|
| 285 |
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
@app.post("/ask")
|
| 288 |
def ask(req: QueryRequest):
|
| 289 |
if not req.question.strip():
|
|
|
|
| 1 |
"""
|
| 2 |
+
api.py β CovidKosha RAG Backend (v3 β fixed threshold + debug endpoint)
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os, json, logging, re
|
|
|
|
| 21 |
EMBED_MODEL = "all-MiniLM-L6-v2"
|
| 22 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 23 |
TOP_K = 5
|
| 24 |
+
|
| 25 |
+
# FIX β : Lowered from 0.40 β 0.20
|
| 26 |
+
# all-MiniLM-L6-v2 cosine scores on domain-specific text typically land 0.20β0.55.
|
| 27 |
+
# 0.40 was causing almost every query to fall through to web search.
|
| 28 |
+
CONFIDENCE_THRESHOLD = 0.20
|
| 29 |
+
|
| 30 |
COVIDKOSHA_SITE = "covidkosha.com"
|
| 31 |
|
| 32 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
def is_satisfactory(results: list) -> bool:
|
| 112 |
+
"""
|
| 113 |
+
FIX β‘: Check only the TOP score (not any score).
|
| 114 |
+
Previously used any() which is fine, but the real issue was the threshold.
|
| 115 |
+
Now we check the best match only β if FAISS can't find even one chunk
|
| 116 |
+
with score β₯ 0.20, the query genuinely has no relevant local data.
|
| 117 |
+
Also guards against an empty index (ntotal == 0).
|
| 118 |
+
"""
|
| 119 |
if not results:
|
| 120 |
return False
|
| 121 |
+
if faiss_index.ntotal == 0:
|
| 122 |
+
log.warning("β οΈ FAISS index is empty β check your index.faiss build step")
|
| 123 |
+
return False
|
| 124 |
+
top_score = results[0]["score"]
|
| 125 |
+
log.info(f" Top FAISS score: {top_score:.4f} (threshold: {CONFIDENCE_THRESHOLD})")
|
| 126 |
+
return top_score >= CONFIDENCE_THRESHOLD
|
| 127 |
|
| 128 |
|
| 129 |
def build_context(results: list) -> str:
|
|
|
|
| 300 |
return {"status": "ok", "chunks": faiss_index.ntotal}
|
| 301 |
|
| 302 |
|
| 303 |
+
# FIX β’: New /debug endpoint β hit this to verify your index has real data
|
| 304 |
+
@app.get("/debug")
|
| 305 |
+
def debug():
|
| 306 |
+
"""
|
| 307 |
+
Diagnostic endpoint. Visit /debug to check:
|
| 308 |
+
- total_chunks: should match your document count (0 = broken index)
|
| 309 |
+
- sample_chunks: first 3 entries β verify they contain your actual COVID data
|
| 310 |
+
- threshold: current CONFIDENCE_THRESHOLD in use
|
| 311 |
+
Call /debug/search?q=your+question to see raw FAISS scores for any query.
|
| 312 |
+
"""
|
| 313 |
+
return {
|
| 314 |
+
"total_chunks": faiss_index.ntotal,
|
| 315 |
+
"threshold": CONFIDENCE_THRESHOLD,
|
| 316 |
+
"embed_model": EMBED_MODEL,
|
| 317 |
+
"sample_chunks": chunks[:3] if chunks else [],
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
@app.get("/debug/search")
|
| 322 |
+
def debug_search(q: str = "what is COVID-19", k: int = 5):
|
| 323 |
+
"""
|
| 324 |
+
Run a live retrieval and return raw scores + chunk previews.
|
| 325 |
+
Use this to tune CONFIDENCE_THRESHOLD:
|
| 326 |
+
- If scores are 0.30β0.55 for relevant questions β threshold 0.20β0.25 is fine
|
| 327 |
+
- If scores are all < 0.15 even for obvious queries β your index may be broken
|
| 328 |
+
Example: GET /debug/search?q=COVID+vaccine+efficacy
|
| 329 |
+
"""
|
| 330 |
+
results = retrieve(q, k)
|
| 331 |
+
return {
|
| 332 |
+
"question": q,
|
| 333 |
+
"threshold": CONFIDENCE_THRESHOLD,
|
| 334 |
+
"satisfied": is_satisfactory(results),
|
| 335 |
+
"results": [
|
| 336 |
+
{
|
| 337 |
+
"score": r["score"],
|
| 338 |
+
"preview": str(r.get("meta", {}))[:200],
|
| 339 |
+
"text": str(r.get("text", ""))[:300],
|
| 340 |
+
}
|
| 341 |
+
for r in results
|
| 342 |
+
],
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
|
| 346 |
@app.post("/ask")
|
| 347 |
def ask(req: QueryRequest):
|
| 348 |
if not req.question.strip():
|