Skip to main content

Simple plug and play GBNF compiler for llama.cpp

Project description

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

  1. Define the LLM Response Template
  2. Create the Rule
  3. Send it out, done!
import requests
from gbnf_compiler.sentence import GBNFCompiler
from gbnf_compiler.rules 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}}"

# Define the Rule - "tools" for variable ("tool")
tools = MultipleChoice('tool', ['calculator', 'web-search', 'web-browse'])

# Create the GBNF Compiler
# Single Sentence is a default Grammar Rule which ends with '.'
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

  1. ItemList: The Result will automatically compiled into a list of string.

    • Drawback: The LLM might keep generating more items until max tokens.
  2. Point Form: Create a Point Form Result with specific numbers.

    • This can restrict the LLM to provide limited items.

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

gbnf_compiler-0.2.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

gbnf_compiler-0.2.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file gbnf_compiler-0.2.2.tar.gz.

File metadata

  • Download URL: gbnf_compiler-0.2.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for gbnf_compiler-0.2.2.tar.gz
Algorithm Hash digest
SHA256 50ecc3f558b0e138c9ddd9cd5377b57505f9dab199a12c2bebe09a87301ca186
MD5 1b2e65e1a338ea726f3701c6dac73d04
BLAKE2b-256 9b628811a54c57ebc6cdb7a47c5a57a5d33c54fdb1891481c14b887a714cd6b5

See more details on using hashes here.

File details

Details for the file gbnf_compiler-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for gbnf_compiler-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8858bfbba58e5db7627683d011e81036bc0303d0dd3f64389488f236f4d85945
MD5 daeb8eba530c3c16978e1e7104f8c1b2
BLAKE2b-256 93c16c019ff13c15c1e2a1723dc93045da21b568f0361e95ce44de0e5300acdd

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