Skip to main content

A collection of LogitsProcessors to customize and enhance LLM behavior for specific tasks.

Project description

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

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 example_notebook 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. 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)

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

logits_processor_zoo-0.1.10.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logits_processor_zoo-0.1.10-py3-none-any.whl (31.7 kB view details)

Uploaded Python 3

File details

Details for the file logits_processor_zoo-0.1.10.tar.gz.

File metadata

  • Download URL: logits_processor_zoo-0.1.10.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.17 Linux/5.4.95-050495-generic

File hashes

Hashes for logits_processor_zoo-0.1.10.tar.gz
Algorithm Hash digest
SHA256 4d7681e0db3e739012d79227befe908a4dc6c0336890e2584876e546406f5520
MD5 7bc1d20b498dfc906e569ee1d278f6f2
BLAKE2b-256 727fec12f524788c01aeb1beb79faac8f08a2885c3d061e60226744cf4df4f23

See more details on using hashes here.

File details

Details for the file logits_processor_zoo-0.1.10-py3-none-any.whl.

File metadata

  • Download URL: logits_processor_zoo-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 31.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.17 Linux/5.4.95-050495-generic

File hashes

Hashes for logits_processor_zoo-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2b9f67b4a11221348970a67f63ec8310ea6a71bf8cd965d69498292f61289fb4
MD5 cd68cece7862ee70887b5b3c96258f3b
BLAKE2b-256 1e2df3c1d887202c33c2252b4bb428a8507fcaa908f9d40a4e6c6ebd994c02c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page