Skip to main content

Controlled language model generation using regex

Project description

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)

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

regex_llm-0.1.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

regex_llm-0.1.0-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: regex_llm-0.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1037-azure

File hashes

Hashes for regex_llm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d12b2d69d956fa7d3924cd551f3712a2629a0064fc0c0ef69cd53fc05a65afca
MD5 1a47ff47c3ecb5f06ac20a5e4fede517
BLAKE2b-256 137b5a6dd6bd808dc55ef97bef558198282502631ccceb43b5f6a76c81c3d6e7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: regex_llm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1037-azure

File hashes

Hashes for regex_llm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5d0b1ebbc57fd14ef7ddb43d8a0c1baf791aab4d65e21aa097651ebc082b52d
MD5 691484e081bec8dd0f4e799c66e2830b
BLAKE2b-256 fcb1a21f033c6c471befccb2e55b53320d86095d12f98c49b05fc1672409c1c2

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