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