json-schema-to-grammar.h 782 B

12345678910111213141516171819202122
  1. #pragma once
  2. #include "ggml.h"
  3. // Change JSON_ASSERT from assert() to GGML_ASSERT:
  4. #define JSON_ASSERT GGML_ASSERT
  5. #include "json.hpp"
  6. std::string json_schema_to_grammar(const nlohmann::ordered_json & schema,
  7. bool force_gbnf = false);
  8. struct common_grammar_builder {
  9. std::function<std::string(const std::string &, const std::string &)> add_rule;
  10. std::function<std::string(const std::string &, const nlohmann::ordered_json &)> add_schema;
  11. std::function<void(nlohmann::ordered_json &)> resolve_refs;
  12. };
  13. struct common_grammar_options {
  14. bool dotall = false;
  15. bool compact_spaces = false;
  16. };
  17. std::string build_grammar(const std::function<void(const common_grammar_builder &)> & cb, const common_grammar_options & options = {});