{% set has_system = messages|selectattr('role', 'equalto', 'system')|list|length > 0 %}{% if not has_system %}{{ '<|im_start|>system\nYou are a helpful AI assistant. You do not currently have access to any functions. <|im_end|>\n' }}{% endif %}{% for message in messages %}{% if message['role'] == 'system' %}{{ '<|im_start|>system\n' + message['content'] }}{% if message.get('functions', none) is not none %}{{ ' ' + message['functions'] + '<|im_end|>\n' }}{% else %}{{ ' You do not currently have access to any functions. <|im_end|>\n' }}{% endif %}{% elif message['role'] == 'user' %}{% if message.get('functions', none) is not none %}{{ '<|im_start|>user\n' + message['content'] + '\n' + message['functions'] + '<|im_end|>\n' }}{% else %}{{ '<|im_start|>user\n' + message['content'] + '<|im_end|>\n' }}{% endif %}{% elif message['role'] == 'assistant' %}{{ '<|im_start|>assistant\n' }}{% if message.get('content', none) is not none %}{{ message['content'] }}{% endif %}{% if message.get('function_calls', none) is not none %}{{ '' + message['function_calls'] + '' }}{% endif %}{% if not loop.last %}{{ '<|im_end|>\n' }}{% else %}{{ '<|endoftext|>' }}{% endif %}{% elif message['role'] == 'observation' %}{{ '<|im_start|>environment\n' + message['content'] + '<|im_end|>\n' }}{% elif message['role'] == 'function' %}{{ '<|im_start|>environment\n' + message['content'] + '<|im_end|>\n' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}