Skip to main content

regex llm

Constrained language model generation with regex.

Installation

pip install regex-llm

Usage

import regex
from transformers import (
    AutoTokenizer,
    GenerationMixin,
    PreTrainedTokenizer,
    AutoModelForCausalLM,
)

from regex_llm import RegexConstraint

model: GenerationMixin = AutoModelForCausalLM.from_pretrained("your model")
tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained("your model")

controller = RegexConstraint(tokenizer=tokenizer, model=model)

prompt = '帮我生成3种不同的水果\n输出json格式如下:\n[{"name":"xxx","description":"xxx"}]'
pattern = r"\[(,?\s*\{\"name\":\s*\"[^\"]+\",\s*\"description\":\s*\"[^\"]+\"\\s*})+\s*\]"
output = controller.generate(
    prompt=prompt,
    pattern=pattern,
    do_sample=True,
    num_beams=1,
    top_p=0.7,
    temperature=0.95,
)
print(output)

Metadata

Release files for regex-llm 0.1.0

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

Source distribution (sdist)

Source distribution for regex-llm 0.1.0
File Size Uploaded
regex_llm-0.1.0.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for regex-llm 0.1.0
File Interpreter ABI Platform
regex_llm-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.7 kB

Release files / regex_llm-0.1.0.tar.gz

Download URL regex_llm-0.1.0.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d12b2d69d956fa7d3924cd551f3712a2629a0064fc0c0ef69cd53fc05a65afca
BLAKE2b-256 checksum
How to use checksums
137b5a6dd6bd808dc55ef97bef558198282502631ccceb43b5f6a76c81c3d6e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1037-azure

Release files / regex_llm-0.1.0-py3-none-any.whl

Download URL regex_llm-0.1.0-py3-none-any.whl
Size 2.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5d0b1ebbc57fd14ef7ddb43d8a0c1baf791aab4d65e21aa097651ebc082b52d
BLAKE2b-256 checksum
How to use checksums
fcb1a21f033c6c471befccb2e55b53320d86095d12f98c49b05fc1672409c1c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1037-azure

Release history Release notifications | RSS feed

This release

0.1.0 This release

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