Text Classification
Transformers
Safetensors
English
HHEMv2Config
custom_code
simonhughes22's picture
Update README.md
afedd94
|
raw
history blame
7.43 kB
metadata
license: apache-2.0
language: en
tags:
  - microsoft/deberta-v3-base
datasets:
  - multi_nli
  - snli
  - fever
  - tals/vitaminc
  - paws
metrics:
  - accuracy
  - auc
  - balanced accuracy
pipeline_tag: text-classification
widget:
  - text: >-
      A man walks into a bar and buys a drink [SEP] A bloke swigs alcohol at a
      pub
    example_title: Positive
  - text: >-
      A boy is jumping on skateboard in the middle of a red bridge. [SEP] The
      boy skates down the sidewalk on a blue bridge
    example_title: Negative

Cross-Encoder for Hallucination Detection

This model was trained using SentenceTransformers Cross-Encoder class. The model outputs a probabilitity from 0 to 1, 0 being a hallucination and 1 being factually consistent. The predictions can be thresholded at 0.5 to predict whether a document is consistent with its source.

Training Data

This model is based on microsoft/deberta-v3-base and is trained initially on NLI data to determine textual entailment, before being further fine tuned on summarization datasets with samples annotated for factual consistency including FEVER, Vitamin C and PAWS.

Performance

LLM Hallucination Leaderboard

If you want to stay up to date with results of the latest tests using this model to evaluate the top LLM models, a public leaderboard is maintained and periodically updated on the vectara/hallucination-leaderboard GitHub repository.

Note about using the Inference API Widget on the Right

To use the model with the widget, you need to pass both documents as a single string separated with [SEP]. For example:

  • A man walks into a bar and buys a drink [SEP] A bloke swigs alcohol at a pub
  • A person on a horse jumps over a broken down airplane. [SEP] A person is at a diner, ordering an omelette.
  • A person on a horse jumps over a broken down airplane. [SEP] A person is outdoors, on a horse.

etc. See examples below for expected probability scores.

Usage with Sentencer Transformers (Recommended)

Inference

The model can be used like this, on pairs of documents, passed as a list of list of strings (List[List[str]]]):

from sentence_transformers import CrossEncoder

model = CrossEncoder('vectara/hallucination_evaluation_model')
scores = model.predict([
    ["A man walks into a bar and buys a drink", "A bloke swigs alcohol at a pub"],
    ["A person on a horse jumps over a broken down airplane.", "A person is at a diner, ordering an omelette."],
    ["A person on a horse jumps over a broken down airplane.", "A person is outdoors, on a horse."],
    ["A boy is jumping on skateboard in the middle of a red bridge.", "The boy skates down the sidewalk on a blue bridge"],
    ["A man with blond-hair, and a brown shirt drinking out of a public water fountain.", "A blond drinking water in public."],
    ["A man with blond-hair, and a brown shirt drinking out of a public water fountain.", "A blond man wearing a brown shirt is reading a book."],
    ["Mark Wahlberg was a fan of Manny.", "Manny was a fan of Mark Wahlberg."],  
])

This returns a numpy array representing a factual consistency score. A score < 0.5 indicates a likely hallucination):

array([0.61051559, 0.00047493709, 0.99639291, 0.00021221573, 0.99599433, 0.0014127002, 0.002.8262993], dtype=float32)

Note that the model is designed to work with entire documents, so long as they fit into the 512 token context window (across both documents). Also note that the order of the documents is important, the first document is the source document, and the second document is validated against the first for factual consistency, e.g. as a summary of the first or a claim drawn from the source.

Training

from sentence_transformers.cross_encoder.evaluation import CEBinaryClassificationEvaluator
from sentence_transformers import SentenceTransformer, InputExample, losses

num_epochs = 5
model_save_path = "./model_dump"

# Load some training examples as such, using a pandas dataframe with source and summary columns:
train_examples, test_examples = [], []
for i, row in df_train.iterrows():
    train_examples.append(InputExample(texts=[row['source'], row['summary']], label=int(row['label'])))

for i, row in df_test.iterrows():
    test_examples.append(InputExample(texts=[row['source'], row['summary']], label=int(row['label'])))
test_evaluator = CEBinaryClassificationEvaluator.from_input_examples(test_examples, name='test_eval')

# Then train the model as such as per the Cross Encoder API:
train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=train_batch_size)
warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) #10% of train data for warm-up
model.fit(train_dataloader=train_dataloader,
          evaluator=test_evaluator,
          epochs=num_epochs,
          evaluation_steps=10_000,
          warmup_steps=warmup_steps,
          output_path=model_save_path,
          show_progress_bar=True)

Usage with Transformers AutoModel

You can use the model also directly with Transformers library (without the SentenceTransformers library):

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np

model = AutoModelForSequenceClassification.from_pretrained('vectara/hallucination_evaluation_model')
tokenizer = AutoTokenizer.from_pretrained('vectara/hallucination_evaluation_model')

pairs = [
    ["A man walks into a bar and buys a drink", "A bloke swigs alcohol at a pub"],
    ["A person on a horse jumps over a broken down airplane.", "A person is at a diner, ordering an omelette."],
    ["A person on a horse jumps over a broken down airplane.", "A person is outdoors, on a horse."],
    ["A boy is jumping on skateboard in the middle of a red bridge.", "The boy skates down the sidewalk on a blue bridge"],
    ["A man with blond-hair, and a brown shirt drinking out of a public water fountain.", "A blond drinking water in public."],
    ["A man with blond-hair, and a brown shirt drinking out of a public water fountain.", "A blond man wearing a brown shirt is reading a book."],
    ["Mark Wahlberg was a fan of Manny.", "Manny was a fan of Mark Wahlberg."], 
]

inputs = tokenizer.batch_encode_plus(pairs, return_tensors='pt', padding=True)

model.eval()
with torch.no_grad():
    outputs = model(**inputs)
    logits = outputs.logits.cpu().detach().numpy()
    # convert logits to probabilities
    scores = 1 / (1 + np.exp(-logits)).flatten()

This returns a numpy array representing a factual consistency score. A score < 0.5 indicates a likely hallucination):

array([0.61051559, 0.00047493709, 0.99639291, 0.00021221573, 0.99599433, 0.0014127002, 0.002.8262993], dtype=float32)