{%- if tools %} {{- '<|BOT|>system ' }} {%- if messages[0]['role'] == 'system' %} {{- messages[0]['content'] + '<|EOT|>' }} {%- else %} {{- 'You are a helpful assistant. Please think step by step and provide your reasoning process within tags, followed by your final answer. Format: your reasoning hereyour final answer<|EOT|>' }} {%- endif %} {{- '<|BOT|>' }} {{- "tool_json_schemas " }} {{- tools | tojson }} {{- '<|EOT|>' }} {%- else %} {%- if messages[0]['role'] == 'system' %} {{- '<|BOT|>system ' + messages[0]['content'] + '<|EOT|>' }} {%- else %} {{- '<|BOT|>system You are a helpful assistant. Please think step by step and provide your reasoning process within tags, followed by your final answer. Format: your reasoning hereyour final answer<|EOT|>' }} {%- endif %} {%- endif %} {%- for message in messages %} {%- if message["role"] == "user" %} {{- '<|BOT|>human ' + message["content"] + '<|EOT|>' }} {%- elif (message["role"] == "system" and not loop.first) or (message["role"] == "assistant" and not message["tool_calls"]) %} {{- '<|BOT|>' + message["role"] + ' ' + message["content"] + '<|EOT|>' }} {%- elif message["role"] == "assistant" %} {{- '<|BOT|>' + message["role"] + ' ' }} {%- if message["content"] %} {{- message["content"] }} {%- endif %} {%- for tool_call in message.tool_calls %} {%- if tool_call["function"] is defined %} {%- set tool_call = tool_call["function"] %} {%- endif %} {{- '<|CALL_START|>' + 'function ' + tool_call["name"] + ' ' }} {{- tool_call["arguments"] | tojson }} {{- '<|CALL_END|>' }} {%- endfor %} {{- '<|EOT|>' }} {%- elif message["role"] == "tool" %} {{- '<|BOT|>' }} {%- set ns = namespace(function_name="tool") %} {%- if message["tool_call_id"] %} {%- for prev_msg in messages %} {%- if prev_msg["role"] == "assistant" and prev_msg["tool_calls"] %} {%- for tool_call in prev_msg["tool_calls"] %} {%- if tool_call["id"] == message["tool_call_id"] %} {%- if tool_call["function"] is defined %} {%- set ns.function_name = tool_call["function"]["name"] %} {%- endif %} {%- endif %} {%- endfor %} {%- endif %} {%- endfor %} {%- endif %} {{- 'function_output ' + ns.function_name + ' ' }} {{- message["content"] }} {{- '<|EOT|>' }} {%- endif %} {%- endfor %} {%- if add_generation_prompt %} {{- '<|BOT|>assistant ' }} {%- endif %}