Skip to main content

Customize, control, and enhance LLM generation with logits processors, featuring visualization capabilities to inspect and understand state transitions

Project description

PyPI version License: MIT

litelines

Customize, control, and enhance LLM generation with logits processors, featuring visualization capabilities to inspect and understand state transitions.

Installation

pip install litelines

Dependencies

The only dependency is outlines-core.

Supported Frameworks

  • transformers

Basic Usage

  • Define a logits processor through a Pydantic schema or a regular expression and visualize it:
from litelines.transformers import SchemaProcessor

processor = SchemaProcessor(r"Yes\.|No\.", tokenizer)
processor.show_graph()
  • Generate a structured answer:
from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda:0")

messages = [{"role": "user", "content": "Are you sentient?"}]
inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(model.device)
generated = model.generate(**inputs, logits_processor=[processor])
print(tokenizer.decode(generated[0][inputs['input_ids'].shape[-1]:]))
# No.
  • Visualize the selected path:
processor.show_graph()

100% Guaranteed Valid JSON answer

  • Define a pydantic schema describing the required JSON or provide the JSON schema as a string:
from typing import Literal
from pydantic import BaseModel, Field

class Sentiment(BaseModel):
    """Correctly inferred `Sentiment` with all the required parameters with correct types."""
    label: Literal["positive", "negative"] = Field(
        ..., description="Sentiment of the text"
    )

'''
Alternatively, provide the JSON schema as a sting:
Sentiment = """{'description': 'Correctly inferred `Sentiment` with all the required parameters with correct types.',
 'properties': {'label': {'description': 'Sentiment of the text',
   'enum': ['positive', 'negative'],
   'title': 'Label',
   'type': 'string'}},
 'required': ['label'],
 'title': 'Sentiment',
 'type': 'object'}"""
'''
  • Define the processor and visualize it:
from litelines.transformers import SchemaProcessor

processor = SchemaProcessor(Sentiment, tokenizer)
processor.show_graph()
  • Generate a structured answer:
from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda:0")

messages = [{"role": "user", "content": "What is the sentiment of the following text: 'Awesome'"}]
inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(model.device)
generated = model.generate(**inputs, logits_processor=[processor])
print(tokenizer.decode(generated[0][inputs['input_ids'].shape[-1]:]))
# {"label": "positive"}
  • Visualize the selected path:
processor.show_graph()

100% Guaranteed Valid Tool Calling answer

  • Define a pydantic schema describing the tool:
from typing import Literal
from pydantic import BaseModel, Field


class Sentiment(BaseModel):
    """Correctly inferred `Sentiment` with all the required parameters with correct types."""
    label: Literal["positive", "negative"] = Field(
        ..., description="Sentiment of the text"
    )
  • Define the processor, add the parameter include_tool_call=True and visualize it:
from litelines.transformers import SchemaProcessor

processor = SchemaProcessor(Sentiment, tokenizer, include_tool_call=True)
processor.show_graph()
  • Generate a structured answer:
from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda:0")

messages = [{"role": "user", "content": "What is the sentiment of the following text: 'Awesome'"}]
inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(model.device)
generated = model.generate(**inputs, logits_processor=[processor])
print(tokenizer.decode(generated[0][inputs['input_ids'].shape[-1]:]))
# <tool_call>
# {"name": "Sentiment", "arguments": {"label": "positive"}}
# </tool_call>
  • Visualize the selected path:
processor.show_graph()

Allow Preamble but still get 100% Guaranteed Valid JSON/Tool Calling answer

  • Define a pydantic schema describing the required JSON or provide the JSON schema as a string:
from typing import Literal
from pydantic import BaseModel, Field

class Sentiment(BaseModel):
    """Correctly inferred `Sentiment` with all the required parameters with correct types."""
    label: Literal["positive", "negative"] = Field(
        ..., description="Sentiment of the text"
    )
  • Define the processor, add parameter allow_preamble=True and visualize it:
from litelines.transformers import SchemaProcessor

processor = SchemaProcessor(Sentiment, tokenizer, allow_preamble=True)
processor.show_graph()
  • Generate a structured answer:
from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda:0")

messages = [{"role": "user", "content": "What is the sentiment of the following text: 'Awesome'"}]
inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(model.device)
generated = model.generate(**inputs, logits_processor=[processor])
print(tokenizer.decode(generated[0][inputs['input_ids'].shape[-1]:]))
# The sentiment of the text "Awesome" is positive.
# {"label": "positive"}
  • Visualize the selected path:
processor.show_graph()

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

litelines-0.1.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

litelines-0.1.1-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file litelines-0.1.1.tar.gz.

File metadata

  • Download URL: litelines-0.1.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for litelines-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0e967a0cb54d8b2cae15d59f0ebec47290306927b8ed74adf692cd149de0312f
MD5 3ec7e941c6f387bc8630fe1669d802c5
BLAKE2b-256 9623edf48f9bb94057cec307c1a4422d79c19b7a53596dae328603b244b7caed

See more details on using hashes here.

File details

Details for the file litelines-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: litelines-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for litelines-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ecae16294ed8ffc586bc0971e159789988fb75e2fab75cd0b2707cc221cbfe5
MD5 9ddf44142b6351e3ffe1cafd065dc46d
BLAKE2b-256 2ca15497dfe5e74a18e2172ab5dbe00a094ac64c7092984a0268374ac5bf8c8d

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