{% set has_system = messages|selectattr('role', 'equalto', 'system')|list|length > 0 %}{% if not has_system %}{{ '<|im_start|>system
You are a helpful AI assistant.<|im_end|>
' }}{% endif %}{% for message in messages %}{% if message['role'] == 'system' %}{{ '<|im_start|>system
' + message['content'] }}{% if tools is defined and tools %}{{ ' ' }}{{ tools | tojson }}{{ '<|im_end|>
' }}{% elif message.get('functions', none) is not none %}{{ ' ' }}{{ message['functions'] }}{{ '<|im_end|>
' }}{% else %}{{ ' You do not currently have access to any functions. <|im_end|>
' }}{% endif %}{% elif message['role'] == 'user' %}{% if message.get('functions', none) is not none %}{{ '<|im_start|>user
' + message['content'] + '
' + '' + message['functions'] + '<|im_end|>
' }}{% else %}{{ '<|im_start|>user
' + message['content'] + '<|im_end|>
' }}{% endif %}{% elif message['role'] == 'assistant' %}{{ '<|im_start|>assistant
' }}{% if message.get('content', none) is not none %}{{ message['content'] }}{% endif %}{% if message.get('function_calls', none) is not none %}{{ '' + message['function_calls'] + '' }}{% elif message.get('tool_calls', none) is not none %}{{ '' }}{%- for tc in message['tool_calls'] %}{%- if tc is mapping and tc.get('function', none) is not none %}{%- set a = tc['function']['arguments'] %}{%- set ns = namespace(al=[]) %}{%- for k,v in a.items() %}{%- set ns.al = ns.al + [k ~ '=' ~ (v | tojson)] %}{%- endfor %}{{- tc['function']['name'] + '(' + (ns.al | join(', ')) + ')' }}{%- if not loop.last %}{{ '
' }}{%- endif %}{%- else %}{{ tc }}{%- endif %}{%- endfor %}{{ '' }}{% endif %}{% if not loop.last %}{{ '<|im_end|>' + '
' }}{% else %}{{ eos_token }}{% endif %}{% elif message['role'] == 'environment' %}{{ '<|im_start|>environment
' + message['content'] + '<|im_end|>
' }}{% elif message['role'] == 'tool' %}{{ '<|im_start|>environment
' + message['content'] + '<|im_end|>
' }}{% endif %}{% if loop.last and add_generation_prompt %}{{ '<|im_start|>assistant
' }}{% endif %}{% endfor %}