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