test-gbnf-validator.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "../src/unicode.h"
  2. #include "../src/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. auto & stacks_cur = llama_grammar_get_stacks(grammar);
  12. size_t pos = 0;
  13. for (const auto & cpt : cpts) {
  14. llama_grammar_accept(grammar, cpt);
  15. if (stacks_cur.empty()) {
  16. error_pos = pos;
  17. error_msg = "Unexpected character '" + unicode_cpt_to_utf8(cpt) + "'";
  18. return false;
  19. }
  20. ++pos;
  21. }
  22. for (const auto & stack : stacks_cur) {
  23. if (stack.empty()) {
  24. return true;
  25. }
  26. }
  27. error_pos = pos;
  28. error_msg = "Unexpected end of input";
  29. return false;
  30. }
  31. static void print_error_message(const std::string & input_str, size_t error_pos, const std::string & error_msg) {
  32. fprintf(stdout, "Input string is invalid according to the grammar.\n");
  33. fprintf(stdout, "Error: %s at position %zu\n", error_msg.c_str(), error_pos);
  34. fprintf(stdout, "\n");
  35. fprintf(stdout, "Input string:\n");
  36. fprintf(stdout, "%s", input_str.substr(0, error_pos).c_str());
  37. if (error_pos < input_str.size()) {
  38. fprintf(stdout, "\033[1;31m%c", input_str[error_pos]);
  39. if (error_pos+1 < input_str.size()) {
  40. fprintf(stdout, "\033[0;31m%s", input_str.substr(error_pos+1).c_str());
  41. }
  42. fprintf(stdout, "\033[0m\n");
  43. }
  44. }
  45. int main(int argc, char** argv) {
  46. if (argc != 3) {
  47. fprintf(stdout, "Usage: %s <grammar_filename> <input_filename>\n", argv[0]);
  48. return 1;
  49. }
  50. const std::string grammar_filename = argv[1];
  51. const std::string input_filename = argv[2];
  52. // Read the GBNF grammar file
  53. FILE* grammar_file = fopen(grammar_filename.c_str(), "r");
  54. if (!grammar_file) {
  55. fprintf(stdout, "Failed to open grammar file: %s\n", grammar_filename.c_str());
  56. return 1;
  57. }
  58. std::string grammar_str;
  59. {
  60. std::ifstream grammar_file(grammar_filename);
  61. GGML_ASSERT(grammar_file.is_open() && "Failed to open grammar file");
  62. std::stringstream buffer;
  63. buffer << grammar_file.rdbuf();
  64. grammar_str = buffer.str();
  65. }
  66. llama_grammar * grammar = llama_grammar_init_impl(nullptr, grammar_str.c_str(), "root", false, nullptr, 0, nullptr, 0);
  67. if (grammar == nullptr) {
  68. fprintf(stdout, "Failed to initialize llama_grammar\n");
  69. return 1;
  70. }
  71. // Read the input file
  72. std::string input_str;
  73. {
  74. std::ifstream input_file(input_filename);
  75. GGML_ASSERT(input_file.is_open() && "Failed to open input file");
  76. std::stringstream buffer;
  77. buffer << input_file.rdbuf();
  78. input_str = buffer.str();
  79. }
  80. // Validate the input string against the grammar
  81. size_t error_pos;
  82. std::string error_msg;
  83. bool is_valid = llama_grammar_validate(grammar, input_str, error_pos, error_msg);
  84. if (is_valid) {
  85. fprintf(stdout, "Input string is valid according to the grammar.\n");
  86. } else {
  87. print_error_message(input_str, error_pos, error_msg);
  88. }
  89. // Clean up
  90. llama_grammar_free_impl(grammar);
  91. return 0;
  92. }