fix: fix ipython running error for Engine due to outlines nest_asyncio (#4582)

Co-authored-by: shuaills <shishuaiuoe@gmail.com>
This commit is contained in:
mlmz
2025-03-22 10:11:15 +08:00
committed by GitHub
parent c7c7dbebbe
commit f6ab4ca6bc
4 changed files with 37 additions and 14 deletions

View File

@@ -26,13 +26,6 @@ from fastapi import HTTPException, Request, UploadFile
from fastapi.responses import ORJSONResponse, StreamingResponse
from pydantic import ValidationError
try:
from outlines.fsm.json_schema import convert_json_schema_to_str
except ImportError:
# Before outlines 0.0.47, convert_json_schema_to_str is under
# outlines.integrations.utils
from outlines.integrations.utils import convert_json_schema_to_str
from sglang.srt.code_completion_parser import (
generate_completion_prompt_from_request,
is_completion_template_defined,
@@ -79,7 +72,7 @@ from sglang.srt.openai_api.protocol import (
UsageInfo,
)
from sglang.srt.reasoning_parser import ReasoningParser
from sglang.utils import get_exception_traceback
from sglang.utils import convert_json_schema_to_str, get_exception_traceback
logger = logging.getLogger(__name__)

View File

@@ -22,6 +22,7 @@ from typing import Any, Callable, List, Optional, Tuple, Type, Union
import numpy as np
import requests
from IPython.display import HTML, display
from pydantic import BaseModel
from tqdm import tqdm
from sglang.srt.utils import kill_process_tree
@@ -29,6 +30,36 @@ from sglang.srt.utils import kill_process_tree
logger = logging.getLogger(__name__)
def convert_json_schema_to_str(json_schema: Union[dict, str, Type[BaseModel]]) -> str:
"""Convert a JSON schema to a string.
Parameters
----------
json_schema
The JSON schema.
Returns
-------
str
The JSON schema converted to a string.
Raises
------
ValueError
If the schema is not a dictionary, a string or a Pydantic class.
"""
if isinstance(json_schema, dict):
schema_str = json.dumps(json_schema)
elif isinstance(json_schema, str):
schema_str = json_schema
elif issubclass(json_schema, BaseModel):
schema_str = json.dumps(json_schema.model_json_schema())
else:
raise ValueError(
f"Cannot parse schema {json_schema}. The schema must be either "
+ "a Pydantic class, a dictionary or a string that contains the JSON "
+ "schema specification"
)
return schema_str
def get_exception_traceback():
etype, value, tb = sys.exc_info()
err_str = "".join(traceback.format_exception(etype, value, tb))