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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| regex_llm-0.1.0.tar.gz | 2.8 kB | Without provenance | Details for regex_llm-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| regex_llm-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for regex_llm-0.1.0-py3-none-any.whl |
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
|
Security for regex-llm 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.