Skip to content

grammar from string #2

@adrid

Description

@adrid

Regarding this comment:

grammar is not passed as string to be parsed, instead parsed llama_grammar* params, as at time of writing grammar_parser has no C api

Is there is any plan to support this? What do you think about creating simple cpp binding for this like that?

extern "C" llama_grammar * parseGrammar(const char * str) {
    grammar_parser::parse_state parsed_state = grammar_parser::parse(str);
    std::vector<const llama_grammar_element *> grammar_rules(parsed_state.c_rules());

    return llama_grammar_init(grammar_rules.data(), grammar_rules.size(), parsed_state.symbol_ids.at("root"));
}

I've tested this and after accept the token with grammar option it seems to work. I would prepare a draft if you think binding would be fine and we could check this in detail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions