Skip to content

Commit 14a4388

Browse files
author
ochafik
committed
optionally allow any spaces in json schema grammars (useful for llama3 8b tool outputs)
1 parent a456911 commit 14a4388

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

common/chat.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ std::string common_chat_format_name(common_chat_format format) {
451451

452452
const common_grammar_options grammar_options {
453453
/* .dotall = */ false,
454-
/* .compact_spaces = */ false,
455-
// /* .compact_spaces = */ true,
454+
/* .any_spaces = */ true,
456455
};
457456

458457
static std::optional<json> parse_json(std::string::const_iterator & it, const std::string::const_iterator & end) {

common/json-schema-to-grammar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,10 @@ class SchemaConverter {
765765
SchemaConverter(
766766
const std::function<json(const std::string &)> & fetch_json,
767767
bool dotall,
768-
bool compact_spaces)
768+
bool any_spaces)
769769
: _fetch_json(fetch_json), _dotall(dotall)
770770
{
771-
_rules["space"] = compact_spaces ? "\" \"?" : SPACE_RULE;
771+
_rules["space"] = any_spaces ? "[ \\t\\n]*" : SPACE_RULE;
772772
}
773773

774774
void resolve_refs(json & schema, const std::string & url) {
@@ -1007,7 +1007,7 @@ std::string json_schema_to_grammar(const json & schema, bool force_gbnf) {
10071007
}
10081008

10091009
std::string build_grammar(const std::function<void(const common_grammar_builder &)> & cb, const common_grammar_options & options) {
1010-
SchemaConverter converter([&](const std::string &) { return json(); }, options.dotall, options.compact_spaces);
1010+
SchemaConverter converter([&](const std::string &) { return json(); }, options.dotall, options.any_spaces);
10111011
common_grammar_builder builder {
10121012
/* .add_rule = */ [&](const std::string & name, const std::string & rule) {
10131013
return converter._add_rule(name, rule);

common/json-schema-to-grammar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct common_grammar_builder {
1616

1717
struct common_grammar_options {
1818
bool dotall = false;
19-
bool compact_spaces = false;
19+
bool any_spaces = false;
2020
};
2121

2222
std::string build_grammar(const std::function<void(const common_grammar_builder &)> & cb, const common_grammar_options & options = {});

scripts/tool_bench.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ cmake --build build -j
88

99
export LLAMA_CACHE=${LLAMA_CACHE:-$HOME/Library/Caches/llama.cpp}
1010
export LLAMA_SERVER_BIN_PATH=$PWD/build/bin/llama-server
11-
export LLAMA_SERVER_BASELINE=$(which llama-server)
1211

1312
if [ ! -x "$LLAMA_SERVER_BIN_PATH" ]; then
1413
echo "Could not find llama-server binary at $LLAMA_SERVER_BIN_PATH"
@@ -20,7 +19,7 @@ if [ ! -d "$LLAMA_CACHE" ]; then
2019
fi
2120

2221
export ARGS=(
23-
--llama-baseline="$LLAMA_SERVER_BASELINE"
22+
--llama-baseline="$(which llama-server)"
2423
--n 30
2524
--temp -1 # Leaves temperature parameter unset (use the server's default, e.g. 0.6 for ollama)
2625
--temp 0

0 commit comments

Comments
 (0)