epfl-dlab / transformers-CFG

🤗 A specialized library for integrating context-free grammars (CFG) in EBNF with the Hugging Face Transformers
http://saibo-creator.xyz:7860/
MIT License
89 stars 16 forks source link

Feature Request: Conversion of JSON Schema to a grammar in ebnf #2

Closed Saibo-creator closed 5 months ago

Saibo-creator commented 9 months ago

Description

The script examples/json-schema-to-grammar.py in llama-cpp provides a way to convert a JSON schema to a grammar. This can be seen in the usage example with the student.json schema:

% cat ../schemas/student.json 
{
   "type": "object",
   "properties": {
       "name": {"type": "string"},
       "age": {"type": "number"},
       "is_student": {"type": "boolean"},
       "courses": {
           "type": "array",
           "items": {"type": "string"}
       }
   }
}
% ./main -m $L13B -p 'Hermione Granger ' --grammar "$(python3 examples/json-schema-to-grammar.py ../schemas/student.json --prop-order 'is_student,name,age,courses')"
...
{"is_student":true, "name":"Hermione","age":12,"courses":[ "Arithmancy", "Defense Against the Dark Arts", "Divination", "Muggle Studies", "Herbology", "Potions" ]}

This allows users to directly generate JSON data that adheres to a predefined schema.

This feature would be largely reduce users's workload of writing a grammar themselves!

Here is a very nice online tool which does the same conversion. It was shared in the llama-cpp discussion

Saibo-creator commented 5 months ago

45