support regex in xgrammar backend (#2983)
This commit is contained in:
committed by
GitHub
parent
2c05f81f15
commit
3bcf5ecea7
@@ -23,7 +23,7 @@ runtime_common = [
|
||||
"packaging", "pillow", "prometheus-client>=0.20.0",
|
||||
"psutil", "pydantic", "python-multipart",
|
||||
"pyzmq>=25.1.2", "torchao>=0.7.0", "uvicorn", "uvloop",
|
||||
"xgrammar>=0.1.6"
|
||||
"xgrammar>=0.1.10"
|
||||
]
|
||||
srt = [
|
||||
"sglang[runtime_common]", "cuda-python",
|
||||
|
||||
@@ -19,6 +19,7 @@ from typing import List, Tuple
|
||||
import torch
|
||||
from xgrammar import (
|
||||
CompiledGrammar,
|
||||
Grammar,
|
||||
GrammarCompiler,
|
||||
GrammarMatcher,
|
||||
TokenizerInfo,
|
||||
@@ -133,10 +134,13 @@ class XGrammarGrammarBackend(BaseGrammarBackend):
|
||||
logging.warning(f"Skip invalid ebnf: ebnf={key_string}, {e=}")
|
||||
return None
|
||||
elif key_type == "regex":
|
||||
logger.warning(
|
||||
"regex hasn't been supported by xgrammar yet. This is skipped."
|
||||
)
|
||||
return None
|
||||
try:
|
||||
ctx = self.grammar_compiler.compile_grammar(
|
||||
Grammar.from_regex(key_string)
|
||||
)
|
||||
except RuntimeError as e:
|
||||
logging.warning(f"Skip invalid regex: regex={key_string}, {e=}")
|
||||
return None
|
||||
else:
|
||||
raise ValueError(f"Invalid key_type: {key_type}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user