algorithm commited on
Commit
5f4c99d
·
verified ·
1 Parent(s): 0e2637b

Fix template for llama.cpp

Browse files

Hi,

I have made fixes to the template, there were quite a few.
Please take a look and see what you think.
The problem is that it isn't compatible with llama.cpp in its current form.

To me more specific, these were the errors:

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected closing block tag at row 1, column 91:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected closing block tag at row 2, column 111:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected value expression at row 1, column 136:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected value expression at row 1, column 748:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected value expression at row 1, column 1163:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected value expression at row 1, column 2369:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected closing expression tag at row 1, column 2407:```

```common_chat_templates_init: failed to parse chat template (defaulting to chatml): Expected value expression at row 1, column 5341:```

Files changed (1) hide show
  1. chat_template.json +1 -1
chat_template.json CHANGED
@@ -1,3 +1,3 @@
1
  {
2
- "chat_template": "{%- set available_tools_string, thought_instructions, add_tool_id, tool_output_format = '', '', true, \"default\" -%}\n\n{%- if tools is not none and tools|length > 0 -%}\n {%- set available_tools_string -%}\nYou are provided with function signatures within <available_tools></available_tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about the arguments. You should infer the argument values from previous user responses and the system message. Here are the available tools:\n<available_tools>\n{% for tool in tools %}\n{{ tool|string }}\n{% endfor %}\n</available_tools>\n{%- endset -%}\n{%- endif -%}\n{%- if tool_output_format is none or tool_output_format == \"default\" -%}\n{%- set tool_output_instructions -%}\nReturn all function calls as a list of json objects within <tool_call></tool_call> XML tags. Each json object should contain a function name and arguments as follows:\n<tool_calls>[{\"name\": <function-name-1>, \"arguments\": <args-dict-1>}, {\"name\": <function-name-2>, \"arguments\": <args-dict-2>},...]</tool_calls>\n{%- endset -%}\n{%- elif tool_output_format == \"yaml\" -%}\n{%- set tool_output_instructions -%}\nReturn all function calls as a list of yaml objects within <tool_call></tool_call> XML tags. Each yaml object should contain a function name and arguments as follows:\n<tool_calls>\n- name: <function-name-1>\n arguments: <args-dict-1>\n- name: <function-name-2>\n arguments: <args-dict-2>\n...\n</tool_calls>\n{%- endset -%}\n{%- endif -%}\n{%- if add_thoughts -%}\n{%- set thought_instructions -%}\nPrior to generating the function calls, you should generate the reasoning for why you're calling the function. Please generate these reasoning thoughts between <thinking> and </thinking> XML tags.\n{%- endset -%}\n{%- endif -%}\n{{- bos_token -}}\n{%- set reasoning_prompt='You are a thoughtful and systematic AI assistant built by ServiceNow Language Models (SLAM) lab. Before providing an answer, analyze the problem carefully and present your reasoning step by step. After explaining your thought process, provide the final solution in the following format: [BEGIN FINAL RESPONSE] ... [END FINAL RESPONSE].' -%}\n{%- if messages[0]['role'] != 'system' and tools is not none and tools|length > 0 -%}\n {{- '<|system|>\\n' + reasoning_prompt + available_tools_string + \"\\n\" + tool_output_instructions + '\\n<|end|>\\n' -}}\n{%- endif -%}\n{%- if messages|selectattr('role', 'equalto', 'system')|list|length == 0 -%}\n{{- '<|system|>\\n' + reasoning_prompt + '\\n<|end|>\\n' -}}\n{%- endif -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'user' -%}\n {{- '<|user|>\\n' }}\n {%- if message['content'] is not string %}\n {%- for chunk in message['content'] %}\n {%- if chunk['type'] == 'text' %}\n {{- chunk['text'] }}\n {%- elif chunk['type'] == 'image' or chunk['type'] == 'image_url'%}\n {{- '[IMG]' }}\n {%- else %}\n {{- raise_exception('Unrecognized content type!') }}\n {%- endif %}\n {%- endfor %}\n {%- else %}\n {{- message['content'] }}\n {%- endif %}\n {{- '\\n<|end|>\\n' }}\n {%- elif message['role'] == 'content' -%}\n {%- if message['content'] is not string %}\n {{- '<|content|>\\n' + message['content'][0]['text'] + '\\n<|end|>\\n' -}}\n {%- else %}\n {{- '<|content|>\\n' + message['content'] + '\\n<|end|>\\n' -}}\n {%- endif -%}\n {%- elif message['role'] == 'system' -%}\n {%- if message['content'] is not none and message['content']|length > 0 %}\n {%- if message['content'] is string %}\n {%- set system_message = message['content'] %}\n {%- else %}\n {%- set system_message = message['content'][0]['text'] %}\n {%- endif %}\n {%- else %}\n {%- set system_message = '' %}\n {%- endif %}\n {%- if tools is not none and tools|length > 0 -%}\n {{- '<|system|>\\n' + reasoning_prompt + system_message + '\\n' + available_tools_string + '\\n<|end|>\\n' -}}\n {%- else -%}\n {{- '<|system|>\\n' + reasoning_prompt + system_message + '\\n<|end|>\\n' -}}\n {%- endif -%}\n {%- elif message['role'] == 'assistant' -%}\n {%- if loop.last -%}\n {%- set add_tool_id = false -%}\n {%- endif -%}\n {{- '<|assistant|>\\n' -}}\n {%- if message['content'] is not none and message['content']|length > 0 -%}\n {%- if message['content'] is not string and message['content'][0]['text'] is not none %}\n {{- message['content'][0]['text'] }}\n {%- else %}\n {{- message['content'] -}}\n {%- endif -%}\n {%- elif message['chosen'] is not none and message['chosen']|length > 0 -%}\n {{- message['chosen'][0] -}}\n {%- endif -%}\n {%- if add_thoughts and 'thought' in message and message['thought'] is not none -%}\n {{- '<thinking>' + message['thought'] + '</thinking>' -}}\n {%- endif -%}\n {%- if message['tool_calls'] is not none and message['tool_calls']|length > 0 -%}\n {{- '\\n<tool_calls>[' -}}\n {%- for tool_call in message[\"tool_calls\"] -%}\n {{- '{\"name\": \"' + tool_call['function']['name'] + '\", \"arguments\": ' + tool_call['function']['arguments']|string -}}\n {%- if add_tool_id == true -%}\n {{- ', \"id\": \"' + tool_call['id'] + '\"' -}}\n {%- endif -%}\n {{- '}' -}}\n {%- if not loop.last -%}{{- ', ' -}}{%- endif -%}\n {%- endfor -%}\n {{- ']</tool_calls>' -}}\n {%- endif -%}\n {{- '\\n<|end|>\\n' + eos_token -}}\n {%- elif message['role'] == 'tool' -%}\n {%- if message['content'] is string %}\n {%- set tool_message = message['content'] %}\n {%- else %}\n {%- set tool_message = message['content'][0]['text'] %}\n {%- endif -%}\n {{- '<|tool_result|>\\n' + tool_message|string + '\\n<|end|>\\n' -}}\n {%- endif -%}\n {%- if loop.last and add_generation_prompt and message['role'] != 'assistant' -%}\n {{- '<|assistant|>\\n' -}}\n {%- endif -%}\n{%- endfor -%}"
3
  }
 
1
  {
2
+ "{%- set available_tools_string = '' -%}{%- set thought_instructions = '' -%}{%- set add_tool_id = true -%}{%- set tool_output_format = 'default' -%}\n\n{% if tools is not none and tools|length > 0 %}\n {% set available_tools_string %}\nYou are provided with function signatures within <available_tools></available_tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about the arguments. You should infer the argument values from previous user responses and the system message. Here are the available tools:\n<available_tools>\n{% for tool in tools %}\n{{ tool|string }}\n{% endfor %}\n</available_tools>\n{% endset %}\n{% endif %}\n{% if tool_output_format is none or tool_output_format == 'default' %}\n{% set tool_output_instructions %}\nReturn all function calls as a list of json objects within <tool_call></tool_call> XML tags. Each json object should contain a function name and arguments as follows:\n<tool_calls>[{\"name\": <function-name-1>, \"arguments\": <args-dict-1>}, {\"name\": <function-name-2>, \"arguments\": <args-dict-2>},...]</tool_calls>\n{% endset %}\n{% elif tool_output_format == 'yaml' %}\n{% set tool_output_instructions %}\nReturn all function calls as a list of yaml objects within <tool_call></tool_call> XML tags. Each yaml object should contain a function name and arguments as follows:\n<tool_calls>\n- name: <function-name-1>\n arguments: <args-dict-1>\n- name: <function-name-2>\n arguments: <args-dict-2>\n...\n</tool_calls>\n{% endset %}\n{% endif %}\n{% if add_thoughts %}\n{% set thought_instructions %}\nPrior to generating the function calls, you should generate the reasoning for why you're calling the function. Please generate these reasoning thoughts between <thinking> and </thinking> XML tags.\n{% endset %}\n{% endif %}\n{{- bos_token -}}\n{% set reasoning_prompt='You are a thoughtful and systematic AI assistant built by ServiceNow Language Models (SLAM) lab. Before providing an answer, analyze the problem carefully and present your reasoning step by step. After explaining your thought process, provide the final solution in the following format: [BEGIN FINAL RESPONSE] ... [END FINAL RESPONSE].' %}\n{% if messages[0]['role'] != 'system' and tools is not none and tools|length > 0 %}\n {{- '<|system|>\\n' + reasoning_prompt + available_tools_string + '\\n' + tool_output_instructions + '\\n<|end|>\\n' -}}\n{% endif %}\n{% if messages|selectattr('role', 'equalto', 'system')|list|length == 0 %}\n{{- '<|system|>\\n' + reasoning_prompt + '\\n<|end|>\\n' -}}\n{% endif %}\n{% for message in messages %}\n {% if message['role'] == 'user' %}\n {{- '<|user|>\\n' }}\n {% if message['content'] is not string %}\n {% for chunk in message['content'] %}\n {% if chunk['type'] == 'text' %}\n {{- chunk['text'] }}\n {% elif chunk['type'] == 'image' or chunk['type'] == 'image_url'%}\n {{- '[IMG]' }}\n {% else %}\n {{- raise_exception('Unrecognized content type!') }}\n {% endif %}\n {% endfor %}\n {% else %}\n {{- message['content'] }}\n {% endif %}\n {{- '\\n<|end|>\\n' }}\n {% elif message['role'] == 'content' %}\n {% if message['content'] is not string %}\n {{- '<|content|>\\n' + message['content'][0]['text'] + '\\n<|end|>\\n' -}}\n {% else %}\n {{- '<|content|>\\n' + message['content'] + '\\n<|end|>\\n' -}}\n {% endif %}\n {% elif message['role'] == 'system' %}\n {% if message['content'] is not none and message['content']|length > 0 %}\n {% if message['content'] is string %}\n {% set system_message = message['content'] %}\n {% else %}\n {% set system_message = message['content'][0]['text'] %}\n {% endif %}\n {% else %}\n {% set system_message = '' %}\n {% endif %}\n {% if tools is not none and tools|length > 0 %}\n {{- '<|system|>\\n' + reasoning_prompt + system_message + '\\n' + available_tools_string + '\\n<|end|>\\n' -}}\n {% else %}\n {{- '<|system|>\\n' + reasoning_prompt + system_message + '\\n<|end|>\\n' -}}\n {% endif %}\n {% elif message['role'] == 'assistant' %}\n {% if loop.last %}\n {% set add_tool_id = false %}\n {% endif %}\n {{- '<|assistant|>\\n' -}}\n {% if message['content'] is not none and message['content']|length > 0 %}\n {% if message['content'] is not string and message['content'][0]['text'] is not none %}\n {{- message['content'][0]['text'] }}\n {% else %}\n {{- message['content'] -}}\n {% endif %}\n {% elif message['chosen'] is not none and message['chosen']|length > 0 %}\n {{- message['chosen'][0] -}}\n {% endif %}\n {% if add_thoughts and 'thought' in message and message['thought'] is not none %}\n {{- '<thinking>' + message['thought'] + '</thinking>' -}}\n {% endif %}\n {% if message['tool_calls'] is not none and message['tool_calls']|length > 0 %}\n {{- '\\n<tool_calls>[' -}}\n {% for tool_call in message['tool_calls'] %}\n {{- '{\"name\": \"' + tool_call['function']['name'] + '\", \"arguments\": ' + tool_call['function']['arguments']|string -}}\n {% if add_tool_id == true %}\n {{- ', \"id\": \"' + tool_call['id'] + '\"' -}}\n {% endif %}\n {{- '}' -}}\n {% if not loop.last %}{{- ', ' -}}{% endif %}\n {% endfor %}\n {{- ']</tool_calls>' -}}\n {% endif %}\n {{- '\\n<|end|>\\n' + eos_token -}}\n {% elif message['role'] == 'tool' %}\n {% if message['content'] is string %}\n {% set tool_message = message['content'] %}\n {% else %}\n {% set tool_message = message['content'][0]['text'] %}\n {% endif %}\n {{- '<|tool_result|>\\n' + tool_message|string + '\\n<|end|>\\n' -}}\n {% endif %}\n {% if loop.last and add_generation_prompt and message['role'] != 'assistant' %}\n {{- '<|assistant|>\\n' -}}\n {% endif %}\n{% endfor %}"
3
  }