Skip to content

Commit e96c7c2

Browse files
committed
improve gguf-function-calling parser
1 parent a7d162b commit e96c7c2

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4151,28 +4151,20 @@ def gguf_function_calling(
41514151
"<|im_start|>{{ message.role }}\n"
41524152
# System message
41534153
"{% if message.role == 'system' %}"
4154-
"<|system|>\n"
41554154
"{{ message.content }}"
4156-
"{% if tools %}"
4157-
"\n# Tools\n"
4158-
"\nYou may call one or more functions to assist with the user query."
4159-
"\nPrefer proposing the function first; only emit `<function_calls>` when user intent to run is clear from context."
4160-
"\n\nSoft consent policy:"
4161-
"\n- Proceed immediately if the user explicitly asks to run/fetch/call/use a tool, or previously agreed, or supplied all required parameters."
4162-
"\n- Otherwise: reply with a one-line proposal naming the function and why, and end with “Proceed?”."
4163-
"\n- Do not over-ask if consent is obvious."
4164-
"\n\nYou are provided with function signatures within <tools></tools> XML tags:"
4165-
"\n<tools>"
4155+
"{% if tool_calls %}"
4156+
"\n\nYou have access to the following functions:\n"
41664157
"{% for tool in tools %}"
4167-
"{{ tool | tojson }}"
4168-
"{% endfor %}"
4169-
"</tools>"
4170-
"\n\nYou can respond in two ways:"
4171-
"\n\n1. Message only (proposal/confirmation step):"
4172-
"\nmessage:"
4173-
"\nI can use `ExampleFunction` to retrieve that. Proceed?"
4174-
"\n\n2. Message + function calls (when intent is clearly allowed by context):"
4175-
"\nmessage:"
4158+
'\n{% if tool.function.get("description") %}/* {{ tool.function.description | trim }} */{% endif %}'
4159+
"\nfunctions.{{ tool.function.name }}:\n"
4160+
"{{ tool.function.parameters | tojson }}"
4161+
"\n{% endfor %}"
4162+
"\nYou must respond to user messages with either a single message or with one or more function calls."
4163+
"\n\nTo respond with a message use the following format:"
4164+
"\n\nmessage:"
4165+
"\n<message>"
4166+
"\n\nTo respond with one or more function calls use the following format:"
4167+
"\n\n<function_calls>"
41764168
"\nfunctions.<function_name>:"
41774169
'\n{ "arg1": "value1", "arg2": "value2" }'
41784170
"\nfunctions.<function_name>:"
@@ -4208,14 +4200,10 @@ def gguf_function_calling(
42084200
"{% endfor %}"
42094201
"{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
42104202
)
4211-
42124203
if hasattr(llama, 'model_path'):
42134204
metadata = llama.metadata
42144205
if metadata and "tokenizer.chat_template" in metadata:
42154206
function_calling_template = metadata["tokenizer.chat_template"]
4216-
4217-
if kwargs.get('chat_template'):
4218-
function_calling_template = kwargs.get('chat_template')
42194207

42204208
template_renderer = ImmutableSandboxedEnvironment(
42214209
autoescape=jinja2.select_autoescape(["html", "xml"]),

0 commit comments

Comments
 (0)