[Bugfix] remove fallback in guided_json (int range, patterns) (#16725)
Signed-off-by: csy1204 <josang1204@gmail.com> Co-authored-by: 조상연[플레이스 AI] <sang-yeon.cho@navercorp.com>
This commit is contained in:
@@ -305,7 +305,7 @@ def test_disable_guided_decoding_fallback(sample_regex, llm):
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="xgrammar does not support advanced JSON schema features "
|
||||
"like enums, patterns or numeric ranges."):
|
||||
"like string length, item limits, or property bounds."):
|
||||
llm.generate(prompts="This should fail",
|
||||
sampling_params=sampling_params,
|
||||
use_tqdm=True)
|
||||
@@ -386,6 +386,62 @@ def test_guided_json_completion_with_enum(llm, guided_decoding_backend: str):
|
||||
jsonschema.validate(instance=output_json, schema=json_schema)
|
||||
|
||||
|
||||
@pytest.mark.skip_global_cleanup
|
||||
@pytest.mark.parametrize("guided_decoding_backend", GUIDED_DECODING_BACKENDS)
|
||||
def test_guided_number_range_json_completion(llm,
|
||||
guided_decoding_backend: str):
|
||||
sample_output_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"age": {
|
||||
"type": "integer",
|
||||
"minimum": 18,
|
||||
"maximum": 99
|
||||
},
|
||||
"score": {
|
||||
"type": "number",
|
||||
"minimum": 0.0,
|
||||
"maximum": 100.0
|
||||
},
|
||||
"zipcode": {
|
||||
"type": "string",
|
||||
"pattern": r"^\d{5}(-\d{4})?$"
|
||||
},
|
||||
},
|
||||
"required": ["age", "score", "zipcode"],
|
||||
}
|
||||
sampling_params = SamplingParams(
|
||||
temperature=1.0,
|
||||
max_tokens=1000,
|
||||
guided_decoding=GuidedDecodingParams(json=sample_output_schema,
|
||||
backend=guided_decoding_backend),
|
||||
)
|
||||
outputs = llm.generate(
|
||||
prompts=[
|
||||
"Create a JSON object for a user with age, score, and zipcode."
|
||||
] * 2,
|
||||
sampling_params=sampling_params,
|
||||
use_tqdm=True,
|
||||
)
|
||||
|
||||
assert outputs is not None
|
||||
|
||||
for output in outputs:
|
||||
assert output is not None
|
||||
assert isinstance(output, RequestOutput)
|
||||
prompt = output.prompt
|
||||
|
||||
generated_text = output.outputs[0].text
|
||||
assert generated_text is not None
|
||||
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
|
||||
output_json = json.loads(generated_text)
|
||||
jsonschema.validate(instance=output_json, schema=sample_output_schema)
|
||||
assert 18 <= output_json["age"] <= 99
|
||||
assert 0.0 <= output_json["score"] <= 100.0
|
||||
assert (re.fullmatch(r"^\d{5}(-\d{4})?$", output_json["zipcode"])
|
||||
is not None)
|
||||
|
||||
|
||||
@pytest.mark.skip_global_cleanup
|
||||
def test_guidance_no_additional_properties(llm):
|
||||
schema = {
|
||||
|
||||
Reference in New Issue
Block a user