diff --git a/examples/common.cpp b/examples/common.cpp index b20a68260..d68bd4ba7 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -394,6 +394,22 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { break; } params.grammar = argv[i]; + } else if (arg == "--grammar-file") { + if (++i >= argc) { + invalid_param = true; + break; + } + std::ifstream file(argv[i]); + if (!file) { + fprintf(stderr, "error: failed to open file '%s'\n", argv[i]); + invalid_param = true; + break; + } + std::copy( + std::istreambuf_iterator(file), + std::istreambuf_iterator(), + std::back_inserter(params.grammar) + ); } else { fprintf(stderr, "error: unknown argument: %s\n", arg.c_str()); gpt_print_usage(argc, argv, default_params); @@ -465,6 +481,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) { fprintf(stderr, " i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"); fprintf(stderr, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n"); fprintf(stderr, " --grammar GRAMMAR BNF-like grammar (TODO explain) to constrain generations\n"); + fprintf(stderr, " --grammar-file FNAME file to read grammar from\n"); fprintf(stderr, " -c N, --ctx-size N size of the prompt context (default: %d)\n", params.n_ctx); fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating (implies --logit-bias 2-inf)\n"); fprintf(stderr, " --no-penalize-nl do not penalize newline token\n"); diff --git a/grammars/arithmetic.gbnf b/grammars/arithmetic.gbnf new file mode 100644 index 000000000..3aa95a9dd --- /dev/null +++ b/grammars/arithmetic.gbnf @@ -0,0 +1,6 @@ +root ::= (expr "=" ws term "\n")+ +expr ::= term ([-+*/] term)* +term ::= ident | num | "(" ws expr ")" ws +ident ::= [a-z] [a-z0-9_]* ws +num ::= [0-9]+ ws +ws ::= [ \t\n]* diff --git a/grammars/chess.gbnf b/grammars/chess.gbnf new file mode 100644 index 000000000..2da6b7139 --- /dev/null +++ b/grammars/chess.gbnf @@ -0,0 +1,5 @@ +root ::= "1. " move " " move "\n" ([1-9] [0-9]? ". " move " " move "\n")+ +move ::= (pawn | nonpawn | castle) [+#]? +nonpawn ::= [NBKQR] [a-h]? [1-8]? "x"? [a-h] [1-8] +pawn ::= ([a-h] "x")? [a-h] [1-8] ("=" [NBKQR])? +castle ::= "O-O" "-O"? diff --git a/grammars/json.gbnf b/grammars/json.gbnf new file mode 100644 index 000000000..cba085d97 --- /dev/null +++ b/grammars/json.gbnf @@ -0,0 +1,8 @@ +root ::= object | array +value ::= object | array | string | number | boolean +object ::= "{" ws (string ":" ws value ("," ws string ":" ws value)*)? "}" +array ::= "[" ws (value ("," ws value)*)? "]" +string ::= "\"" [ \t!#-\[\]-~]* "\"" ws +number ::= [0-9]+ ws +boolean ::= ("true" | "false") ws +ws ::= [ \t\t] ws |