GBNF Compiler (Python)
Dependency-free GBNF Compiler, plug and play for llama.cpp GBNF.
Simple yet powerful GBNF Compiler, use it like handlebars.js with better response from LLMs.
Why ?
GBNF is very useful to confine the response format from LLMs.
In most of the time, using sentence-based GBNF can produce better result than JSON-based GBNF in most LLMs without fine-tuning, gbnf-compiler provides the flexibility to construct or parse sentence / JSON / (anything you can think of) GBNF.
Getting Started
pip install gbnf-compiler
How to Use
- Define the LLM Response Template
- Create the Rule
- Send it out, done!
import requests
from gbnf_compiler import *
# Define your Prompt
prompt = "What tool will you use to calculate 2^5 ?"
# Define the LLM Response Template
# Each {{}} is a variable with a rule
template = "I choose {{tool}} because {{reason}}"
tools = MultipleChoice('tool', ['calculator', 'web-search', 'web-browse'])
c = GBNFCompiler(template, { 'tool': tools, 'reason': SingleSentence() })
print(c.grammar())
# Try a dummy result
text = "I choose calculator because it is the most efficient and accurate way to calculate 2^5."
result = c.parse(text)
print(result)
"""
Result:
{'tool': 'calculator', 'reason': 'it is the most efficient and accurate way to calculate 2^5.'}
"""
# Example: Send it out to local llama.cpp
def template(role: str, prompt: str):
return """[INST] <<SYS>>
{role}
<</SYS>>
{prompt}
[/INST]""".format(role=role, prompt=prompt)
data_json = {
"prompt": template("", prompt), "temperature": 0.0,
"n_predict": 512, "top_p": 0.2, "top_k": 10,
"stream": False, "grammar": c.grammar() }
resp = requests.post(
url="http://127.0.0.1:9999/completion",
headers={"Content-Type": "application/json"},
json=data_json,
)
result = resp.json()["content"]
print(c.parse(result))
Useful Rule Examples
-
ItemList: The Result will automatically compiled into a list of string.- Drawback: The LLM might keep generating more items until max tokens.
-
Point Form: Create a Point Form Result with specific numbers.- This can restrict the LLM to provide limited items.
Metadata
Release files for gbnf-compiler 0.3.2
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 |
|---|---|---|---|---|
| gbnf_compiler-0.3.2.tar.gz | 4.6 kB | Without provenance | Details for gbnf_compiler-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gbnf_compiler-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for gbnf_compiler-0.3.2-py3-none-any.whl |
Total release size: 10.2 kB
Release files / gbnf_compiler-0.3.2.tar.gz
| Download URL | gbnf_compiler-0.3.2.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88c50967b5e1d9a7dffee8209f624100c7b94b211001598ab9ab7fda941a6d8e
|
|
BLAKE2b-256 checksum How to use checksums |
9c482fbb8321b01925b2b32ea654e52ba841668d1079ad93c9935349932229e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / gbnf_compiler-0.3.2-py3-none-any.whl
| Download URL | gbnf_compiler-0.3.2-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
daa2b07e86b377b0e9b93e069a8221c8523dc4bbd8822cc5300129cf1a2930e4
|
|
BLAKE2b-256 checksum How to use checksums |
33d8245abc5f49e6ac496df2251b6a02bfb8368ceceacebaefbbb4c5321fb0e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for gbnf-compiler 0.3.2
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.