Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d8f6914

Browse filesBrowse files
authored
Add json schema mode (abetlen#1122)
* Add json schema mode * Add llava chat format support
1 parent c6d3bd6 commit d8f6914
Copy full SHA for d8f6914

File tree

Expand file treeCollapse file tree

2 files changed

+17
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-5
lines changed

‎llama_cpp/llama_chat_format.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_chat_format.py
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff 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(

‎llama_cpp/llama_types.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_types.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class ChatCompletionFunctionCallOption(TypedDict):
154154

155155
class ChatCompletionRequestResponseFormat(TypedDict):
156156
type: Literal["text", "json_object"]
157+
schema: NotRequired[JsonType] # https://docs.endpoints.anyscale.com/guides/json_mode/
157158

158159

159160
class ChatCompletionRequestMessageContentPartText(TypedDict):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.