]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commit
grammar : fix JSON Schema for string regex with top-level alt. (#9903)
authorJoe Eli McIlvain <redacted>
Wed, 16 Oct 2024 16:03:24 +0000 (09:03 -0700)
committerGitHub <redacted>
Wed, 16 Oct 2024 16:03:24 +0000 (19:03 +0300)
commit66c2c93082289325199ae1f773f3b0ab2e399a47
treeae1b930676346489bda767f3c4e113d23b8de9ad
parent10433e8b457c4cfd759cbb41fc55fc398db4a5da
grammar : fix JSON Schema for string regex with top-level alt. (#9903)

Prior to this commit, using a JSON Schema containing a string
with `pattern` regular expression that uses top-level alternation
(e.g. `"pattern": "^A|B|C|D$"`) would result in invalid JSON
output from the constrained sampling grammar, because it
ended up creating a grammar rule like this for the string:

```
thing ::= "\"" "A" | "B" | "C" | "D" "\"" space
```

Note that this rule will only match a starting quote for the "A" case,
and will only match an ending quote for the "D" case,
so this rule will always produce invalid JSON when used for sampling
(that is, the JSON will always be lacking the starting quote,
the ending quote, or both).

This was fixed in a simple way by adding parentheses to the
generated rule (for all string pattern rules, to keep it simple),
such that the new generated rule looks like this (correct):

```
thing ::= "\"" ("A" | "B" | "C" | "D") "\"" space
```
common/json-schema-to-grammar.cpp
examples/json_schema_to_grammar.py
examples/server/public/json-schema-to-grammar.mjs
tests/test-json-schema-to-grammar.cpp