Team Ai
Apppublic

Decoder2704/python-chatbot-jay

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
test_retriever.py85 linesDownload Raw Back to backend
1#!/usr/bin/env python32"""3Smoke-test retrieval artifacts using the same functions as the FastAPI backend.4 5Read-only with respect to retrieval logic; delegates to app.services.retriever.6Run from repo root:  python backend/test_retriever.py7"""8 9from __future__ import annotations10 11import sys12import traceback13from pathlib import Path14 15# Ensure `app` package resolves when executing this file directly16_BACKEND_DIR = Path(__file__).resolve().parent17if str(_BACKEND_DIR) not in sys.path:18    sys.path.insert(0, str(_BACKEND_DIR))19 20from app.services import retriever21 22ANSWER_TRUNCATE = 20023 24SAMPLE_PYTHON_QUESTIONS = [25    "How do I read a text file in Python?",26    "What is a list comprehension and how do I use it?",27    "How do I install third-party packages with pip?",28    "What is the difference between == and is in Python?",29    "How do I catch and handle exceptions with try/except?",30    "What is a virtual environment and why use one?",31    "How can I load and parse JSON from a string or file?",32    "What is a Python decorator?",33    "How do I loop over keys and values in a dictionary?",34    "What is the Global Interpreter Lock in CPython?",35]36 37 38def _truncate(text: object, width: int = ANSWER_TRUNCATE) -> str:39    s = str(text)40    if len(s) <= width:41        return s42    return s[: width - 3] + "..."43 44 45def main() -> int:46    print("Retriever smoke test (vectorizer + question_vectors + chatbot_df)")47    print(f"Artifacts dir: {retriever.ARTIFACTS_DIR}")48 49    try:50        retriever.load_artifacts()51        _v, _qv, df = retriever._require_artifacts()52    except FileNotFoundError as exc:53        print(f"ERROR: Missing artifact(s): {exc}", file=sys.stderr)54        return 155    except OSError as exc:56        print(f"ERROR: Could not load artifacts: {exc}", file=sys.stderr)57        return 158 59    print(60        "Loaded: vectorizer.pkl, question_vectors.pkl, chatbot_df.pkl "61        f"(df rows: {len(df):,})\n"62    )63 64    for i, question in enumerate(SAMPLE_PYTHON_QUESTIONS, start=1):65        print(f"--- Sample {i}/10 ---")66        print(f"Input: {question}")67        try:68            answer, _alternate, qid, aid = retriever.chatbot_response_for_api(question)69        except Exception as exc:70            print(f"ERROR: {exc}", file=sys.stderr)71            traceback.print_exc()72            return 173 74        print(f"Matched QId: {qid}")75        print(f"Matched AId: {aid}")76        print(f"Answer (truncated): {_truncate(answer)}")77        print()78 79    print("All 10 samples completed successfully.")80    return 081 82 83if __name__ == "__main__":84    raise SystemExit(main())85