Spaces:
Sleeping
Sleeping
Create verifier.py
Browse files- verifier.py +74 -0
verifier.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from research_state import ResearchState
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def verify_research(state: ResearchState) -> dict:
|
| 5 |
+
results = []
|
| 6 |
+
|
| 7 |
+
for claim in state.claims:
|
| 8 |
+
evidence = state.evidence_for_claim(claim)
|
| 9 |
+
|
| 10 |
+
if not evidence:
|
| 11 |
+
results.append(
|
| 12 |
+
{
|
| 13 |
+
"claim": claim,
|
| 14 |
+
"status": "UNVERIFIED",
|
| 15 |
+
"reason": "No evidence was collected.",
|
| 16 |
+
"sources": [],
|
| 17 |
+
}
|
| 18 |
+
)
|
| 19 |
+
continue
|
| 20 |
+
|
| 21 |
+
supporting = [
|
| 22 |
+
item for item in evidence
|
| 23 |
+
if item.supports_claim is True
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
contradicting = [
|
| 27 |
+
item for item in evidence
|
| 28 |
+
if item.supports_claim is False
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
if contradicting:
|
| 32 |
+
status = "CONFLICTING"
|
| 33 |
+
elif supporting:
|
| 34 |
+
status = "SUPPORTED"
|
| 35 |
+
else:
|
| 36 |
+
status = "UNCERTAIN"
|
| 37 |
+
|
| 38 |
+
results.append(
|
| 39 |
+
{
|
| 40 |
+
"claim": claim,
|
| 41 |
+
"status": status,
|
| 42 |
+
"reason": _build_reason(
|
| 43 |
+
supporting,
|
| 44 |
+
contradicting,
|
| 45 |
+
evidence,
|
| 46 |
+
),
|
| 47 |
+
"sources": [
|
| 48 |
+
item.source_url
|
| 49 |
+
for item in evidence
|
| 50 |
+
],
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return {
|
| 55 |
+
"question": state.question,
|
| 56 |
+
"claims": results,
|
| 57 |
+
"verified": all(
|
| 58 |
+
item["status"] == "SUPPORTED"
|
| 59 |
+
for item in results
|
| 60 |
+
),
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _build_reason(supporting, contradicting, evidence):
|
| 65 |
+
if contradicting:
|
| 66 |
+
return "At least one source contradicts the claim."
|
| 67 |
+
|
| 68 |
+
if supporting:
|
| 69 |
+
return f"{len(supporting)} source(s) support the claim."
|
| 70 |
+
|
| 71 |
+
return (
|
| 72 |
+
f"Evidence was found from {len(evidence)} source(s), "
|
| 73 |
+
"but it has not been explicitly classified as supporting."
|
| 74 |
+
)
|