MCPcopy Index your code
hub / github.com/IINemo/lm-polygraph

github.com/IINemo/lm-polygraph @v0.6.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.6.0 ↗ · + Follow
1,197 symbols 3,937 edges 215 files 419 documented · 35%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

LM-Polygraph logo LM-Polygraph: Uncertainty Estimation for LLMs

License: MIT Python 3.12 Hugging Face Benchmark EMNLP 2023 TACL 2025 Tutorial ACL 2025

Installation | Basic usage | Overview | Benchmark | Demo application | Documentation

LM-Polygraph provides a battery of state-of-the-art uncertainty estimation (UE) methods for LLMs in text generation tasks. High uncertainty can indicate the presence of hallucinations and knowing a score that estimates uncertainty can help to make applications of LLMs safer.

LM-Polygraph is also one of the most widely used benchmarks for the consistent evaluation of uncertainty estimation and hallucination detection methods. It is adopted by hundreds of researchers and technology companies.

Installation

From GitHub

The latest stable version is available in the main branch, it is recommended to use a virtual environment:

python -m venv env # Substitute this with your virtual environment creation command
source env/bin/activate
pip install git+https://github.com/IINemo/lm-polygraph.git

You can also use tags:

pip install git+https://github.com/IINemo/lm-polygraph.git@v0.5.0

From PyPI

The latest tagged version is also available via PyPI:

pip install lm-polygraph

Optional dependencies

Some features require additional packages that are not installed by default:

  • COMET metric (translation evaluation): unbabel-comet pins numpy<2.0 which may conflict with packages like vLLM. Install via extras: shell pip install lm-polygraph[comet] If you need numpy 2.x (e.g., for vLLM), install without the extra and add comet manually: shell pip install lm-polygraph pip install unbabel-comet --no-deps

Basic usage

  1. Initialize the base model (encoder-decoder or decoder-only) and tokenizer from HuggingFace or a local file, and use them to initialize the WhiteboxModel for evaluation:
from transformers import AutoModelForCausalLM, AutoTokenizer
from lm_polygraph.utils.model import WhiteboxModel

model_path = "Qwen/Qwen2.5-0.5B-Instruct"
base_model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cuda:0")
tokenizer = AutoTokenizer.from_pretrained(model_path)

model = WhiteboxModel(base_model, tokenizer, model_path=model_path)
  1. Specify the UE method:
from lm_polygraph.estimators import *

ue_method = MeanTokenEntropy()
  1. Get predictions and their uncertainty scores:
from lm_polygraph.utils import estimate_uncertainty

input_text = "Who is George Bush?"
ue = estimate_uncertainty(model, ue_method, input_text=input_text)
print(ue)
# UncertaintyOutput(uncertainty=-6.504108926902215, input_text='Who is George Bush?', generation_text=' President of the United States', model_path='Qwen/Qwen2.5-0.5B-Instruct')
  1. More examples: basic_example.ipynb
  2. See also a low-level example for efficient integration into your code: low_level_example.ipynb

Using with LLMs deployed as a service

LM-Polygraph can work with any OpenAI-compatible API services:

from lm_polygraph import BlackboxModel
from lm_polygraph.estimators import Perplexity, MaximumSequenceProbability

model = BlackboxModel.from_openai(
    openai_api_key='YOUR_API_KEY',
    model_path='gpt-4o',
    supports_logprobs=True  # Enable for deployments
)

ue_method = Perplexity()  # or MeanTokenEntropy(), EigValLaplacian(), etc.
estimate_uncertainty(model, ue_method, input_text='What has a head and a tail but no body?')

UE methods such as EigValLaplacian() support fully blackbox LLMs that do not provide logits.

More examples:

Overview of methods

Uncertainty Estimation Method Type Category Compute Memory Need Training Data? Level
Maximum sequence probability White-box Information-based Low Low No sequence/claim
Perplexity (Fomicheva et al., 2020a) White-box Information-based Low Low No sequence/claim
Mean/max token entropy (Fomicheva et al., 2020a) White-box Information-based Low Low No sequence/claim
Monte Carlo sequence entropy (Kuhn et al., 2023) White-box Information-based High Low No sequence
Pointwise mutual information (PMI) (Takayama and Arase, 2019) White-box Information-based Medium Low No sequence/claim
Conditional PMI (van der Poel et al., 2022) White-box Information-based Medium Medium No sequence
Rényi divergence (Darrin et al., 2023) White-box Information-based Low Low No sequence
Fisher-Rao distance (Darrin et al., 2023) White-box Information-based Low Low No sequence
Attention Score (Sriramanan et al., 2024) White-box Information-based Medium Low No sequence/claim
Contextualized Sequence Likelihood (CSL) (Lin et al., 2024) White-box Information-based Medium Low No sequence
Recurrent Attention-based Uncertainty Quantification (RAUQ) (Vazhentsev et al., 2025) White-box Information-based Low Low No sequence
Focus (Zhang et al., 2023) White-box Information-based Medium Low No sequence/claim
BoostedProb (Dinh et al., 2025) White-box Information-based Low Low No sequence/claim
Semantic entropy (Kuhn et al., 2023) White-box Meaning diversity High Low No sequence
Claim-Conditioned Probability (Fadeeva et al., 2024) White-box Meaning diversity Low Low No sequence/claim
FrequencyScoring (Mohri et al., 2024) White-box Meaning diversity High Low No claim
TokenSAR (Duan et al., 2023) White-box Meaning diversity High Low No sequence/claim
SentenceSAR [(Duan et al., 2023)](https://arxiv.org/abs/23

Core symbols most depended-on inside this repo

to
called by 102
src/lm_polygraph/utils/deberta.py
estimate_uncertainty
called by 68
src/lm_polygraph/utils/estimate_uncertainty.py
from_pretrained
called by 52
src/lm_polygraph/utils/model.py
_register
called by 44
src/lm_polygraph/defaults/register_default_stat_calculators.py
device
called by 31
src/lm_polygraph/utils/model.py
decode
called by 30
src/lm_polygraph/utils/api_with_uncertainty.py
load
called by 26
src/lm_polygraph/utils/dataset.py
tokenizer
called by 16
src/lm_polygraph/utils/model.py

Shape

Method 705
Function 280
Class 212

Languages

Python100%

Modules by API surface

src/lm_polygraph/estimators/ensemble_token_measures.py58 symbols
test/test_estimators.py44 symbols
src/lm_polygraph/utils/model.py32 symbols
src/lm_polygraph/generation_metrics/alignscore_utils.py30 symbols
src/lm_polygraph/estimators/ensemble_sequence_measures.py27 symbols
test/local/test_estimators_visual.py26 symbols
src/lm_polygraph/utils/vllm_with_uncertainty.py25 symbols
test/local/test_benchmark.py19 symbols
test/local/blackbox/mock_openai.py14 symbols
src/lm_polygraph/utils/manager.py14 symbols
src/lm_polygraph/utils/ensemble_utils/ensemble_generator.py14 symbols
src/lm_polygraph/model_adapters/visual_whitebox_model.py14 symbols

For agents

$ claude mcp add lm-polygraph \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page