File tree Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -318,7 +318,14 @@ def chat_completion_handler(
318318 stop = stop + rstop
319319
320320 if response_format is not None and response_format ["type" ] == "json_object" :
321- grammar = llama_grammar .LlamaGrammar .from_string (llama_grammar .JSON_GBNF )
321+ try :
322+ # create grammar from json schema
323+ if "schema" in response_format :
324+ grammar = llama_grammar .LlamaGrammar .from_json_schema (
325+ json .dumps (response_format ["schema" ])
326+ )
327+ except Exception as e :
328+ grammar = llama_grammar .LlamaGrammar .from_string (llama_grammar .JSON_GBNF )
322329
323330 completion_or_chunks = llama .create_completion (
324331 prompt = prompt ,
@@ -1434,10 +1441,14 @@ def __call__(
14341441 prompt = llama .input_ids [: llama .n_tokens ].tolist ()
14351442
14361443 if response_format is not None and response_format ["type" ] == "json_object" :
1437- with suppress_stdout_stderr (disable = self .verbose ):
1438- grammar = llama_grammar .LlamaGrammar .from_string (
1439- llama_grammar .JSON_GBNF
1440- )
1444+ try :
1445+ # create grammar from json schema
1446+ if "schema" in response_format :
1447+ grammar = llama_grammar .LlamaGrammar .from_json_schema (
1448+ json .dumps (response_format ["schema" ])
1449+ )
1450+ except Exception as e :
1451+ grammar = llama_grammar .LlamaGrammar .from_string (llama_grammar .JSON_GBNF )
14411452
14421453 return _convert_completion_to_chat (
14431454 llama .create_completion (
Original file line number Diff line number Diff line change @@ -154,6 +154,7 @@ class ChatCompletionFunctionCallOption(TypedDict):
154154
155155class ChatCompletionRequestResponseFormat (TypedDict ):
156156 type : Literal ["text" , "json_object" ]
157+ schema : NotRequired [JsonType ] # https://docs.endpoints.anyscale.com/guides/json_mode/
157158
158159
159160class ChatCompletionRequestMessageContentPartText (TypedDict ):
You can’t perform that action at this time.
0 commit comments