64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from src.rag.qa_v1_1_73.answer_composer import AnswerComposer
|
|
from src.rag.qa_v1_1_73.schemas import EvidenceItem, RetrievalHypothesis
|
|
|
|
|
|
class _LLM:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def complete(self, system, user, *, temperature, max_tokens):
|
|
self.calls.append((system, user, temperature, max_tokens))
|
|
return "answer"
|
|
|
|
|
|
def _evidence(evidence_id, relation, target):
|
|
return EvidenceItem(
|
|
evidence_id=evidence_id,
|
|
kb="test",
|
|
source="HSPs",
|
|
relation=relation,
|
|
target=target,
|
|
evidence=f"HSPs {relation} {target}.",
|
|
paper_id="paper-hsp",
|
|
doi="10.1000/hsp",
|
|
)
|
|
|
|
|
|
def test_composer_receives_only_grouped_accepted_evidence_and_mechanism_shape():
|
|
llm = _LLM()
|
|
composer = AnswerComposer(llm)
|
|
accepted = _evidence("Q1.A1.E1", "prevent", "protein aggregation")
|
|
audit_only = _evidence("Q1.A1.E2", "upregulated_in", "heat stress")
|
|
|
|
answer = composer.compose(
|
|
"HSP基因家族如何帮助植物应对高温?",
|
|
RetrievalHypothesis(question_type="mechanism"),
|
|
[accepted],
|
|
{},
|
|
[],
|
|
subquestion_evidence_groups=[
|
|
{
|
|
"subquestion_id": "Q1",
|
|
"question": "HSP如何保护蛋白质?",
|
|
"role": "required",
|
|
"answer_slot": "molecular action",
|
|
"status": "SATISFIED",
|
|
"evidence": [accepted],
|
|
"chains": [],
|
|
}
|
|
],
|
|
)
|
|
|
|
assert answer == "answer"
|
|
prompt = llm.calls[0][1]
|
|
assert "Agent-approved subquestion evidence groups" in prompt
|
|
assert "Q1" in prompt
|
|
assert "molecular action" in prompt
|
|
assert "prevent" in prompt
|
|
assert "upregulated_in" not in prompt
|
|
assert "trigger/context → molecular action → protected target or process → biological outcome" in prompt
|
|
assert "Do not add an intermediate step" in prompt
|
|
assert audit_only.evidence_id not in prompt
|