Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
b53d7998
Unverified
Commit
b53d7998
authored
Feb 18, 2025
by
Michael Goin
Committed by
GitHub
Feb 18, 2025
Browse files
Add outlines fallback when JSON schema has enum (#13449)
Signed-off-by:
mgoin
<
mgoin64@gmail.com
>
parent
9915912f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
0 deletions
+86
-0
tests/entrypoints/conftest.py
tests/entrypoints/conftest.py
+41
-0
tests/entrypoints/llm/test_guided_generate.py
tests/entrypoints/llm/test_guided_generate.py
+41
-0
vllm/model_executor/guided_decoding/utils.py
vllm/model_executor/guided_decoding/utils.py
+4
-0
No files found.
tests/entrypoints/conftest.py
View file @
b53d7998
...
...
@@ -141,6 +141,47 @@ def sample_definition_json_schema():
}
@
pytest
.
fixture
def
sample_enum_json_schema
():
return
{
"type"
:
"object"
,
"properties"
:
{
"status"
:
{
"type"
:
"string"
,
"enum"
:
[
"active"
,
"inactive"
,
"pending"
]
# Literal values using enum
},
"priority"
:
{
"type"
:
"string"
,
"enum"
:
[
"low"
,
"medium"
,
"high"
,
"critical"
]
},
"category"
:
{
"type"
:
"object"
,
"properties"
:
{
"type"
:
{
"type"
:
"string"
,
"enum"
:
[
"bug"
,
"feature"
,
"improvement"
]
},
"severity"
:
{
"type"
:
"integer"
,
"enum"
:
[
1
,
2
,
3
,
4
,
5
]
# Enum can also contain numbers
}
},
"required"
:
[
"type"
,
"severity"
]
},
"flags"
:
{
"type"
:
"array"
,
"items"
:
{
"type"
:
"string"
,
"enum"
:
[
"urgent"
,
"blocked"
,
"needs_review"
,
"approved"
]
}
}
},
"required"
:
[
"status"
,
"priority"
,
"category"
,
"flags"
]
}
@
pytest
.
fixture
def
sample_guided_choice
():
return
[
...
...
tests/entrypoints/llm/test_guided_generate.py
View file @
b53d7998
...
...
@@ -146,6 +146,47 @@ def test_guided_definition_json_completion(sample_definition_json_schema, llm,
schema
=
sample_definition_json_schema
)
@
pytest
.
mark
.
skip_global_cleanup
@
pytest
.
mark
.
parametrize
(
"guided_decoding_backend"
,
GUIDED_DECODING_BACKENDS
)
def
test_guided_enum_json_completion
(
sample_enum_json_schema
,
llm
,
guided_decoding_backend
:
str
):
sampling_params
=
SamplingParams
(
temperature
=
1.0
,
max_tokens
=
1000
,
guided_decoding
=
GuidedDecodingParams
(
json
=
sample_enum_json_schema
,
backend
=
guided_decoding_backend
))
outputs
=
llm
.
generate
(
prompts
=
[
"Create a bug report JSON that fits this schema: "
f
"
{
sample_enum_json_schema
}
. Make it for a high priority critical bug."
]
*
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_enum_json_schema
)
# Additional assertions to verify enum values
assert
output_json
[
"status"
]
in
[
"active"
,
"inactive"
,
"pending"
]
assert
output_json
[
"priority"
]
in
[
"low"
,
"medium"
,
"high"
,
"critical"
]
assert
output_json
[
"category"
][
"type"
]
in
[
"bug"
,
"feature"
,
"improvement"
]
assert
output_json
[
"category"
][
"severity"
]
in
[
1
,
2
,
3
,
4
,
5
]
for
flag
in
output_json
[
"flags"
]:
assert
flag
in
[
"urgent"
,
"blocked"
,
"needs_review"
,
"approved"
]
@
pytest
.
mark
.
skip_global_cleanup
@
pytest
.
mark
.
parametrize
(
"guided_decoding_backend"
,
GUIDED_DECODING_BACKENDS
)
def
test_guided_choice_completion
(
sample_guided_choice
,
llm
,
...
...
vllm/model_executor/guided_decoding/utils.py
View file @
b53d7998
...
...
@@ -14,6 +14,10 @@ def has_xgrammar_unsupported_json_features(schema: dict) -> bool:
if
"pattern"
in
obj
:
return
True
# Check for enum restrictions
if
"enum"
in
obj
:
return
True
# Check for numeric ranges
if
obj
.
get
(
"type"
)
in
(
"integer"
,
"number"
)
and
any
(
key
in
obj
for
key
in
[
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment