Skip to main content

A fast constrained decoding engine based on context free grammar.

Project description

kbnf

crates.io docs.rs PyPI CI PyPI Downloads

This crate provides a constrained decoding engine which ensures that a language model's output adheres strictly to the format defined by KBNF (Koishi's BNF), an enhanced variant of EBNF. KBNF includes features that enhance usability, notably embeddable regular expressions.

If you are interested in the design and implementation behind this crate, you may want to check out my blog.

Features

  • Supports full context free grammar with worst case O(m*n^3) time complexity, where n is the generated text length and m is the vocabulary size.
  • Asymptotically fastest for subclasses of context free grammar.
    • Guarantees worst case O(m*n) time complexity for every LR(k) grammar(which includes almost all practical grammars)
    • Achieves O(n) time complexity with caching eventually given that n has a fixed upper bound, or the grammar is regular.
  • Vocabulary-independent.
    • BPE, BBPE, you-name-it, all types of vocabulary are supported.
  • Supports UTF-8 characters in grammar.
  • Embeddable regular expressions.

Documentation

Documentation and examples.

Add to your project

Simply add it to your Cargo.toml or run cargo add kbnf in your command line.

Performance

One of the goals of this crate is for the constrained decoding engine to be "fast." This can be interpreted both theoretically and practically.

Theoretically, this crate is designed to provide the asymptotically fastest algorithms for each subclass of context free grammar. By implementing an Earley recognizer with Leo optimization, this crate has successfully achieve linear time complexity for every LR(k) grammar and quadratic time complexity for every unambiguous grammar. For general context free grammar, things are more ambiguous(pun intended): while subcubic algorithms exist(although with a large constant), all other general-purpose parsing algorithms(like Earley, GLR, GLL...) are indeed cubic, like ours.

Practically, this crate tries to make the engine be as efficient as possible for grammars used in practice. While many improvements, such as Earley sets compaction and lazy caching, have been made, this is inherently an ongoing process. If you find the engine is a bottleneck in your application, feel free to open an issue.

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

kbnf-0.4.2.tar.gz (3.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

kbnf-0.4.2-cp37-abi3-win_amd64.whl (902.7 kB view details)

Uploaded CPython 3.7+Windows x86-64

kbnf-0.4.2-cp37-abi3-musllinux_1_2_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ x86-64

kbnf-0.4.2-cp37-abi3-musllinux_1_2_aarch64.whl (1.3 MB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

kbnf-0.4.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

kbnf-0.4.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

kbnf-0.4.2-cp37-abi3-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

kbnf-0.4.2-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (2.1 MB view details)

Uploaded CPython 3.7+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file kbnf-0.4.2.tar.gz.

File metadata

  • Download URL: kbnf-0.4.2.tar.gz
  • Upload date:
  • Size: 3.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for kbnf-0.4.2.tar.gz
Algorithm Hash digest
SHA256 68d677fd21e64d1d5748933bac74b4c3f3ed7f4adfcd2bf66ebf491c4e9eca6a
MD5 b17d75967734830a7dc309d504113e78
BLAKE2b-256 091ec8589f5437769cbb4a240ca88cac905099333f01f8b8569fe6575e2057f2

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: kbnf-0.4.2-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 902.7 kB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3a9be9659d0892bdf632c75d6bd964dd8391e869d4a9a528667fb851a5df5415
MD5 79de886bb96823b22f514a869daa03bc
BLAKE2b-256 f3512848a82589d22531a548417c85a9c584ac4c605236971565887864298556

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: kbnf-0.4.2-cp37-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.7+, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 31def96dc90eaed688278f3f1e317d8d25d96e6a7394ad47bf90cb06901f95a8
MD5 fd24f1e60b32e70aa8c5950ce12e75de
BLAKE2b-256 0864050b562e9b4ce99a0ae33ec621585aadc2649d5377c97b18975c18d3eb43

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 ae0220c692beab721012387240050ffb386203f812bbc2529531df313bc01bbc
MD5 f86cce1a971b68bf088bfbc17cb953ea
BLAKE2b-256 d9a21a642fa98be8018603c5bf4dad5a533617afb27be93cdefddbf530d8de10

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a738ad9a27aef66af112a1e720213582cf5123701ec2e24a3c070a97c0729bc1
MD5 cc8c6f1c94d2a40553d27ce11905afb1
BLAKE2b-256 0268da3844f700341082dbf9eff226a217d03349073e7d1e80dc86923d3448d4

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f6137e22e209264bf235f7c320661dcc9a0556d892de65a7f5a4cf8ae5ca2596
MD5 b7800f0f2b1b1f7873f1beb2b5a46f9f
BLAKE2b-256 03d9705763dc749d3820e7876102d4b6d9674d0c9fbd4ac49dc993482e80b914

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: kbnf-0.4.2-cp37-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.7+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 da5e42660c2458d87d58fcc0645ec739a31707382a910fc43a0dbcdce9f5a6ad
MD5 baa7b495a9c765c8c0311a8f513ef234
BLAKE2b-256 0bd07a9d1ae676e6a3aa0488cf3bf355b4ddaa13ce0ebff41ef4afe8a5cdf647

See more details on using hashes here.

File details

Details for the file kbnf-0.4.2-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for kbnf-0.4.2-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 37ad93831ee5e505312708d33b49cba9c454baf0956ed5127c9da998ddfb3434
MD5 218e9103cd4cae8a80fb33497a88565d
BLAKE2b-256 cd01307a1077caccdc6e06055913119eed232c1b813739bd27ba84b6b3dee7c9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page