[Frontend] add json_schema support from OpenAI protocol (#7654)

This commit is contained in:
Tyler Rockwood
2024-08-24 01:07:24 -05:00
committed by GitHub
parent 8da48e4d95
commit d81abefd2e
4 changed files with 59 additions and 2 deletions

View File

@@ -49,6 +49,13 @@ async def get_lm_format_enforcer_guided_decoding_logits_processor(
and request.response_format.type == "json_object"):
character_level_parser = JsonSchemaParser(
None) # None means any json object
elif (request.response_format is not None
and request.response_format.type == "json_schema"
and request.response_format.json_schema is not None
and request.response_format.json_schema.json_schema is not None):
schema = _normalize_json_schema_object(
request.response_format.json_schema.json_schema)
character_level_parser = JsonSchemaParser(schema)
else:
return None

View File

@@ -127,6 +127,13 @@ def _get_guide_and_mode(
and request.response_format is not None
and request.response_format.type == "json_object"):
return JSON_GRAMMAR, GuidedDecodingMode.GRAMMAR
elif (not isinstance(request, GuidedDecodingRequest)
and request.response_format is not None
and request.response_format.type == "json_schema"
and request.response_format.json_schema is not None
and request.response_format.json_schema.json_schema is not None):
json = json_dumps(request.response_format.json_schema.json_schema)
return json, GuidedDecodingMode.JSON
else:
return None, None