* local question answering over analysis notebooks with LightRAG, Neo4j, Qdrant and Ollama * RAGAS evaluation with a generator x judge comparison matrix, fully local * docs in README and OKF
221 lines
7.5 KiB
Python
221 lines
7.5 KiB
Python
"""Tests for askme CLI entrypoints (api/cli.py and evaluation/cli.py)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from askme.api.cli import app as query_app
|
|
from askme.evaluation.cli import app as eval_app
|
|
|
|
runner = CliRunner()
|
|
|
|
# ── askme-query ───────────────────────────────────────────────────────────────
|
|
|
|
RAW_RESPONSE = (
|
|
"Scanpy is used for single-cell RNA-seq analysis.\n\n"
|
|
"## Sources\n"
|
|
"- notebooks/tutorial.ipynb: Scanpy is a Python library\n"
|
|
)
|
|
|
|
|
|
def _mock_rag(raw: str = RAW_RESPONSE):
|
|
rag = AsyncMock()
|
|
rag.initialize_storages = AsyncMock()
|
|
rag.finalize_storages = AsyncMock()
|
|
rag.aquery = AsyncMock(return_value=raw)
|
|
return rag
|
|
|
|
|
|
def test_query_prints_answer():
|
|
with patch("askme.api.cli.create_rag", return_value=_mock_rag()):
|
|
result = runner.invoke(query_app, ["What is scanpy?"])
|
|
assert result.exit_code == 0
|
|
assert "Scanpy is used for single-cell RNA-seq analysis." in result.output
|
|
|
|
|
|
def test_query_prints_sources():
|
|
with patch("askme.api.cli.create_rag", return_value=_mock_rag()):
|
|
result = runner.invoke(query_app, ["What is scanpy?"])
|
|
assert "Sources:" in result.output
|
|
assert "notebooks/tutorial.ipynb" in result.output
|
|
|
|
|
|
def test_query_no_sources_section():
|
|
with patch("askme.api.cli.create_rag", return_value=_mock_rag("Just an answer.")):
|
|
result = runner.invoke(query_app, ["What is scanpy?"])
|
|
assert result.exit_code == 0
|
|
assert "Sources:" not in result.output
|
|
|
|
|
|
def test_query_mode_option():
|
|
mock = _mock_rag()
|
|
with patch("askme.api.cli.create_rag", return_value=mock):
|
|
result = runner.invoke(query_app, ["question", "--mode", "local"])
|
|
assert result.exit_code == 0
|
|
call_args = mock.aquery.call_args
|
|
param = call_args.kwargs.get("param") or call_args.args[1]
|
|
assert param.mode == "local"
|
|
|
|
|
|
# ── askme-eval ────────────────────────────────────────────────────────────────
|
|
|
|
FAKE_SCORES = {
|
|
"faithfulness": 0.9,
|
|
"answer_relevancy": 0.85,
|
|
"context_precision": 0.8,
|
|
"samples": [],
|
|
}
|
|
|
|
FAKE_TESTSET = [
|
|
{
|
|
"question": "What is scanpy?",
|
|
"ground_truth": "Scanpy is for single-cell analysis.",
|
|
"contexts": ["Scanpy is a Python library."],
|
|
}
|
|
]
|
|
|
|
|
|
def test_eval_exits_zero(tmp_path: Path, sample_chunks):
|
|
report = tmp_path / "report.json"
|
|
with (
|
|
patch("askme.evaluation.cli._sample_docs", return_value=sample_chunks),
|
|
patch("askme.evaluation.cli.generate_testset", return_value=FAKE_TESTSET),
|
|
patch("askme.evaluation.cli.run_eval", return_value=FAKE_SCORES),
|
|
):
|
|
result = runner.invoke(
|
|
eval_app,
|
|
["--testset-size", "1", "--output", str(report)],
|
|
)
|
|
assert result.exit_code == 0
|
|
|
|
|
|
def test_eval_writes_json_report(tmp_path: Path, sample_chunks):
|
|
import json
|
|
|
|
report = tmp_path / "report.json"
|
|
with (
|
|
patch("askme.evaluation.cli._sample_docs", return_value=sample_chunks),
|
|
patch("askme.evaluation.cli.generate_testset", return_value=FAKE_TESTSET),
|
|
patch("askme.evaluation.cli.run_eval", return_value=FAKE_SCORES),
|
|
):
|
|
runner.invoke(eval_app, ["--testset-size", "1", "--output", str(report)])
|
|
assert report.exists()
|
|
data = json.loads(report.read_text())
|
|
assert "faithfulness" in data
|
|
|
|
|
|
def test_eval_exits_one_when_no_docs(tmp_path: Path):
|
|
report = tmp_path / "report.json"
|
|
with patch("askme.evaluation.cli._sample_docs", return_value=[]):
|
|
result = runner.invoke(
|
|
eval_app,
|
|
["--testset-size", "1", "--output", str(report)],
|
|
)
|
|
assert result.exit_code == 1
|
|
|
|
|
|
def test_eval_default_testset_size_is_50(tmp_path: Path, sample_chunks):
|
|
"""
|
|
Scenario: a full eval defaults to 50 questions
|
|
When eval runs with no --testset-size
|
|
Then generate_testset is asked for 50 Q&A pairs
|
|
"""
|
|
report = tmp_path / "report.json"
|
|
with (
|
|
patch("askme.evaluation.cli._sample_docs", return_value=sample_chunks),
|
|
patch("askme.evaluation.cli.generate_testset", return_value=FAKE_TESTSET) as mock_gen,
|
|
patch("askme.evaluation.cli.run_eval", return_value=FAKE_SCORES),
|
|
):
|
|
runner.invoke(eval_app, ["--output", str(report)])
|
|
assert mock_gen.call_args.kwargs.get("n") == 50
|
|
|
|
|
|
def test_eval_report_records_elapsed_seconds(tmp_path: Path, sample_chunks):
|
|
"""
|
|
Scenario: the eval report records how long the run took
|
|
When eval finishes
|
|
Then the JSON report carries an elapsed_seconds number (CPU-judge runs are
|
|
long; the duration is part of the result)
|
|
"""
|
|
import json
|
|
|
|
report = tmp_path / "report.json"
|
|
with (
|
|
patch("askme.evaluation.cli._sample_docs", return_value=sample_chunks),
|
|
patch("askme.evaluation.cli.generate_testset", return_value=FAKE_TESTSET),
|
|
patch("askme.evaluation.cli.run_eval", return_value=FAKE_SCORES),
|
|
):
|
|
runner.invoke(eval_app, ["--testset-size", "1", "--output", str(report)])
|
|
data = json.loads(report.read_text())
|
|
assert isinstance(data["elapsed_seconds"], (int, float))
|
|
|
|
|
|
def test_eval_reuses_cached_doc_sample(tmp_path: Path, sample_chunks):
|
|
"""
|
|
Scenario: second run reuses the cached doc sample
|
|
Given a doc-sample cache matching the source and sample size
|
|
When eval runs without --refresh
|
|
Then the extract/filter sampling is skipped (cache is used)
|
|
"""
|
|
import json
|
|
|
|
report = tmp_path / "report.json"
|
|
source = tmp_path / "notebooks"
|
|
source.mkdir()
|
|
cache = report.with_suffix(".docsample.json")
|
|
cache.write_text(
|
|
json.dumps({"source": str(source), "doc_sample": 25, "docs": sample_chunks})
|
|
)
|
|
|
|
with (
|
|
patch("askme.evaluation.cli._sample_docs") as mock_sample,
|
|
patch("askme.evaluation.cli.generate_testset", return_value=FAKE_TESTSET),
|
|
patch("askme.evaluation.cli.run_eval", return_value=FAKE_SCORES),
|
|
):
|
|
result = runner.invoke(
|
|
eval_app,
|
|
["--source", str(source), "--testset-size", "1", "--output", str(report)],
|
|
)
|
|
|
|
assert result.exit_code == 0
|
|
mock_sample.assert_not_called()
|
|
|
|
|
|
def test_eval_refresh_ignores_cache(tmp_path: Path, sample_chunks):
|
|
"""
|
|
Scenario: --refresh forces re-sampling even when a cache exists
|
|
Given a doc-sample cache
|
|
When eval runs with --refresh
|
|
Then _sample_docs is called again (cache ignored, then rewritten)
|
|
"""
|
|
import json
|
|
|
|
report = tmp_path / "report.json"
|
|
source = tmp_path / "notebooks"
|
|
source.mkdir()
|
|
cache = report.with_suffix(".docsample.json")
|
|
cache.write_text(
|
|
json.dumps({"source": str(source), "doc_sample": 25, "docs": sample_chunks})
|
|
)
|
|
|
|
with (
|
|
patch("askme.evaluation.cli._sample_docs", return_value=sample_chunks) as mock_sample,
|
|
patch("askme.evaluation.cli.generate_testset", return_value=FAKE_TESTSET),
|
|
patch("askme.evaluation.cli.run_eval", return_value=FAKE_SCORES),
|
|
):
|
|
result = runner.invoke(
|
|
eval_app,
|
|
[
|
|
"--source", str(source),
|
|
"--refresh",
|
|
"--testset-size", "1",
|
|
"--output", str(report),
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 0
|
|
mock_sample.assert_called_once()
|