{%- set available_tool_string, add_tool_id = '', true -%}{%- 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].' -%}{%- set reasoning_asst_turn_start='Here are my reasoning steps:
' -%}{%- if tools is not none and tools|length > 0 -%}    {%- set available_tools_string -%}You 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: <available_tools>{% for tool in tools %}{{ tool|string }}{% endfor %}</available_tools>.

Return all function calls as a list of json objects within <tool_calls></tool_calls> XML tags. Each json object should contain a function name and arguments as follows: <tool_calls>[{"name": <function-name-1>, "arguments": <args-dict-1>}, {"name": <function-name-2>, "arguments": <args-dict-2>},...]</tool_calls>    {%- endset -%}{%- endif -%}{%- if messages[0]['role'] != 'system' -%}{%- if tools is not none and tools|length > 0 -%}    {{- '

' + reasoning_prompt + '

' + available_tools_string + '


' -}}    {%- else -%}        {{- '

' + reasoning_prompt + '


' -}}    {%- endif -%}{%- endif -%}{%- for message in messages -%}    {%- if message['role'] == 'user' -%}        {{- '

' + message['content'] + '


' -}}    {%- elif message['role'] == 'system' -%}        {%- if tools is not none and tools|length > 0 -%}            {{- '

' + reasoning_prompt + '

' + message['content'] + '
' + available_tools_string + '


' -}}        {%- else -%}            {{- '

' + reasoning_prompt + '

' + message['content'] + '


' -}}        {%- endif -%}    {%- elif message['role'] == 'assistant' -%}        {%- if loop.last -%}            {%- set add_tool_id = false -%}        {%- endif -%}        {{- '

' -}}        {%- if message['content'] is not none -%}            {{- message['content'] -}}        {%- elif message['chosen'] is not none and message['chosen']|length > 0 -%}            {{- message['chosen'][0] -}}        {%- endif -%}        {%- if message['tool_calls'] is not none and message['tool_calls']|length > 0 -%}            {{- '
<tool_calls>[' -}}            {%- for tool_call in message["tool_calls"] -%}                {{- '{"name": "' + tool_call['function']['name'] + '", "arguments": ' + tool_call['function']['arguments']|string -}}                {%- if add_tool_id == true -%}                    {{- ', "id": "' + tool_call['id'] -}}                {%- endif -%}                {{- '}' -}}                {%- if not loop.last -%}{{- ', ' -}}{%- endif -%}            {%- endfor -%}            {{- ']</tool_calls>' -}}        {%- endif -%}        {%- if not loop.last or training_prompt -%}            {{- '


' -}}        {%- endif -%}    {%- elif message['role'] == 'tool' -%}        {{- '

' + message['content']|string + '


' -}}    {%- endif -%}    {%- if loop.last and add_generation_prompt and message['role'] != 'assistant' -%}        {{- '

' + reasoning_asst_turn_start -}}    {%- endif -%}{%- endfor -%}