grammar

grammar#

Grammar allows to specify rules that make-up a text format. Gramar is generally used where regular expressions fall short, allowing to specify complex text structures. Grammar may be specified using specialised grammar languages. Langworks supports these languages through specialised constraint tags.

class langworks.dsl.constraints.grammar_gbnf.GBNF#

A constraint specifying a GBNF grammar.(-like) grammar that the LLM must conform to during generation. They may be embedded in queries as follows:

```
Present the number '7' with two leading zeroes: {% gbnf %}
root ::= "00" [1-9]
{% endgbnf %}.

Like all embeddable constraints, this contraint also accepts the default constraint arguments:

```
{% gbnf var = "num", params = Params(temperature = 0.0) %}...{% endgbnf %}
```

Properties#

spec: str = None#

The GBNF(-like) grammar that the generated content must conform to.

class langworks.dsl.constraints.grammar_lark.Lark#

A constraint specifying a Lark(-like) grammar that the LLM must conform to during generation. They may be embedded in queries as follows:

```
Present the number '7' with two leading zeroes: {% lark %}
?start: "0" "0" NUMBER
%import common.NUMBER
{% endlark %}.
```

Like all embeddable constraints, this contraint also accepts the default constraint arguments:

```
{% lark var = "num", params = Params(temperature = 0.0) %}...{% endlark %}
```

Properties#

spec: str = None#

The Lark(-like) grammar that the generated content must conform to.