Skip to main content

PyPI version License: MIT

logits-processor-zoo

Struggling to get LLMs to follow your instructions? LogitsProcessorZoo offers a zoo of tools to use LLMs for specific tasks, beyond just grammar enforcement!

Installation

pip install logits-processor-zoo

Supported Frameworks

  • transformers
  • vLLM
  • TensorRT-LLM (>=0.20.0)

Usage

import vllm
from logits_processor_zoo.vllm import GenLengthLogitsProcessor, CiteFromPromptLogitsProcessor, ForceLastPhraseLogitsProcessor

model = vllm.LLM(
            model_name,
            trust_remote_code=True,
            dtype="half",
            enforce_eager=True
        )
tokenizer = model.get_tokenizer()
        
logits_processors = [
    CiteFromPromptLogitsProcessor(tokenizer, boost_factor=2.0),
    GenLengthLogitsProcessor(tokenizer, boost_factor=-0.2, p=1),
    ForceLastPhraseLogitsProcessor("\n\nReferences:\n", tokenizer)
]


gen_output = model.generate(
            prompts,
            vllm.SamplingParams(
                n=1,
                temperature=0,
                seed=0,
                skip_special_tokens=True,
                max_tokens=64,
                logits_processors=logits_processors
            ),
            use_tqdm=False
        )

For the detailed examples in each framework, please have a look at lpz_examples directory.

Available Logits Processors

GenLengthLogitsProcessor

A logits processor that adjusts the likelihood of the end-of-sequence (EOS) token based on the length of the generated sequence, encouraging or discouraging shorter answers.

CiteFromPromptLogitsProcessor

A logits processor which boosts or diminishes the likelihood of tokens present in the prompt (and optionally EOS token) to encourage the model to generate tokens similar to those seen in the prompt or vice versa.

ForceLastPhraseLogitsProcessor

A logits processor which forces LLMs to use the given phrase before they finalize their answers. Most common use cases can be providing references, thanking user with context etc.

MultipleChoiceLogitsProcessor

A logits processor to answer multiple choice questions with one of the choices. A multiple choice question is like:

I am getting a lot of calls during the day. What is more important for me to consider when I buy a new phone?
0. Camera
1. Screen resolution
2. Operating System
3. Battery

The goal is to make LLM generate "3" as an answer.

TriggerPhraseLogitsProcessor

A logits processor which triggers phrases when it encounters a given token or after a specified time. One common use case is to force writing python code just after thinking:

trigger_python = TriggerPhraseLogitsProcessor(phrase="\n```python", trigger_token_phrase="</think>", 
                                              tokenizer=tokenizer, trigger_count=1, trigger_after=True)

PreventHallucinationLogitsProcessor

A logits processor that mitigates hallucinated model outputs by enforcing a predefined fallback phrase when token confidence falls below a specified threshold.

MaxTimeLogitsProcessor

A logits processor that enforces the end-of-sentence (EOS) token after a specified maximum time passes, optionally waiting for a new line or a full stop. Useful for controlling generation time and ensuring responses complete within time constraints.

Metadata

Release files for logits-processor-zoo 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logits-processor-zoo 0.2.1
File Size Uploaded
logits_processor_zoo-0.2.1.tar.gz 19.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logits-processor-zoo 0.2.1
File Interpreter ABI Platform
logits_processor_zoo-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 64.3 kB

Release files / logits_processor_zoo-0.2.1.tar.gz

Download URL logits_processor_zoo-0.2.1.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f67c2641aa08dddb93b780633d0cd7b2804e4d3439d5dcc004fd0440b99deabb
BLAKE2b-256 checksum
How to use checksums
cc918035fec7a3d30d888b02fb15855912ef3eeff28610d24327ecf960e89ef1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.17 Linux/5.4.95-050495-generic

Release files / logits_processor_zoo-0.2.1-py3-none-any.whl

Download URL logits_processor_zoo-0.2.1-py3-none-any.whl
Size 44.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cfca87212cc5e1f5fc757938c9241a2f1e8c20a064efe8ac9f30a9ddd7e7205a
BLAKE2b-256 checksum
How to use checksums
dd45c2d82e58f52261bd49e8038676096804848e7b5576e42985b459686b782e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.17 Linux/5.4.95-050495-generic

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page