Skip to main content

Estimate hallucination risk in LLM answers from uncertainty language, unsupported specifics, citations, and context coverage. Python port of @mukundakatta/hallucination-risk-meter.

Project description

hallucination-risk-meter

PyPI Python License: MIT

Estimate hallucination risk in LLM answers from uncertainty language, unsupported specifics, citations, and context coverage. Zero runtime dependencies.

Python port of @mukundakatta/hallucination-risk-meter. The JS sibling has the original API; this README sticks to the Python surface.

Install

pip install hallucination-risk-meter

Usage

from hallucination_risk_meter import score

answer = "The company earned $42 million in 2023."
ctx    = "Acme Inc. reported revenue of $40-44 million for fiscal year 2023."

r = score(answer, context=ctx, citations=[{"id": "1"}])

r.score      # float in [0.0, 1.0]
r.signals    # list[str] -- triggered heuristics
r.severity   # 'low' | 'medium' | 'high'

Signals

Each triggered signal contributes a fixed weight to the final score (clamped to [0, 1]):

Signal Weight Triggers when...
uncertainty_language 0.10 Hedge phrases like "I think", "may", "possibly", "not sure" appear.
unsourced_specifics 0.30 Specific numbers, percentages, currency, years, or dates appear in an uncited sentence.
confident_overreach 0.20 "definitely" / "guaranteed" / "always" appear without supporting context overlap.
unsourced_named_entities 0.20 A capitalized multi-word entity appears uncited and absent from the context.
length_disproportionate 0.20 The answer is roughly 3x longer than the supplied context.
no_citations 0.15 The answer asserts something factual-looking but no citations were supplied.

Severity buckets

  • score < 0.34 -> "low"
  • 0.34 - 0.66 -> "medium"
  • score >= 0.67 -> "high"

Custom signals

Inject precomputed signals from your own classifier:

score("...", signals=["nli_contradiction", "self_consistency_drop"])

Names not in the built-in weight table contribute 0.0 but still appear in r.signals for downstream logging.

API differences from the JS sibling

  • Returns a RiskScore dataclass with score, signals, and severity instead of the JS {risk, reasons, likelyHallucinated} object.
  • Adds the signals= parameter for injecting upstream-detector signals.
  • Adds severity bucketing (low/medium/high) for convenient guardrail thresholds.

See the JS sibling's README for the full design notes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hallucination_risk_meter-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hallucination_risk_meter-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file hallucination_risk_meter-0.1.0.tar.gz.

File metadata

File hashes

Hashes for hallucination_risk_meter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f4d256146229612a0a13a92318088b5b67585ff509e8a047d76958c47232996d
MD5 45a895982f5e19cb4f99c3f257101aff
BLAKE2b-256 3f173c91eb0423e839f5d301eee35b98d2174d6ec428a120642a60067ab83c0b

See more details on using hashes here.

File details

Details for the file hallucination_risk_meter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hallucination_risk_meter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d01f8c6f9c1dd0eca96c5b9d499b5cafdf0bc8756905bb0106b3065bf634cfa2
MD5 cffc26e7e8797d012d0da5745ee5fa10
BLAKE2b-256 745998fd671a4c19bbfd49d69a225e7acaa84a5c8f965938386e0c63e38587de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page