gbnf-validator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #define LLAMA_API_INTERNAL
  2. #include "grammar-parser.h"
  3. #include "ggml.h"
  4. #include "llama.h"
  5. #include "unicode.h"
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <sstream>
  9. #include <fstream>
  10. #include <string>
  11. #include <vector>
  12. static bool llama_sample_grammar_string(struct llama_grammar * grammar, const std::string & input_str, size_t & error_pos, std::string & error_msg) {
  13. auto decoded = decode_utf8(input_str, {});
  14. const auto & code_points = decoded.first;
  15. size_t pos = 0;
  16. for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
  17. auto prev_stacks = grammar->stacks;
  18. llama_grammar_accept(grammar->rules, prev_stacks, *it, grammar->stacks);
  19. if (grammar->stacks.empty()) {
  20. error_pos = pos;
  21. error_msg = "Unexpected character '" + unicode_cpt_to_utf8(*it) + "'";
  22. grammar->stacks = prev_stacks;
  23. return false;
  24. }
  25. ++pos;
  26. }
  27. for (const auto & stack : grammar->stacks) {
  28. if (stack.empty()) {
  29. return true;
  30. }
  31. }
  32. error_pos = pos;
  33. error_msg = "Unexpected end of input";
  34. return false;
  35. }
  36. static void print_error_message(const std::string & input_str, size_t error_pos, const std::string & error_msg) {
  37. fprintf(stdout, "Input string is invalid according to the grammar.\n");
  38. fprintf(stdout, "Error: %s at position %zu\n", error_msg.c_str(), error_pos);
  39. fprintf(stdout, "\n");
  40. fprintf(stdout, "Input string:\n");
  41. fprintf(stdout, "%s", input_str.substr(0, error_pos).c_str());
  42. if (error_pos < input_str.size()) {
  43. fprintf(stdout, "\033[1;31m%c", input_str[error_pos]);
  44. if (error_pos+1 < input_str.size()) {
  45. fprintf(stdout, "\033[0;31m%s", input_str.substr(error_pos+1).c_str());
  46. }
  47. fprintf(stdout, "\033[0m\n");
  48. }
  49. }
  50. int main(int argc, char** argv) {
  51. if (argc != 3) {
  52. fprintf(stdout, "Usage: %s <grammar_filename> <input_filename>\n", argv[0]);
  53. return 1;
  54. }
  55. const std::string grammar_filename = argv[1];
  56. const std::string input_filename = argv[2];
  57. // Read the GBNF grammar file
  58. FILE* grammar_file = fopen(grammar_filename.c_str(), "r");
  59. if (!grammar_file) {
  60. fprintf(stdout, "Failed to open grammar file: %s\n", grammar_filename.c_str());
  61. return 1;
  62. }
  63. std::string grammar_str;
  64. {
  65. std::ifstream grammar_file(grammar_filename);
  66. GGML_ASSERT(grammar_file.is_open() && "Failed to open grammar file");
  67. std::stringstream buffer;
  68. buffer << grammar_file.rdbuf();
  69. grammar_str = buffer.str();
  70. }
  71. // Parse the GBNF grammar
  72. auto parsed_grammar = grammar_parser::parse(grammar_str.c_str());
  73. // will be empty (default) if there are parse errors
  74. if (parsed_grammar.rules.empty()) {
  75. fprintf(stdout, "%s: failed to parse grammar\n", __func__);
  76. return 1;
  77. }
  78. // Ensure that there is a "root" node.
  79. if (parsed_grammar.symbol_ids.find("root") == parsed_grammar.symbol_ids.end()) {
  80. fprintf(stdout, "%s: grammar does not contain a 'root' symbol\n", __func__);
  81. return 1;
  82. }
  83. std::vector<const llama_grammar_element *> grammar_rules(parsed_grammar.c_rules());
  84. // Create the LLAMA grammar
  85. auto grammar = llama_grammar_init(
  86. grammar_rules.data(),
  87. grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
  88. if (grammar == nullptr) {
  89. throw std::runtime_error("Failed to initialize llama_grammar");
  90. }
  91. // Read the input file
  92. std::string input_str;
  93. {
  94. std::ifstream input_file(input_filename);
  95. GGML_ASSERT(input_file.is_open() && "Failed to open input file");
  96. std::stringstream buffer;
  97. buffer << input_file.rdbuf();
  98. input_str = buffer.str();
  99. }
  100. // Validate the input string against the grammar
  101. size_t error_pos;
  102. std::string error_msg;
  103. bool is_valid = llama_sample_grammar_string(grammar, input_str, error_pos, error_msg);
  104. if (is_valid) {
  105. fprintf(stdout, "Input string is valid according to the grammar.\n");
  106. } else {
  107. print_error_message(input_str, error_pos, error_msg);
  108. }
  109. // Clean up
  110. llama_grammar_free(grammar);
  111. return 0;
  112. }