# regex llm
Constrained language model generation with regex.
## Installation
```bash
pip install regex-llm
```
## Usage
```python
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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/yanyongyu/regex-llm",
"name": "regex-llm",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "llm,regex,language model,generation",
"author": "yanyongyu",
"author_email": "yyy@yyydl.top",
"download_url": "https://files.pythonhosted.org/packages/13/7b/5a6dd6bd808dc55ef97bef558198282502631ccceb43b5f6a76c81c3d6e7/regex_llm-0.1.0.tar.gz",
"platform": null,
"description": "# regex llm\n\nConstrained language model generation with regex.\n\n## Installation\n\n```bash\npip install regex-llm\n```\n\n## Usage\n\n```python\nimport regex\nfrom transformers import (\n AutoTokenizer,\n GenerationMixin,\n PreTrainedTokenizer,\n AutoModelForCausalLM,\n)\n\nfrom regex_llm import RegexConstraint\n\nmodel: GenerationMixin = AutoModelForCausalLM.from_pretrained(\"your model\")\ntokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(\"your model\")\n\ncontroller = RegexConstraint(tokenizer=tokenizer, model=model)\n\nprompt = '\u5e2e\u6211\u751f\u62103\u79cd\u4e0d\u540c\u7684\u6c34\u679c\\n\u8f93\u51fajson\u683c\u5f0f\u5982\u4e0b\uff1a\\n[{\"name\":\"xxx\",\"description\":\"xxx\"}]'\npattern = r\"\\[(,?\\s*\\{\\\"name\\\":\\s*\\\"[^\\\"]+\\\",\\s*\\\"description\\\":\\s*\\\"[^\\\"]+\\\"\\\\s*})+\\s*\\]\"\noutput = controller.generate(\n prompt=prompt,\n pattern=pattern,\n do_sample=True,\n num_beams=1,\n top_p=0.7,\n temperature=0.95,\n)\nprint(output)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Controlled language model generation using regex",
"version": "0.1.0",
"project_urls": {
"Documentation": "https://github.com/yanyongyu/regex-llm",
"Homepage": "https://github.com/yanyongyu/regex-llm",
"Repository": "https://github.com/yanyongyu/regex-llm"
},
"split_keywords": [
"llm",
"regex",
"language model",
"generation"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fcb1a21f033c6c471befccb2e55b53320d86095d12f98c49b05fc1672409c1c2",
"md5": "691484e081bec8dd0f4e799c66e2830b",
"sha256": "f5d0b1ebbc57fd14ef7ddb43d8a0c1baf791aab4d65e21aa097651ebc082b52d"
},
"downloads": -1,
"filename": "regex_llm-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "691484e081bec8dd0f4e799c66e2830b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 2946,
"upload_time": "2023-05-30T07:10:18",
"upload_time_iso_8601": "2023-05-30T07:10:18.918427Z",
"url": "https://files.pythonhosted.org/packages/fc/b1/a21f033c6c471befccb2e55b53320d86095d12f98c49b05fc1672409c1c2/regex_llm-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "137b5a6dd6bd808dc55ef97bef558198282502631ccceb43b5f6a76c81c3d6e7",
"md5": "1a47ff47c3ecb5f06ac20a5e4fede517",
"sha256": "d12b2d69d956fa7d3924cd551f3712a2629a0064fc0c0ef69cd53fc05a65afca"
},
"downloads": -1,
"filename": "regex_llm-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "1a47ff47c3ecb5f06ac20a5e4fede517",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 2784,
"upload_time": "2023-05-30T07:10:20",
"upload_time_iso_8601": "2023-05-30T07:10:20.966018Z",
"url": "https://files.pythonhosted.org/packages/13/7b/5a6dd6bd808dc55ef97bef558198282502631ccceb43b5f6a76c81c3d6e7/regex_llm-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-30 07:10:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yanyongyu",
"github_project": "regex-llm",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "regex-llm"
}