Library for running inference on large language models with the ability to remove generated tokens.
Project description
Backtracking LLM
A Python library for running large language models with a backtracking mechanism, allowing the model to dynamically revise and "undo" its own generated tokens to improve output quality.
This project is the official implementation accompanying the research presented at AI conferences.
Core Concepts
Standard autoregressive language models generate text one token at a time, and each token is chosen irreversibly. This can lead to compounding errors, where a single poor token choice results in a low-quality or nonsensical completion.
Backtracking LLM introduces a "self-correction" step into the generation
loop. After a token is sampled, a decision function (an Operator) evaluates
the choice. If the choice is deemed low-quality (e.g., it's a repetitive token
or the model's confidence was too low), the generator can "backtrack,"
effectively erasing the last N tokens and attempting a different generation
path.
This is managed by two primary components:
Generator: The core engine that wraps atransformersmodel and tokenizer. It manages the token-by-token generation loop, including the stateful KV cache, and executes the backtracking logic when instructed.Operator: A pluggable rule that decides when to backtrack. The library provides a suite of operators based on different heuristics, such as token probability, distribution entropy, and repetition.
Features
- Backtracking Mechanism: A simple yet powerful way to add a self-correction loop to standard LLM inference.
- Pluggable Decision Operators: A collection of built-in rules for controlling backtracking, from simple probability thresholds to n-gram overlap detection.
- High-Level Chat Pipeline: A stateless
ChatPipelinethat correctly handles multi-turn conversations using model-specific chat templates. - Interactive CLI: A user-friendly command-line interface (
backtracking-llm) for interactively chatting with any Hugging Face model, with full support for configuring backtracking. - Built on
transformers: Fully compatible with the Hugging Face ecosystem, allowing you to use thousands of pretrained models.
Installation
This library requires Python 3.9+.
pip install backtracking-llm
Quickstart
1. Interactive Chat (CLI)
The easiest way to get started is with the built-in interactive CLI. Simply provide a model name from the Hugging Face Hub.
Basic Usage:
backtracking-llm "Qwen/Qwen2.5-0.5B-Instruct"
Usage with a Backtracking Operator:
This example will load the model and use the Repetition operator to prevent
the model from repeating the same token more than twice.
backtracking-llm "Qwen/Qwen2.5-0.5B-Instruct" --operator repetition
2. Library Usage in Python
You can easily integrate the Generator into your own Python projects.
import logging
from backtracking_llm.generation import Generator
from backtracking_llm.decision import Repetition
logging.basicConfig(level=logging.INFO)
generator = Generator.from_pretrained('gpt2')
repetition_operator = Repetition(max_repetitions=2)
prompt = ('The best thing about AI is its ability to learn and adapt. For '
'example, AI can learn to play games, write stories, and even create'
'art. This is because AI is constantly learning, learning, learning')
completion = generator.generate(
prompt,
operator=repetition_operator,
max_new_tokens=50,
backtrack_every_n=1
)
print(f"\nPrompt: {prompt}")
print(f"Completion: {completion}")
Available Backtracking Operators
You can pass any of these operators to the Generator or select them in the CLI
via the --operator flag.
| Operator | Description |
|---|---|
ProbabilityThreshold |
Backtracks if a chosen token's probability is below a threshold. |
EntropyThreshold |
Backtracks if the probability distribution is too uncertain (high entropy). |
ProbabilityMargin |
Backtracks if the confidence margin between the top two tokens is too small. |
ProbabilityDrop |
Backtracks if the probability drops sharply compared to the previous token. |
ProbabilityTrend |
Backtracks if probability drops below a moving average of recent tokens. |
Repetition |
Backtracks on excessive consecutive token repetitions. |
NGramOverlap |
Backtracks when a sequence of N tokens is repeated. |
LogitThreshold |
Backtracks if a chosen token's raw logit value is below a threshold. |
Contributing
Contributions are welcome! Please see the CONTRIBUTING.md file.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file backtracking_llm-0.1.0.tar.gz.
File metadata
- Download URL: backtracking_llm-0.1.0.tar.gz
- Upload date:
- Size: 898.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9c344097e9464b04f9be8425bd8ba37e397458dcd532e8353050bd92ca90091
|
|
| MD5 |
f5ca677660639b3c6adeaea74baa06ef
|
|
| BLAKE2b-256 |
43d7652d34c5a2c8cacf73d411adb6015a7c67e4ef3ca398f0620a95fd204d2f
|
Provenance
The following attestation bundles were made for backtracking_llm-0.1.0.tar.gz:
Publisher:
python-publish.yml on matee8/backtracking_llm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
backtracking_llm-0.1.0.tar.gz -
Subject digest:
d9c344097e9464b04f9be8425bd8ba37e397458dcd532e8353050bd92ca90091 - Sigstore transparency entry: 469559604
- Sigstore integration time:
-
Permalink:
matee8/backtracking_llm@1878d1b1a7d55a7ff5fb6b078fa35818883249d4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/matee8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1878d1b1a7d55a7ff5fb6b078fa35818883249d4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file backtracking_llm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: backtracking_llm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b5631c2fe69d39a13e4026e4ff9c31ea25560398365b397720664a712e493eb
|
|
| MD5 |
2566c57183501cd98e23fdc262f41f8c
|
|
| BLAKE2b-256 |
b931d22238119cc7a6d0ed8669b886c6fcf5a2deab28864220e197139c6c85ba
|
Provenance
The following attestation bundles were made for backtracking_llm-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on matee8/backtracking_llm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
backtracking_llm-0.1.0-py3-none-any.whl -
Subject digest:
1b5631c2fe69d39a13e4026e4ff9c31ea25560398365b397720664a712e493eb - Sigstore transparency entry: 469559614
- Sigstore integration time:
-
Permalink:
matee8/backtracking_llm@1878d1b1a7d55a7ff5fb6b078fa35818883249d4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/matee8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1878d1b1a7d55a7ff5fb6b078fa35818883249d4 -
Trigger Event:
push
-
Statement type: