Skip to main content

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

Project description

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!

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.

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.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

logits_processor_zoo-0.1.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for logits_processor_zoo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ab4af322fbb45dc80554bb1a1d6945562a2c386b011ef66f2a4c7ccb3f07c9c3
MD5 18d87fc5ace121eb1a1cf8a4e6dfb84f
BLAKE2b-256 73192037379dda8e2e1cd75255cf30d68f8b7ab09264d9ef032b605ecc44156a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for logits_processor_zoo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec8eb48ae79e819168cfc97f413d26bd42ccec39394e835c58fff5065adf6b20
MD5 11a6815093b8eae6aefef0e7e22117b3
BLAKE2b-256 d5175cddf7cbd856b6386fdcf6193d5f5f38a9eb7ad6c1c4d5a69c06d22a2b63

See more details on using hashes here.

Supported by

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