{# ================= SYSTEM PROMPT ================= #} {%- if messages and messages[0]['role'] == 'system' %} {%- set system_msg = messages[0]['content'] %} {%- set remaining_messages = messages[1:] %} {%- elif messages and messages[0]['role'] == 'developer' %} {%- set system_msg = messages[0]['content'] %} {%- set remaining_messages = messages[1:] %} {%- else %} {%- set system_msg = "You are Falcon, a helpful AI assistant created by Technology Innovation Institute (TII)." %} {%- set remaining_messages = messages %} {%- endif %} {%- if tools %} <|im_start|>system {{ system_msg }} # Tools You may call one or more functions to assist with the user query. {%- for tool in tools %} {{ tool | tojson }} {%- endfor %} <|im_end|> {%- else %} <|im_start|>system {{ system_msg }} <|im_end|> {%- endif %} {# ================= FIND LAST USER QUERY ================= #} {%- set ns = namespace(multi_step_tool=true, last_query_index=remaining_messages|length - 1) %} {%- for message in remaining_messages %} {%- if message.role == "user" %} {%- set ns.last_query_index = loop.index0 %} {%- endif %} {%- endfor %} {# ================= RENDER MESSAGES ================= #} {%- for message in remaining_messages %} {# ---- Normalize content to string ---- #} {%- set content = message.get('content', '') %} {%- set reasoning_content = message.get('reasoning_content', none) %} {%- if content is string %} {%- set content_str = content %} {%- elif content is not none %} {%- set ns_content = namespace(text='') %} {%- for item in content if item.type == 'text' %} {%- set ns_content.text = ns_content.text ~ item.text %} {%- endfor %} {%- set content_str = ns_content.text %} {%- endif %} {# ================= USER ================= #} {%- if message.role in ['user', 'human'] %} <|im_start|>user {{ content_str }}<|im_end|> {%- elif message.role in ['assistant', 'gpt', 'function_call'] %} <|im_start|>assistant {%- if reasoning_content is string %} {{ "\n" ~ reasoning_content ~ "" }} {%- endif %} {{ content_str }} {%- if message.tool_calls %} {%- for tool_call in message.tool_calls %} {%- if tool_call.function is defined %} {%- set tool_call = tool_call.function %} {%- endif %} {"name": "{{ tool_call.name }}", "arguments": {{ tool_call.arguments if tool_call.arguments is string else (tool_call.arguments | tojson) }}} {%- endfor %} {%- endif %} <|im_end|> {%- elif message.role == 'tool' %} <|im_start|>user {{ content_str }} <|im_end|> {%- endif %} {%- endfor %} {# ================= GENERATION PROMPT ================= #} {%- if add_generation_prompt %} <|im_start|>assistant {%- if enable_thinking is defined and enable_thinking is false %} {{- "\n" }} {%- endif %} {%- endif %}