gbnf-validator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "unicode.h"
  2. #include "llama-grammar.h"
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <fstream>
  7. #include <string>
  8. #include <vector>
  9. static bool llama_grammar_validate(struct llama_grammar * grammar, const std::string & input_str, size_t & error_pos, std::string & error_msg) {
  10. const auto cpts = unicode_cpts_from_utf8(input_str);
  11. const llama_grammar_rules & rules = llama_grammar_get_rules (grammar);
  12. llama_grammar_stacks & stacks_cur = llama_grammar_get_stacks(grammar);
  13. size_t pos = 0;
  14. for (const auto & cpt : cpts) {
  15. const llama_grammar_stacks stacks_prev = llama_grammar_get_stacks(grammar); // copy
  16. llama_grammar_accept(rules, stacks_prev, cpt, stacks_cur);
  17. if (stacks_cur.empty()) {
  18. error_pos = pos;
  19. error_msg = "Unexpected character '" + unicode_cpt_to_utf8(cpt) + "'";
  20. stacks_cur = stacks_prev;
  21. return false;
  22. }
  23. ++pos;
  24. }
  25. for (const auto & stack : stacks_cur) {
  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. std::string grammar_str;
  62. {
  63. std::ifstream grammar_file(grammar_filename);
  64. GGML_ASSERT(grammar_file.is_open() && "Failed to open grammar file");
  65. std::stringstream buffer;
  66. buffer << grammar_file.rdbuf();
  67. grammar_str = buffer.str();
  68. }
  69. llama_grammar * grammar = llama_grammar_init_impl(nullptr, grammar_str.c_str(), "root");
  70. if (grammar == nullptr) {
  71. throw std::runtime_error("Failed to initialize llama_grammar");
  72. }
  73. // Read the input file
  74. std::string input_str;
  75. {
  76. std::ifstream input_file(input_filename);
  77. GGML_ASSERT(input_file.is_open() && "Failed to open input file");
  78. std::stringstream buffer;
  79. buffer << input_file.rdbuf();
  80. input_str = buffer.str();
  81. }
  82. // Validate the input string against the grammar
  83. size_t error_pos;
  84. std::string error_msg;
  85. bool is_valid = llama_grammar_validate(grammar, input_str, error_pos, error_msg);
  86. if (is_valid) {
  87. fprintf(stdout, "Input string is valid according to the grammar.\n");
  88. } else {
  89. print_error_message(input_str, error_pos, error_msg);
  90. }
  91. // Clean up
  92. llama_grammar_free_impl(grammar);
  93. return 0;
  94. }