allow loading grammar from file
This commit is contained in:
parent
fd0eb663ce
commit
834d423edf
4 changed files with 36 additions and 0 deletions
|
@ -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<char>(file),
|
||||
std::istreambuf_iterator<char>(),
|
||||
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");
|
||||
|
|
6
grammars/arithmetic.gbnf
Normal file
6
grammars/arithmetic.gbnf
Normal file
|
@ -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]*
|
5
grammars/chess.gbnf
Normal file
5
grammars/chess.gbnf
Normal file
|
@ -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"?
|
8
grammars/json.gbnf
Normal file
8
grammars/json.gbnf
Normal file
|
@ -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 |
|
Loading…
Add table
Add a link
Reference in a new issue