Felipe97/llama-cpp-compiled
01.2k
1{% macro render_content(content) %}{% if content is none %}{{- '' }}{% elif content is string %}{{- content }}{% elif content is mapping %}{{- content['value'] if 'value' in content else content['text'] }}{% elif content is iterable %}{% for item in content %}{% if item.type == 'text' %}{{- item['value'] if 'value' in item else item['text'] }}{% elif item.type == 'image' %}<im_patch>{% endif %}{% endfor %}{% endif %}{% endmacro %}2{{bos_token}}{%- if tools %}3 {{- '<|im_start|>system\n' }}4 {%- if messages[0].role == 'system' %}5 {{- render_content(messages[0].content) + '\n\n' }}6 {%- endif %}7 {{- "# Tools\n\nYou have access to the following functions in JSONSchema format:\n\n<tools>" }}8 {%- for tool in tools %}9 {{- "\n" }}10 {{- tool | tojson(ensure_ascii=False) }}11 {%- endfor %}12 {{- "\n</tools>\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...>\n...\n</function> block must be nested within <tool_call>\n...\n</tool_call> XML tags\n- Required parameters MUST be specified\n</IMPORTANT><|im_end|>\n" }}13{%- else %}14 {%- if messages[0].role == 'system' %}15 {{- '<|im_start|>system\n' + render_content(messages[0].content) + '<|im_end|>\n' }}16 {%- endif %}17{%- endif %}18{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}19{%- for message in messages[::-1] %}20 {%- set index = (messages|length - 1) - loop.index0 %}21 {%- if ns.multi_step_tool and message.role == "user" and render_content(message.content) is string and not(render_content(message.content).startswith('<tool_response>') and render_content(message.content).endswith('</tool_response>')) %}22 {%- set ns.multi_step_tool = false %}23 {%- set ns.last_query_index = index %}24 {%- endif %}25{%- endfor %}26{%- for message in messages %}27 {%- set content = render_content(message.content) %}28 {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}29 {%- set role_name = 'observation' if (message.role == "system" and not loop.first and message.name == 'observation') else message.role %}30 {{- '<|im_start|>' + role_name + '\n' + content + '<|im_end|>' + '\n' }}31 {%- elif message.role == "assistant" %}32 {%- if message.reasoning_content is string %}33 {%- set reasoning_content = render_content(message.reasoning_content) %}34 {%- else %}35 {%- if '</think>' in content %}36 {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}37 {%- set content = content.split('</think>')[-1].lstrip('\n') %}38 {%- else %}39 {%- set reasoning_content = '' %}40 {%- endif %}41 {%- endif %}42 {%- if loop.index0 > ns.last_query_index %}43 {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n' + content }}44 {%- else %}45 {{- '<|im_start|>' + message.role + '\n' + content }}46 {%- endif %}47 {%- if message.tool_calls %}48 {%- for tool_call in message.tool_calls %}49 {%- if tool_call.function is defined %}50 {%- set tool_call = tool_call.function %}51 {%- endif %}52 {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}53 {%- if tool_call.arguments is defined %}54 {%- set arguments = tool_call.arguments %}55 {%- for args_name, args_value in arguments|items %}56 {{- '<parameter=' + args_name + '>\n' }}57 {%- set args_value = args_value | tojson(ensure_ascii=False) | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}58 {{- args_value }}59 {{- '\n</parameter>\n' }}60 {%- endfor %}61 {%- endif %}62 {{- '</function>\n</tool_call>' }}63 {%- endfor %}64 {%- endif %}65 {{- '<|im_end|>\n' }}66 {%- elif message.role == "tool" %}67 {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}68 {{- '<|im_start|>tool_response\n' }}69 {%- endif %}70 {{- '<tool_response>' }}71 {{- content }}72 {{- '</tool_response>' }}73 {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}74 {{- '<|im_end|>\n' }}75 {%- endif %}76 {%- endif %}77{%- endfor %}78{%- if add_generation_prompt %}79 {{- '<|im_start|>assistant\n<think>\n' }}80{%- endif %}81 