Skip to main content

Haystack Entailment Checker

Custom node for the Haystack NLP framework. Using a Natural Language Inference model, it checks whether a lists of Documents/passages entails, contradicts or is neutral with respect to a given statement.

Live Demo: Fact Checking 🎸 Rocks!   Generic badge

How it works

Entailment Checker Node

  • The node takes a list of Documents (commonly returned by a Retriever) and a statement as input.
  • Using a Natural Language Inference model, the text entailment between each text passage/Document (premise) and the statement (hypothesis) is computed. For every text passage, we get 3 scores (summing to 1): entailment, contradiction and neutral.
  • The text entailment scores are aggregated using a weighted average. The weight is the relevance score of each passage returned by the Retriever, if availaible. It expresses the similarity between the text passage and the statement. Now we have a summary score, so it is possible to tell if the passages confirm, are neutral or disprove the user statement.
  • empirical consideration: if in the first N passages (N<K), there is strong evidence of entailment/contradiction (partial aggregate scores > threshold), it is better not to consider (K-N) less relevant documents.

Installation

pip install haystack-entailment-checker

Usage

Basic example

from haystack import Document
from haystack_entailment_checker import EntailmentChecker

ec = EntailmentChecker(
        model_name_or_path = "microsoft/deberta-v2-xlarge-mnli",
        use_gpu = False,
        entailment_contradiction_threshold = 0.5)

doc = Document("My cat is lazy")

print(ec.run("My cat is very active", [doc]))
# ({'documents': [...],
# 'aggregate_entailment_info': {'contradiction': 1.0, 'neutral': 0.0, 'entailment': 0.0}}, ...)

Fact-checking pipeline (Retriever + EntailmentChecker)

from haystack import Document, Pipeline
from haystack.nodes import BM25Retriever
from haystack.document_stores import InMemoryDocumentStore
from haystack_entailment_checker import EntailmentChecker

# INDEXING
# the knowledge base can consist of many documents
docs = [...]
ds = InMemoryDocumentStore(use_bm25=True)
ds.write_documents(docs)

# QUERYING
retriever = BM25Retriever(document_store=ds)
ec = EntailmentChecker()

pipe = Pipeline()
pipe.add_node(component=retriever, name="Retriever", inputs=["Query"])
pipe.add_node(component=ec, name="EntailmentChecker", inputs=["Retriever"])

pipe.run(query="YOUR STATEMENT TO CHECK")

Acknowledgements 🙏

Special thanks goes to @davidberenstein1957, who contributed to the original implementation of this node, in the Fact Checking 🎸 Rocks! project.

Release files for haystack-entailment-checker 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for haystack-entailment-checker 0.0.4
File Size Uploaded
haystack_entailment_checker-0.0.4.tar.gz 213.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for haystack-entailment-checker 0.0.4
File Interpreter ABI Platform
haystack_entailment_checker-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 218.2 kB

Release files / haystack_entailment_checker-0.0.4.tar.gz

Download URL haystack_entailment_checker-0.0.4.tar.gz
Size 213.3 kB
Tags Source
SHA-256 checksum
How to use checksums
beef71150b62a3aa870a96829ad340808de98821db79d4ce4dc965f504a6ee9c
BLAKE2b-256 checksum
How to use checksums
7096a41b6157ca0147cdff01c5e9b5209cb928fa385dc1216b8ccd8054e98d47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.24.1

Release files / haystack_entailment_checker-0.0.4-py3-none-any.whl

Download URL haystack_entailment_checker-0.0.4-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da9ff320dac544ca68d43bbcbe8d2b2904a208e8263a34136531eb5b85ccb9c5
BLAKE2b-256 checksum
How to use checksums
fed3567ce33b610d778d9d1af62fbd6438823b5a7bc3f2963111e8eaf66daaa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.24.1

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page