| 1 | #include "sampling.h" |
| 2 | |
| 3 | struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params) { |
| 4 | struct llama_sampling_context * result = new llama_sampling_context(); |
| 5 | |
| 6 | result->params = params; |
| 7 | result->grammar = nullptr; |
| 8 | |
| 9 | // if there is a grammar, parse it |
| 10 | if (!params.grammar.empty()) { |
| 11 | result->parsed_grammar = grammar_parser::parse(params.grammar.c_str()); |
| 12 | |
| 13 | // will be empty (default) if there are parse errors |
| 14 | if (result->parsed_grammar.rules.empty()) { |
| 15 | fprintf(stderr, "%s: failed to parse grammar\n", __func__); |
| 16 | return nullptr; |
| 17 | } |
| 18 | |
| 19 | std::vector<const llama_grammar_element *> grammar_rules(result->parsed_grammar.c_rules()); |
| 20 | |
| 21 | result->grammar = llama_grammar_init( |
| 22 | grammar_rules.data(), |
| 23 | grammar_rules.size(), result->parsed_grammar.symbol_ids.at("root")); |
| 24 | } |
| 25 | |
| 26 | result->prev.resize(params.n_prev); |
| 27 | |
| 28 | return result; |
| 29 | } |
| 30 | |
| 31 | void llama_sampling_free(struct llama_sampling_context * ctx) { |
| 32 | if (ctx->grammar != NULL) { |