mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-09-17 20:31:47 +02:00
refactor : rename common_schema_parse to common_schema_from_json
This commit is contained in:
@@ -998,7 +998,7 @@ std::string json_schema_to_grammar(const common_json & schema, bool force_gbnf)
|
||||
(void)force_gbnf;
|
||||
#endif // LLAMA_USE_LLGUIDANCE
|
||||
try {
|
||||
return json_schema_to_grammar(common_schema_parse(schema));
|
||||
return json_schema_to_grammar(common_schema_from_json(schema));
|
||||
} catch (const std::runtime_error & e) {
|
||||
throw std::invalid_argument(std::string("JSON schema conversion failed:\n") + e.what());
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
class common_schema_parser {
|
||||
class common_schema_builder {
|
||||
const common_json & root_;
|
||||
common_schema_document & doc_;
|
||||
|
||||
// the targets parsed here, moved into doc_ once the whole schema parsed
|
||||
// the targets built here, moved into doc_ once the whole schema is built
|
||||
std::map<std::string, common_schema_ptr> refs_;
|
||||
|
||||
// ref nodes get their target once every $ref is parsed, a cycle would otherwise need it too early
|
||||
// ref nodes get their target once every $ref is built, a cycle would otherwise need it too early
|
||||
std::vector<common_schema_ref *> pending_;
|
||||
|
||||
[[noreturn]] static void fail(const std::string & path, const std::string & msg) {
|
||||
@@ -323,9 +323,9 @@ class common_schema_parser {
|
||||
}
|
||||
|
||||
public:
|
||||
common_schema_parser(const common_json & root, common_schema_document & doc) : root_(root), doc_(doc) {}
|
||||
common_schema_builder(const common_json & root, common_schema_document & doc) : root_(root), doc_(doc) {}
|
||||
|
||||
common_schema_ptr parse() {
|
||||
common_schema_ptr build() {
|
||||
auto node = parse_schema(root_, "#");
|
||||
for (auto & entry : refs_) {
|
||||
doc_.refs[entry.first] = std::move(entry.second);
|
||||
@@ -337,14 +337,14 @@ class common_schema_parser {
|
||||
}
|
||||
};
|
||||
|
||||
common_schema_document common_schema_parse(const common_json & schema) {
|
||||
common_schema_document common_schema_from_json(const common_json & schema) {
|
||||
common_schema_document doc;
|
||||
doc.root = common_schema_parser(schema, doc).parse();
|
||||
doc.root = common_schema_builder(schema, doc).build();
|
||||
return doc;
|
||||
}
|
||||
|
||||
common_schema_ptr common_schema_parse(const common_json & schema, common_schema_document & doc) {
|
||||
return common_schema_parser(schema, doc).parse();
|
||||
common_schema_ptr common_schema_from_json(const common_json & schema, common_schema_document & doc) {
|
||||
return common_schema_builder(schema, doc).build();
|
||||
}
|
||||
|
||||
static common_schema::value_type json_type(const common_json & value) {
|
||||
|
||||
@@ -191,13 +191,13 @@ struct common_schema_document {
|
||||
std::map<std::string, common_schema_ptr> refs;
|
||||
};
|
||||
|
||||
// A document shared by the parsers built from its nodes, which it keeps alive
|
||||
// A document shared by the PEG parsers built from its nodes, which it keeps alive
|
||||
using common_schema_document_ptr = std::shared_ptr<const common_schema_document>;
|
||||
|
||||
// Throws std::runtime_error when the schema falls outside the supported subset.
|
||||
common_schema_document common_schema_parse(const common_json & schema);
|
||||
common_schema_document common_schema_from_json(const common_json & schema);
|
||||
|
||||
// Parses a schema that belongs to a document parsed earlier, e.g. one property of it.
|
||||
// Builds a schema that belongs to a document built earlier, e.g. one property of it.
|
||||
// A $ref it cannot resolve on its own is looked up in doc.refs, the targets it resolves itself are added there.
|
||||
// doc is unchanged when the schema is rejected.
|
||||
common_schema_ptr common_schema_parse(const common_json & schema, common_schema_document & doc);
|
||||
common_schema_ptr common_schema_from_json(const common_json & schema, common_schema_document & doc);
|
||||
|
||||
@@ -89,7 +89,7 @@ common_chat_params common_chat_params_init_minimax_m3(const common_chat_template
|
||||
const auto & function = tool.at("function");
|
||||
std::string name = function.at("name");
|
||||
auto params = function.contains("parameters") ? function.at("parameters") : json::object();
|
||||
auto doc = std::make_shared<const common_schema_document>(common_schema_parse(params));
|
||||
auto doc = std::make_shared<const common_schema_document>(common_schema_from_json(params));
|
||||
|
||||
// The template expands argument values recursively in XML (see the to_xml() macro)
|
||||
std::function<common_peg_parser(const common_schema &, const std::string &, const std::string &)> value_of;
|
||||
|
||||
@@ -14,7 +14,7 @@ void foreach_function(const json & tools, const std::function<void(const json &)
|
||||
|
||||
void foreach_parameter(const json & function, const std::function<void(const common_schema_property &, const common_schema_document_ptr &)> & fn) {
|
||||
auto params = function.contains("parameters") ? function.at("parameters") : json::object();
|
||||
auto doc = std::make_shared<const common_schema_document>(common_schema_parse(params));
|
||||
auto doc = std::make_shared<const common_schema_document>(common_schema_from_json(params));
|
||||
const auto * object = dynamic_cast<const common_schema_object *>(doc->root.get());
|
||||
if (!object) {
|
||||
return;
|
||||
|
||||
@@ -1124,7 +1124,7 @@ common_peg_parser common_peg_parser_builder::schema(const common_peg_parser & p,
|
||||
}
|
||||
|
||||
common_peg_parser common_peg_parser_builder::schema(const common_peg_parser & p, const std::string & name, const common_json & schema, bool raw) {
|
||||
auto doc = std::make_shared<const common_schema_document>(common_schema_parse(schema));
|
||||
auto doc = std::make_shared<const common_schema_document>(common_schema_from_json(schema));
|
||||
return this->schema(p, name, doc, *doc->root, raw);
|
||||
}
|
||||
|
||||
|
||||
@@ -1534,7 +1534,7 @@ int main() {
|
||||
}
|
||||
}
|
||||
})""");
|
||||
assert(json_schema_to_grammar(common_schema_parse(schema)) == json_schema_to_grammar(schema, true));
|
||||
assert(json_schema_to_grammar(common_schema_from_json(schema)) == json_schema_to_grammar(schema, true));
|
||||
}
|
||||
|
||||
// a property node carries its $ref target, so its grammar names the ref rule
|
||||
@@ -1565,7 +1565,7 @@ int main() {
|
||||
string ::= "\"" char* "\""
|
||||
)""",
|
||||
};
|
||||
auto doc = common_schema_parse(parameters);
|
||||
auto doc = common_schema_from_json(parameters);
|
||||
tc.verify(build_grammar([&](const common_grammar_builder & builder) {
|
||||
const auto & item = static_cast<const common_schema_object &>(*doc.root).properties.at(0);
|
||||
builder.add_schema("root", *item.schema);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <utility>
|
||||
|
||||
static common_schema_document parse(const std::string & schema) {
|
||||
return common_schema_parse(common_json::parse(schema));
|
||||
return common_schema_from_json(common_json::parse(schema));
|
||||
}
|
||||
|
||||
// the node as T, aborting the current test when it is some other kind
|
||||
@@ -370,13 +370,13 @@ static void test_ref(testing & t) {
|
||||
|
||||
t.test("a schema parsed into a document shares its refs", [](testing & t) {
|
||||
auto doc = parse(R"({"properties": {"a": {"$ref": "#/$defs/t"}}, "$defs": {"t": {"type": "boolean"}}})");
|
||||
auto node = common_schema_parse(common_json::parse(R"({"items": {"$ref": "#/$defs/t"}})"), doc);
|
||||
auto node = common_schema_from_json(common_json::parse(R"({"items": {"$ref": "#/$defs/t"}})"), doc);
|
||||
const auto & a = as<common_schema_array>(t, node.get(), "node");
|
||||
const auto & r = as<common_schema_ref>(t, a.items.get(), "items");
|
||||
t.assert_true("shared target", r.target == doc.refs.at("#/$defs/t").get());
|
||||
t.assert_equal("refs", (size_t) 1, doc.refs.size());
|
||||
|
||||
auto added = common_schema_parse(common_json::parse(R"({"$ref": "#/$defs/u", "$defs": {"u": {"type": "null"}}})"), doc);
|
||||
auto added = common_schema_from_json(common_json::parse(R"({"$ref": "#/$defs/u", "$defs": {"u": {"type": "null"}}})"), doc);
|
||||
as<common_schema_null>(t, as<common_schema_ref>(t, added.get(), "added").target, "target");
|
||||
t.assert_equal("refs", (size_t) 2, doc.refs.size());
|
||||
});
|
||||
@@ -384,7 +384,7 @@ static void test_ref(testing & t) {
|
||||
t.test("a rejected schema leaves the document unchanged", [](testing & t) {
|
||||
common_schema_document doc;
|
||||
try {
|
||||
common_schema_parse(common_json::parse(R"({"allOf": [{"$ref": "#/$defs/t"}, {"type": "x"}], "$defs": {"t": {"type": "null"}}})"), doc);
|
||||
common_schema_from_json(common_json::parse(R"({"allOf": [{"$ref": "#/$defs/t"}, {"type": "x"}], "$defs": {"t": {"type": "null"}}})"), doc);
|
||||
t.assert_true("rejected", false);
|
||||
} catch (const std::runtime_error &) {
|
||||
t.assert_true("no refs", doc.refs.empty());
|
||||
|
||||
Reference in New Issue
Block a user