From 906d795f15e4df3535f1b76af709932076a07797 Mon Sep 17 00:00:00 2001 From: gobraves Date: Sun, 1 Dec 2024 18:07:27 +0800 Subject: [PATCH] Feat: upgrade outlines & support compatibility with the old version (#2292) --- python/sglang/srt/constrained/outlines_backend.py | 7 ++++++- python/sglang/srt/constrained/outlines_jump_forward.py | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/python/sglang/srt/constrained/outlines_backend.py b/python/sglang/srt/constrained/outlines_backend.py index 994027ab3..26c476a05 100644 --- a/python/sglang/srt/constrained/outlines_backend.py +++ b/python/sglang/srt/constrained/outlines_backend.py @@ -152,7 +152,12 @@ class OutlinesGrammarBackend(BaseGrammarBackend): raise ValueError(f"Invalid key_type: {key_type}") try: - guide = RegexGuide(regex, self.outlines_tokenizer) + if hasattr(RegexGuide, "from_regex"): + # outlines >= 0.1.1 + guide = RegexGuide.from_regex(regex, self.outlines_tokenizer) + else: + # outlines <= 0.0.46 + guide = RegexGuide(regex, self.outlines_tokenizer) except interegular.patterns.InvalidSyntax as e: logger.warning(f"skip invalid regex schema: {regex=}, {e=}") return None diff --git a/python/sglang/srt/constrained/outlines_jump_forward.py b/python/sglang/srt/constrained/outlines_jump_forward.py index 4d6c03282..5e32c40d7 100644 --- a/python/sglang/srt/constrained/outlines_jump_forward.py +++ b/python/sglang/srt/constrained/outlines_jump_forward.py @@ -23,7 +23,14 @@ from collections import defaultdict import interegular from interegular import InvalidSyntax from outlines.caching import cache as disk_cache -from outlines.fsm.regex import FSMInfo, make_byte_level_fsm, make_deterministic_fsm + +try: + # outlines >= 0.1.0 + from outlines_core.fsm.outlines_core_rs import FSMInfo + from outlines_core.fsm.regex import make_byte_level_fsm, make_deterministic_fsm +except ImportError: + # outlines <= 0.0.46 + from outlines.fsm.regex import FSMInfo, make_byte_level_fsm, make_deterministic_fsm IP_REGEX = r"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)"