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