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
4bf82d4b
Unverified
Commit
4bf82d4b
authored
Mar 11, 2025
by
Russell Bryant
Committed by
GitHub
Mar 11, 2025
Browse files
[V1] Add regex structured output support with xgrammar (#14590)
Signed-off-by:
Russell Bryant
<
rbryant@redhat.com
>
parent
9ab32671
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
19 deletions
+25
-19
requirements/common.txt
requirements/common.txt
+2
-2
tests/v1/entrypoints/llm/test_struct_output_generate.py
tests/v1/entrypoints/llm/test_struct_output_generate.py
+16
-16
vllm/v1/structured_output/__init__.py
vllm/v1/structured_output/__init__.py
+2
-0
vllm/v1/structured_output/utils.py
vllm/v1/structured_output/utils.py
+5
-1
No files found.
requirements/common.txt
View file @
4bf82d4b
...
...
@@ -19,7 +19,7 @@ tiktoken >= 0.6.0 # Required for DBRX tokenizer
lm-format-enforcer >= 0.10.11, < 0.11
outlines == 0.1.11
lark == 1.2.2
xgrammar == 0.1.1
1
; platform_machine == "x86_64"
xgrammar == 0.1.1
5
; platform_machine == "x86_64"
or platform_machine == "aarch64"
typing_extensions >= 4.10
filelock >= 3.16.1 # need to contain https://github.com/tox-dev/filelock/pull/317
partial-json-parser # used for parsing partial JSON outputs
...
...
tests/v1/entrypoints/llm/test_struct_output_generate.py
View file @
4bf82d4b
# SPDX-License-Identifier: Apache-2.0
import
json
import
re
import
jsonschema
import
pytest
...
...
@@ -219,25 +220,24 @@ def test_guided_regex(monkeypatch, sample_regex, guided_decoding_backend: str):
guided_decoding
=
GuidedDecodingParams
(
regex
=
sample_regex
,
backend
=
guided_decoding_backend
))
with
pytest
.
raises
(
ValueError
,
match
=
"Regex guided decoding is not supported."
):
llm
.
generate
(
prompts
=
[
outputs
=
llm
.
generate
(
prompts
=
[
f
"Give an example IPv4 address with this regex:
{
sample_regex
}
"
]
*
2
,
sampling_params
=
sampling_params
,
use_tqdm
=
True
)
use_tqdm
=
True
,
)
# Once regex is supported --
#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
# print(generated_text)
# assert generated_text is not None
# assert re.fullmatch(sample_regex, generated_text) is not None
# print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
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
print
(
generated_text
)
assert
generated_text
is
not
None
assert
re
.
fullmatch
(
sample_regex
,
generated_text
)
is
not
None
print
(
f
"Prompt:
{
prompt
!
r
}
, Generated text:
{
generated_text
!
r
}
"
)
@
pytest
.
mark
.
skip_global_cleanup
...
...
vllm/v1/structured_output/__init__.py
View file @
4bf82d4b
...
...
@@ -112,6 +112,8 @@ class StructuredOutputManager:
ctx
=
self
.
compiler
.
compile_builtin_json_grammar
()
elif
request_type
==
StructuredOutputOptions
.
GRAMMAR
:
ctx
=
self
.
compiler
.
compile_grammar
(
grammar_spec
)
elif
request_type
==
StructuredOutputOptions
.
REGEX
:
ctx
=
self
.
compiler
.
compile_regex
(
grammar_spec
)
else
:
logger
.
error
(
"Validation should have already occurred. "
"Please file an issue."
)
...
...
vllm/v1/structured_output/utils.py
View file @
4bf82d4b
...
...
@@ -251,7 +251,11 @@ def validate_structured_output_request(
gd_params
=
sampling_params
.
guided_decoding
if
gd_params
.
regex
:
raise
ValueError
(
"Regex structured output is not supported."
)
try
:
xgr
.
Grammar
.
from_regex
(
gd_params
.
regex
)
except
Exception
as
err
:
raise
ValueError
(
"Failed to transform regex into a grammar: "
f
"
{
err
}
"
)
from
err
if
gd_params
.
choice
:
choice_grammar
=
choice_as_grammar
(
gd_params
.
choice
)
...
...
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