mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2025-06-28 12:25:03 +00:00
grammar : handle maxItems == 0 in JSON schema (#13117)
Co-authored-by: Richard Lyons <frob@cloudstaff.com>
This commit is contained in:
@ -16,6 +16,9 @@ using json = nlohmann::ordered_json;
|
|||||||
static std::string build_repetition(const std::string & item_rule, int min_items, int max_items, const std::string & separator_rule = "") {
|
static std::string build_repetition(const std::string & item_rule, int min_items, int max_items, const std::string & separator_rule = "") {
|
||||||
auto has_max = max_items != std::numeric_limits<int>::max();
|
auto has_max = max_items != std::numeric_limits<int>::max();
|
||||||
|
|
||||||
|
if (max_items == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
if (min_items == 0 && max_items == 1) {
|
if (min_items == 0 && max_items == 1) {
|
||||||
return item_rule + "?";
|
return item_rule + "?";
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,9 @@ from typing import Any, List, Optional, Set, Tuple, Union
|
|||||||
|
|
||||||
def _build_repetition(item_rule, min_items, max_items, separator_rule=None):
|
def _build_repetition(item_rule, min_items, max_items, separator_rule=None):
|
||||||
|
|
||||||
|
if max_items == 0:
|
||||||
|
return ""
|
||||||
|
|
||||||
if min_items == 0 and max_items == 1:
|
if min_items == 0 and max_items == 1:
|
||||||
return f'{item_rule}?'
|
return f'{item_rule}?'
|
||||||
|
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
const SPACE_RULE = '| " " | "\\n"{1,2} [ \\t]{0,20}';
|
const SPACE_RULE = '| " " | "\\n"{1,2} [ \\t]{0,20}';
|
||||||
|
|
||||||
function _buildRepetition(itemRule, minItems, maxItems, opts={}) {
|
function _buildRepetition(itemRule, minItems, maxItems, opts={}) {
|
||||||
|
if (maxItems == 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
if (minItems === 0 && maxItems === 1) {
|
if (minItems === 0 && maxItems === 1) {
|
||||||
return `${itemRule}?`;
|
return `${itemRule}?`;
|
||||||
}
|
}
|
||||||
|
@ -597,6 +597,22 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
|
|||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
SUCCESS,
|
||||||
|
"maxItems 0",
|
||||||
|
R"""({
|
||||||
|
"items": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"maxItems": 0
|
||||||
|
})""",
|
||||||
|
R"""(
|
||||||
|
boolean ::= ("true" | "false") space
|
||||||
|
root ::= "[" space "]" space
|
||||||
|
space ::= | " " | "\n"{1,2} [ \t]{0,20}
|
||||||
|
)"""
|
||||||
|
});
|
||||||
|
|
||||||
test({
|
test({
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
"maxItems 1",
|
"maxItems 1",
|
||||||
|
Reference in New Issue
Block a user