The package provides three main scoring variants for detecting hallucinations by comparing a target passage against multiple sampled passages from the same LLM:
SelfCheckMQAG(): Uses Question Answering. Scores are in [0.0, 1.0], where higher values indicate non-factual sentences.SelfCheckBERTScore(): Uses BERTScore. Scores are in [0.0, 1.0], where higher values indicate non-factual sentences.SelfCheckNgram(): Uses n-gram overlap. Scores are in [0.0, +inf) and are not bounded. It provides both sent_level and doc_level scores.
All variants implement a .predict() method. For reproducibility, set torch.manual_seed before calling predict().
from selfcheckgpt.modeling_selfcheck import SelfCheckMQAG, SelfCheckBERTScore, SelfCheckNgram
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Initialize variants
selfcheck_mqag = SelfCheckMQAG(device=device)
selfcheck_bertscore = SelfCheckBERTScore(rescale_with_baseline=True)
selfcheck_ngram = SelfCheckNgram(n=1) # n=1 for Unigram, n=2 for Bigram
# Example usage for MQAG
sent_scores_mqag = selfcheck_mqag.predict(
sentences = ["Sentence 1", "Sentence 2"],
passage = "The full original passage",
sampled_passages = ["Sample 1", "Sample 2", "Sample 3"],
num_questions_per_sent = 5,
scoring_method = 'bayes_with_alpha', # options: 'counting', 'bayes', 'bayes_with_alpha'
beta1 = 0.8, beta2 = 0.8
)