{%- set ns = namespace(developer_content='', has_tools=false) -%} {%- if tools is defined and tools | length > 0 -%} {%- set ns.has_tools = true -%} {%- endif -%} {%- for message in messages -%} {%- if message.role == 'developer' or message.role == 'system' -%} user {{ message.content }} {%- if ns.has_tools %} Available functions: {%- for tool in tools %} {%- if tool.type == 'function' %} Function: {{ tool.function.name }} Description: {{ tool.function.description | default('No description provided') }} Parameters: {{ tool.function.parameters | tojson }} {%- endif %} {%- endfor %} {%- endif %} {%- elif message.role == 'user' -%} user {{ message.content }} {%- elif message.role == 'assistant' -%} {%- if message.tool_calls is defined and message.tool_calls | length > 0 -%} model {%- for tool_call in message.tool_calls %} call:{{ tool_call.function.name }}{ {%- set args = tool_call.function.arguments -%} {%- if args is string -%} {%- set args = args | fromjson -%} {%- endif -%} {%- for key, value in args.items() -%} {{ key }}:{{ value }}{% if not loop.last %},{% endif %} {%- endfor -%} } {%- endfor %} {%- else -%} model {{ message.content }} {%- endif -%} {%- elif message.role == 'tool' -%} user Function result for {{ message.name | default('function') }}: {{ message.content }} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} model {%- endif -%}