Files
PlantAI-rag/tests/test_qa_v1_1_73_terminal_answer_ranker.py

58 lines
2.4 KiB
Python

from src.rag.qa_v1_1_73.natural_question_compiler import NaturalQuestionCompiler
from src.rag.qa_v1_1_73.terminal_answer_ranker import TerminalAnswerRanker
def _chain(end, score, assertion_ids, *, target_type="biological_process", category="pathway_participation"):
return {
"end": end,
"path_score": score,
"hops": [
{
"assertion_id": assertion_id,
"target": end if index == len(assertion_ids) - 1 else f"bridge-{index}",
"target_type": target_type if index == len(assertion_ids) - 1 else "gene",
"relation_category": category if index == len(assertion_ids) - 1 else "protein_interaction",
}
for index, assertion_id in enumerate(assertion_ids)
],
}
def test_independent_complete_support_beats_one_high_scoring_wrong_path():
question = "Which biological process was the protein reported to participate in?"
plan = NaturalQuestionCompiler().compile(question)
ranking = TerminalAnswerRanker().rank(
question,
plan,
[
_chain("nucleus", 0.98, ["wrong"], target_type="cellular_location", category="subcellular_localization"),
_chain("citric acid cycle", 0.82, ["a", "b"]),
_chain("Citric-acid cycle", 0.78, ["c", "d"]),
],
)
assert ranking.answer == "citric acid cycle"
assert ranking.candidates[0]["independent_support"] == 2
assert ranking.candidates[0]["answer_type_compatibility"] > ranking.candidates[1]["answer_type_compatibility"]
def test_duplicate_assertion_sequences_do_not_count_as_independent_support():
question = "Which condition was GeneA reported to respond to?"
plan = NaturalQuestionCompiler().compile(question)
ranking = TerminalAnswerRanker().rank(
question,
plan,
[
_chain("salt stress", 0.80, ["same"], target_type="condition", category="stress_response"),
_chain("Salt-stress", 0.79, ["same"], target_type="condition", category="stress_response"),
],
)
assert ranking.candidates[0]["independent_support"] == 1
def test_empty_chains_return_an_auditable_empty_ranking():
plan = NaturalQuestionCompiler().compile("Which condition was GeneA reported to respond to?")
ranking = TerminalAnswerRanker().rank("Which condition was GeneA reported to respond to?", plan, [])
assert ranking.answer == ""
assert ranking.candidates == []