2023-11-21 05:23:18 +00:00
|
|
|
import llama_cpp
|
2024-01-15 15:45:57 +00:00
|
|
|
import json
|
2023-11-21 05:23:18 +00:00
|
|
|
|
|
|
|
tree = """
|
|
|
|
leaf ::= "."
|
|
|
|
node ::= leaf | "(" node node ")"
|
|
|
|
root ::= node
|
|
|
|
"""
|
|
|
|
|
2024-01-15 15:45:57 +00:00
|
|
|
|
2023-11-21 05:23:18 +00:00
|
|
|
def test_grammar_from_string():
|
|
|
|
grammar = llama_cpp.LlamaGrammar.from_string(tree)
|
|
|
|
assert grammar._n_rules == 3
|
|
|
|
assert grammar._start_rule_index == 2
|
|
|
|
assert grammar.grammar is not None
|
2024-01-15 15:45:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_composed_pydantic_grammar():
|
|
|
|
"""
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
class A(BaseModel):
|
|
|
|
a: int
|
|
|
|
|
|
|
|
class B(BaseModel):
|
|
|
|
a: A
|
|
|
|
b: int
|
|
|
|
"""
|
|
|
|
|
|
|
|
# This schema corresponds to the grammar in the comment above.
|
|
|
|
# We don't use the pydantic models directly to avoid the dependency.
|
|
|
|
schema = {
|
|
|
|
"$defs": {
|
|
|
|
"A": {
|
|
|
|
"properties": {"a": {"title": "A", "type": "integer"}},
|
|
|
|
"required": ["a"],
|
|
|
|
"title": "A",
|
|
|
|
"type": "object",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"properties": {
|
|
|
|
"a": {"$ref": "#/$defs/A"},
|
|
|
|
"b": {"title": "B", "type": "integer"},
|
|
|
|
},
|
|
|
|
"required": ["a", "b"],
|
|
|
|
"title": "B",
|
|
|
|
"type": "object",
|
|
|
|
}
|
|
|
|
|
|
|
|
grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(schema))
|
|
|
|
|
|
|
|
assert grammar.grammar is not None
|