CoolFace
Apppublic

krishna0506/Document_Intelligence

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
api.py52 linesDownload Raw Back to root
1from fastapi import APIRouter2 3from schemas import AskRequest, AskResponse4from retriever import retrieve5from guardrails import validate6from generator import generate_answer7 8router = APIRouter()9 10# ⭐ STRICT SIMILARITY THRESHOLD11SIMILARITY_THRESHOLD = 0.5   # you can adjust (0.45 – 0.6)12 13def create_routes(vector_store):14 15    @router.post("/ask-recruiter", response_model=AskResponse)16    def ask(request: AskRequest):17 18        # 🔎 Retrieve top results from vector store19        results = retrieve(request.question, vector_store)20 21        # 🚨 If nothing returned22        if not results:23            return AskResponse(24                answer="Information not found in internal documents.",25                confidence="low",26                source_documents=[],27                similarity_score=0.028            )29 30        top_result = results[0]31        score = top_result.get("score", 0.0)32 33        # 🚨 STRICT CHECK → only answer if similarity is high34        if (not validate(results)) or (score < SIMILARITY_THRESHOLD):35            return AskResponse(36                answer="Information not found in internal documents.",37                confidence="low",38                source_documents=[],39                similarity_score=score40            )41 42        # ✅ Generate answer ONLY when relevant43        answer = generate_answer(results)44 45        return AskResponse(46            answer=answer,47            confidence="high",48            source_documents=[top_result.get("source", "")],49            similarity_score=score50        )51 52    return router