json: refactor to surface a versatile builder
This commit is contained in:
parent
9a5acbb4a3
commit
4de5cf8a10
2 changed files with 30 additions and 5 deletions
|
@ -991,10 +991,27 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string json_schema_to_grammar(const json & schema) {
|
std::string json_schema_to_grammar(const json & schema) {
|
||||||
SchemaConverter converter([](const std::string &) { return json::object(); }, /* dotall= */ false);
|
return build_grammar([&](const llama_grammar_builder & callbacks) {
|
||||||
auto copy = schema;
|
auto copy = schema;
|
||||||
converter.resolve_refs(copy, "input");
|
callbacks.resolve_refs(copy);
|
||||||
converter.visit(copy, "");
|
callbacks.add_schema("", copy);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string build_grammar(const std::function<void(const llama_grammar_builder &)> & cb) {
|
||||||
|
SchemaConverter converter([&](const std::string &) { return json(); }, /* dotall= */ false);
|
||||||
|
llama_grammar_builder builder {
|
||||||
|
/* .add_rule = */ [&](const std::string & name, const std::string & rule) {
|
||||||
|
return converter._add_rule(name, rule);
|
||||||
|
},
|
||||||
|
/* .add_schema = */ [&](const std::string & name, const nlohmann::ordered_json & schema) {
|
||||||
|
return converter.visit(schema, name == "root" ? "" : name);
|
||||||
|
},
|
||||||
|
/* .resolve_refs = */ [&](nlohmann::ordered_json & schema) {
|
||||||
|
converter.resolve_refs(schema, "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cb(builder);
|
||||||
converter.check_errors();
|
converter.check_errors();
|
||||||
return converter.format_grammar();
|
return converter.format_grammar();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,4 +5,12 @@
|
||||||
#define JSON_ASSERT GGML_ASSERT
|
#define JSON_ASSERT GGML_ASSERT
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
|
|
||||||
std::string json_schema_to_grammar(const nlohmann::ordered_json& schema);
|
std::string json_schema_to_grammar(const nlohmann::ordered_json & schema);
|
||||||
|
|
||||||
|
struct llama_grammar_builder {
|
||||||
|
std::function<std::string(const std::string &, const std::string &)> add_rule;
|
||||||
|
std::function<std::string(const std::string &, const nlohmann::ordered_json &)> add_schema;
|
||||||
|
std::function<void(nlohmann::ordered_json &)> resolve_refs;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string build_grammar(const std::function<void(const llama_grammar_builder &)> & cb);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue