[Bugfix][Structured Output] Decode choice specs as JSON in outlines and LMFE (#60748)
Signed-off-by: hpdkhoa <hpdkhoa2311@gmail.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
@@ -16,7 +16,8 @@ from vllm.parser.engine.parser_engine import ReasoningEnd
|
||||
from vllm.sampling_params import SamplingParams, StructuredOutputsParams
|
||||
from vllm.v1.request import Request
|
||||
from vllm.v1.structured_output import StructuredOutputManager
|
||||
from vllm.v1.structured_output.backend_outlines import OutlinesGrammar
|
||||
from vllm.v1.structured_output.backend_outlines import OutlinesBackend, OutlinesGrammar
|
||||
from vllm.v1.structured_output.request import get_structured_output_key
|
||||
|
||||
TOKENIZER = "gpt2"
|
||||
THINK_END = "\n" # reasoning-end marker (single GPT-2 token)
|
||||
@@ -560,3 +561,18 @@ def test_outlines_termination(tokenizer):
|
||||
assert grammar.validate_tokens([eos]) == []
|
||||
assert grammar.accept_tokens(request.request_id, [one, eos, one])
|
||||
assert grammar.is_terminated()
|
||||
|
||||
|
||||
def test_outlines_choice_with_non_bmp_characters(tokenizer):
|
||||
"""The choice spec is JSON, which encodes emoji as surrogate pairs."""
|
||||
choices = ["😀 yes", "no"]
|
||||
backend = OutlinesBackend(
|
||||
VllmConfig(), tokenizer=tokenizer, vocab_size=len(tokenizer)
|
||||
)
|
||||
request_type, grammar_spec = get_structured_output_key(
|
||||
StructuredOutputsParams(choice=choices)
|
||||
)
|
||||
|
||||
grammar = backend.compile_grammar(request_type, grammar_spec)
|
||||
|
||||
assert grammar.accept_tokens("", tokenizer.encode(choices[0]))
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import ast
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from functools import lru_cache
|
||||
@@ -117,7 +116,7 @@ class LMFormatEnforcerBackend(StructuredOutputBackend):
|
||||
grammar_spec,
|
||||
)
|
||||
elif request_type == StructuredOutputOptions.CHOICE:
|
||||
choices = ast.literal_eval(grammar_spec)
|
||||
choices = json.loads(grammar_spec)
|
||||
character_level_parser = lmformatenforcer.UnionParser(
|
||||
[lmformatenforcer.StringParser(choice) for choice in choices]
|
||||
)
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from re import _constants, _parser # type: ignore[attr-defined]
|
||||
@@ -68,7 +67,7 @@ class OutlinesBackend(StructuredOutputBackend):
|
||||
elif request_type == StructuredOutputOptions.REGEX:
|
||||
regex = grammar_spec
|
||||
elif request_type == StructuredOutputOptions.CHOICE:
|
||||
choices = ast.literal_eval(grammar_spec)
|
||||
choices = json.loads(grammar_spec)
|
||||
choices = [regex_escape(c) for c in choices]
|
||||
regex = "(" + "|".join(choices) + ")"
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user