Skip to content

Commit 7e32386

Browse files
committed
improve gguf-function-calling parser
1 parent dccabe6 commit 7e32386

File tree

2 files changed

+12
-392
lines changed

2 files changed

+12
-392
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,16 +4270,19 @@ def gguf_function_calling(
42704270
messages=messages, tools=tools, tool_calls=True, add_generation_prompt=True
42714271
)
42724272
initial_gbnf_tool_grammar = (
4273-
(
4274-
'root ::= message_only | message_with_functions | functions_only\n'
4275-
'message_only ::= "message:" text\n'
4276-
'message_with_functions ::= "message:" text "<function_calls>\\n" functions\n'
4277-
'functions_only ::= "<function_calls>\\n" functions\n'
4278-
'text ::= [^<]+\n'
4279-
f"functions ::= {function_names}\n"
4280-
)
4273+
f"""
4274+
root ::= message_only | message_with_functions | functions_only
4275+
message_only ::= "message:" content
4276+
message_with_functions ::= "message:" content "<function_calls>\\n" functions
4277+
functions_only ::= "<function_calls>\\n" functions
4278+
content ::= ([^<] | "<" [^f] )+
4279+
functions ::= {function_names}
4280+
"""
42814281
if tool_choice == "auto"
4282-
else f'root ::= "<function_calls>\\n" functions\nfunctions ::= {function_names}\n'
4282+
else f"""
4283+
root ::= "<function_calls>\\n" functions
4284+
functions ::= {function_names}
4285+
"""
42834286
)
42844287
completion = cast(
42854288
llama_types.CreateCompletionResponse,

0 commit comments

Comments
 (0)