uqlm: Uncertainty Quantification for Language Models

repository·main·Indexed 22 days ago

https://github.com/cvs-health/uqlm

A Python library for Large Language Model (LLM) hallucination detection using state-of-the-art uncertainty quantification (UQ) techniques. It provides a variety of scorers including Black-Box (consistency-based), White-Box (token-probability-based), LLM-as-a-Judge, and Ensemble methods. The library also supports specialized UQ for long-form text, code generation via CodeGenUQ, and multimodal inputs.

Tokens
41.2K
Snippets
88
Records
182
Agent score
78%

What's inside uqlm

  1. Overview of uqlm uncertainty quantification

    main

    uqlm is a Python library designed for LLM hallucination detection using uncertainty quantification techniques. It provides various scoring methods to assess the reliability of model outputs.

    Each scorer in the library returns a confidence score between 0 and 1. A higher score indicates a lower likelihood of hallucination (higher confidence), while a lower score indicates a higher likelihood of hallucination.

  2. What is UQLM and how does it detect hallucinations?

    main

    UQLM (Uncertainty Quantification for Language Models) is a Python package designed for generation-time, zero-resource LLM hallucination detection. Unlike traditional methods that require human-authored ground-truth text (offline testing), UQLM integrates generation and evaluation to compute confidence scores during the generation process.

    Key Features:

    • Confidence Scores: Returns a value between 0 and 1, where higher scores indicate a lower likelihood of hallucinations.
    • Zero-Resource: Does not require external knowledge sources or ground-truth data at generation time.
    • Four Scorer Categories:
      • Black-box UQ: Based on the consistency of multiple responses to the same prompt.
      • White-box UQ: Based on token probabilities (requires API support for logprobs).
      • LLM-as-a-Judge: Uses one or more LLMs to evaluate the question-response pair.
      • Ensembles: Combines the above methods into a single score.
  3. Overview of uqlm: Uncertainty Quantification for Language Models

    main
    uqlm is a Python library designed for Large Language Model (LLM) hallucination detection. It utilizes state-of-the-art uncertainty quantification techniques to identify when an LLM might be generating inaccurate or ungrounded information.
  4. Overview of Hallucination Detection Scorers

    main

    UQLM provides several categories of response-level scorers to quantify LLM output uncertainty. Each scorer returns a confidence score between 0 and 1 (higher is better).

    Scorer TypeLatencyCostCompatibilityEffort
    Black-BoxMedium-HighHighUniversalOff-the-shelf
    White-BoxMinimal*None*Limited (requires token probabilities)Off-the-shelf
    LLM-as-a-JudgeLow-MediumLow-HighUniversalOff-the-shelf
    EnsembleFlexibleFlexibleFlexibleOff-the-shelf or Tuned
    Long-TextHigh-Very HighHighUniversalOff-the-shelf

    *Note: White-box costs/latency increase for multi-generation scorers.

  5. What is the Consistency and Confidence (CoCoA) scorer?

    main

    The Consistency and Confidence (CoCoA) approach is a white-box uncertainty quantification method that combines two distinct signals to produce a score in the range [0, 1]. A high score requires both high token-level confidence and high response-level semantic consistency.

    CoCoA is calculated using three steps:

    1. Length-Normalized Token Probability (LNTP): Computes the product of token probabilities in the original response, normalized by its length.
    2. Normalized Cosine Similarity (NCS): Calculates the average cosine similarity between the original response and $m$ sampled responses, normalized to the [0, 1] range.
    3. CoCoA Score: The product of the LNTP and the NCS.

    This method is effective because it ensures that a response is only considered high-confidence if the model was confident in its token predictions AND the resulting responses are semantically consistent with one another.

  6. How Black-Box Scorers work for Uncertainty Quantification

    main

    Black-box Uncertainty Quantification (UQ) methods estimate response-level confidence by treating the LLM as a black box. Instead of inspecting internal model states, these scorers evaluate the consistency of multiple responses generated from the same prompt.

    To use this approach, you must generate $m$ responses (where $m > 1$) for a given prompt $x_i$ using a non-zero temperature. The scorer then compares these responses $\tilde{\mathbf{y}}i = { \tilde{y}{i1},...,\tilde{y}_{im} }$ against the original response $y_i$ or against each other to determine uncertainty.

    When to use Black-Box Scorers:

    • When you are using an LLM that does not expose token probabilities or internal states (e.g., closed-source APIs like OpenAI or Anthropic).
    • When you need a method that is universally compatible with any model.

    Trade-offs to consider:

    • Higher Cost: You must perform multiple generations per prompt.
    • Higher Latency: Generating and comparing multiple responses is slower than single-pass scoring.
  7. Compare uqlm scorer types

    main

    uqlm offers five distinct categories of scorers, each with different trade-offs regarding latency, cost, and model requirements:

    • Black-Box Scorers: Measure consistency across multiple LLM generations. They are universal (work with any model) but have medium latency and higher costs due to multiple calls.
    • White-Box Scorers: Leverage token probabilities for fast, free single-generation scoring. They have minimal latency and no extra cost, but require access to model logprobs.
    • LLM-as-a-Judge: Uses one or more LLMs to evaluate response reliability. These are highly customizable via prompt engineering and are universal, with low-to-medium latency and variable costs.
    • Ensemble Scorers: Combine multiple scorers via weighted averaging for more robust confidence estimates. They offer flexible latency and cost and are universal.
    • Long-Text Scorers: Score uncertainty at the claim level for long-form responses. They support uncertainty-aware response refinement but have high latency and high costs.
  8. What are White-Box Scorers in uqlm?

    main

    White-box Uncertainty Quantification (UQ) methods estimate uncertainty by leveraging the internal token probabilities of an LLM. Unlike black-box methods that require additional LLM calls to judge a response, white-box scorers use the probabilities already returned during the initial generation.

    Key Advantages:

    • Minimal Latency: Uses data already provided by the LLM.
    • No Added Cost: Single-generation scorers do not require extra API calls.
    • High Performance: Provides rich uncertainty signals from internal model states.

    Limitations:

    • Limited Compatibility: You must use an LLM or API that provides access to token probabilities (logprobs).
  9. Use LLM-as-a-Judge scorers for reliability evaluation

    main

    LLM-as-a-Judge scorers utilize one or more Large Language Models (LLMs) to evaluate the reliability of an original LLM's response. This approach is highly customizable via prompt engineering and allows you to choose any LLM to act as the judge.

    Key Features:

    • Universal Compatibility: Can be used with any LLM.
    • Customizability: You can tailor instruction prompts for specific use cases.
    • Self-Reflection: You can use the same LLM as both the generator and the judge.

    Trade-offs to consider:

    • Cost: Requires additional LLM calls for the judge(s).
    • Latency: Evaluation steps increase the total response time.

    Available scoring templates include true_false_uncertain, true_false, continuous, and likert (refer to specific template documentation for implementation details).

  10. How UQLM ensembles work (Off-the-shelf vs. Tuned)

    main

    UQLM ensembles compute a weighted average of black-box, white-box, and LLM-as-a-Judge scorers. There are two ways to use them:

    Off-the-Shelf Ensemble

    Use this when you want immediate results without extra configuration. If no specific scorers are provided, the implementation defaults to an ensemble of:

    • exact match
    • non-contradiction probability
    • self-judge (Based on Chen & Mueller, 2023).

    Tunable Ensemble

    Use this to optimize weights for a specific use case. This requires more effort and a graded dataset.

    Tuning Process:

    1. Provide Data: Supply a list of prompts and corresponding ideal responses (the 'answer key').
    2. Grading: Responses are graded against the ideal answers. If you don't provide a custom grader, UQLM uses the vectara/hallucination_evaluation_model by default.
    3. Optimization: An optimization routine solves for optimal weights based on a classification objective:
      • Threshold-agnostic: e.g., ROC-AUC.
      • Threshold-dependent: e.g., F1-score.
    4. Deployment: The optimized weights are stored as class attributes for subsequent scoring.