Skip to content

Commit f7c9704

Browse files
author
ochafik
committed
common: add -jf / --json-schema-file flag
1 parent 51f311e commit f7c9704

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

common/arg.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,23 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
12721272
params.sampling.grammar = json_schema_to_grammar(json::parse(value));
12731273
}
12741274
).set_sparam());
1275+
add_opt(common_arg(
1276+
{"-jf", "--json-schema-file"}, "FILE",
1277+
"File containing a JSON schema to constrain generations (https://json-schema.org/), e.g. `{}` for any JSON object\nFor schemas w/ external $refs, use --grammar + example/json_schema_to_grammar.py instead",
1278+
[](common_params & params, const std::string & value) {
1279+
std::ifstream file(value);
1280+
if (!file) {
1281+
throw std::runtime_error(string_format("error: failed to open file '%s'\n", value.c_str()));
1282+
}
1283+
std::string schema;
1284+
std::copy(
1285+
std::istreambuf_iterator<char>(file),
1286+
std::istreambuf_iterator<char>(),
1287+
std::back_inserter(schema)
1288+
);
1289+
params.sampling.grammar = json_schema_to_grammar(json::parse(schema));
1290+
}
1291+
).set_sparam());
12751292
add_opt(common_arg(
12761293
{"--pooling"}, "{none,mean,cls,last,rank}",
12771294
"pooling type for embeddings, use model default if unspecified",

0 commit comments

Comments
 (0)