[Feature] integrate Structural Tag in xgrammar backend for function calling (#3566)

Co-authored-by: shuaills <shishuaiuoe@gmail.com>
This commit is contained in:
mlmzandshuaills authored and GitHub committed 2025-02-27 23:33:41 -08:00
1 parent eec3f6d1eb
commit bac414ab53
5 files changed
+44 -1

No files matched your search

+7
View File
@@ -994,10 +994,17 @@ def v1_chat_generate_request(
"ignore_eos": request.ignore_eos,
"skip_special_tokens": request.skip_special_tokens,
}
if request.response_format and request.response_format.type == "json_schema":
sampling_params["json_schema"] = convert_json_schema_to_str(
request.response_format.json_schema.schema_
)
elif (
request.response_format and request.response_format.type == "structural_tag"
):
sampling_params["structural_tag"] = convert_json_schema_to_str(
request.response_format.model_dump(by_alias=True)
)
sampling_params_list.append(sampling_params)
image_data_list.append(image_data)
+13 -1
View File
@@ -258,6 +258,18 @@ class ResponseFormat(BaseModel):
json_schema: Optional[JsonSchemaResponseFormat] = None
class StructuresResponseFormat(BaseModel):
begin: str
schema_: Optional[Dict[str, object]] = Field(alias="schema", default=None)
end: str
class StructuralTagResponseFormat(BaseModel):
type: Literal["structural_tag"]
structures: List[StructuresResponseFormat]
triggers: List[str]
class Function(BaseModel):
"""Function descriptions."""
@@ -298,7 +310,7 @@ class ChatCompletionRequest(BaseModel):
max_tokens: Optional[int] = None
n: int = 1
presence_penalty: float = 0.0
response_format: Optional[ResponseFormat] = None
response_format: Union[ResponseFormat, StructuralTagResponseFormat] = None
seed: Optional[int] = None
stop: Optional[Union[str, List[str]]] = None
stream: bool = False