mirror of
https://github.com/ollama/ollama.git
synced 2026-04-20 07:54:25 +02:00
1.7 KiB
1.7 KiB
Example Grammars
This directory contains example EBNF grammars for constrained decoding.
Usage
go run -tags mlx ./x/imagegen/cmd/engine/ \
-model /path/to/model \
-prompt "Your prompt" \
-grammar x/grammar/grammars/json.ebnf \
-grammar-start value
Available Grammars
| File | Start Rule | Description |
|---|---|---|
json.ebnf |
value |
Standard JSON (RFC 8259) |
expression.ebnf |
expr |
Arithmetic expressions (+, -, *, /, parens) |
identifier.ebnf |
ident |
Programming language identifiers |
boolean.ebnf |
expr |
Boolean expressions (AND, OR, NOT) |
list.ebnf |
list |
Comma-separated word list |
yesno.ebnf |
response |
Simple yes/no responses |
date.ebnf |
date |
Dates in YYYY-MM-DD format |
email.ebnf |
email |
Basic email addresses |
phone.ebnf |
phone |
US phone numbers |
hexcolor.ebnf |
color |
CSS hex colors (#RGB or #RRGGBB) |
url.ebnf |
url |
HTTP/HTTPS URLs |
Grammar Syntax
Note: Comments are not supported. Grammar files must contain only EBNF productions.
The grammars use EBNF notation:
=defines a production rule|is alternation (or){ }is repetition (zero or more)[ ]is optional (zero or one)" "is a literal string…is a character range (e.g.,"a" … "z").ends a production
Writing Custom Grammars
- Define your grammar in a
.ebnffile - Choose a start rule name
- Pass
-grammar path/to/grammar.ebnf -grammar-start rulename
Example custom grammar for RGB colors:
color = "#" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit .
hexdigit = "0" … "9" | "a" … "f" | "A" … "F" .