Skip to main content

A Python SDK for Hallucination Detection — powered by Litmus AI

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Litmus Hallucination Detector

version License Follow @LitmusAI

Litmus Hallucination Detector is an SDK for detecting hallucinations in Large Language Model (LLM) outputs. It uses geometric algebra on embeddings to verify outputs against a provided context.

Installation

pip install litmus-ai

Quick Start

1. Context Grounding (Type I - Default)

Checks if claims are supported by the provided context.

from litmus_ai import LitmusDetector

# Initialize grounding detector (default)
detector = LitmusDetector(type='context-grounding')

context = "The capital of France is Paris."
output = "Paris is the capital of France."

result = detector.check(context=context, output=output)
print(f"Outcome: {'Supported' if not result.is_hallucination else 'Hallucination'}")
print(f"Support Score: {result.score}")

Output:

Outcome: Supported
Support Score: 1.0

2. Context Contradiction (Type II)

Checks if claims actively contradict the provided context.

# Initialize contradiction detector
detector = LitmusDetector(type='context-contradiction')

context = ["Revenue increased 15%."]
output = ["Revenue decreased 15%."]

result = detector.check(context=context, output=output)
print(f"Is Hallucination: {result.is_hallucination}")
print(f"Contradiction Score: {result.score}")

Output:

Is Hallucination: True
Contradiction Score: 0.9998

3. Relational Inversion (Type III)

Detects if the logical relationship between entities is flipped or if there's a direct contradiction/negation.

# Initialize relational inversion detector
detector = LitmusDetector(type='relational-inversion')

context = ["Alice hired Bob."]
output = ["Bob hired Alice."] # Relational Inversion

result = detector.check(context=context, output=output)
print(f"Status: {result.details['claim_scores'][0]['type']}")

Output:

Status: Relational Inversion

4. Instruction Drift (Type IV)

Detects when an answer drifts away from the query-context relationship, even if it uses similar keywords. Requires a query parameter.

# Initialize instruction drift detector
detector = LitmusDetector(type='instruction-drift')

query = "Why did revenue decline?"
context = "Revenue declined due to supply chain issues."
output = "Revenue declined amid a challenging environment."

result = detector.check(query=query, context=context, output=output)
print(f"Drift Score: {result.score:.4f}")
print(f"Is Hallucination: {result.is_hallucination}")

# Override threshold at runtime (must be 0.0 to 1.0)
result = detector.check(query=query, context=context, output=output, threshold=0.5)

Output:

Drift Score: 0.45
Is Hallucination: True

Threshold Override

Every detector.check() call supports an optional threshold parameter to override the default for that call:

# Uses default threshold
result = detector.check(context=context, output=output)

# Override threshold (0.0 to 1.0 inclusive)
result = detector.check(context=context, output=output, threshold=0.9)

# Invalid — raises ValueError
result = detector.check(context=context, output=output, threshold=1.5)

Detection Types

Type Value Classification Model Default Threshold Description
context-grounding Type I all-MiniLM-L6-v2 0.7 Checks if claims are supported by evidence.
context-contradiction Type II nli-deberta-v3-base 0.3 Checks if claims contradict evidence.
relational-inversion Type III nli-deberta-v3-base 0.5 Checks for entity inversions or contradictions.
instruction-drift Type IV all-MiniLM-L6-v2 0.3 Checks if answers drift from query-context alignment.

How it works

Litmus uses mathematical projections to verify claims:

  • Type I projects embeddings onto the subspace spanned by context facts.
  • Type II projects cross-encoded pairs onto a contradiction discriminant.
  • Type III compares projection magnitudes on specialized Inversion vs. Contradiction subspaces.
  • Type IV compares bivector plane alignment between (Query ∧ Context) and (Query ∧ Answer).

License

This project is licensed under the MIT License

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

litmus_ai-0.2.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

litmus_ai-0.2.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file litmus_ai-0.2.1.tar.gz.

File metadata

  • Download URL: litmus_ai-0.2.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for litmus_ai-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7ed4d7e9c99aa203c54a34f3a3889c46eca70e172e6481119a2bfa1777b479b0
MD5 1f20a679347f604f75cad69e09bc774e
BLAKE2b-256 fcde6be27c4e7729ea83c9e49e58baa23fa42e1f13d9602e2976c280d1c47d90

See more details on using hashes here.

File details

Details for the file litmus_ai-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: litmus_ai-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for litmus_ai-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a5f5e6cef99489e8924f47f08550653fcca847a7e152df7a51d4c1d63aec5f8
MD5 68c2c7a6459b260dcc02221b5fe51074
BLAKE2b-256 d2dda3228d350fd2b3d806d5f03183035f2eba084fd9b68904e3d40731026b9f

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