{%- set ns = namespace( has_system=false, has_images=false, has_tools=false, message_count=0, image_count=0, tool_call_count=0 ) -%} {%- if messages[0]['role'] == 'system' -%} {%- set system_message = messages[0]['content'] -%} {%- set loop_messages = messages[1:] -%} {%- set ns.has_system = true -%} {%- else -%} {%- set system_message = 'You are Helion, an advanced AI assistant with the following capabilities: 1. Vision Understanding: You can analyze images, charts, diagrams, documents, and screenshots. Use the token to reference visual inputs. 2. Tool Use: You can call functions and use tools by responding with valid JSON in the format: {"tool": "tool_name", "parameters": {...}} 3. Long Context: You can process and remember up to 200,000 tokens of conversation history. 4. Reasoning: You excel at step-by-step logical reasoning, mathematical problem-solving, and code generation. 5. Multilingual: You can communicate in 12+ languages with strong performance. Guidelines: - Always be helpful, respectful, and honest - Provide accurate, well-reasoned responses - Cite sources when referencing external information - Admit uncertainty when appropriate - Refuse harmful or inappropriate requests - Use tools when they would be helpful - Break down complex problems into steps - Consider multiple perspectives' -%} {%- set loop_messages = messages -%} {%- endif -%} {{- bos_token -}} {%- if system_message and system_message|length > 0 -%} <|system|> {{- system_message }} {%- endif -%} {%- for message in loop_messages -%} {%- set ns.message_count = ns.message_count + 1 -%} {%- if message['role'] == 'user' -%} <|user|> {%- if message.get('images') -%} {%- set ns.has_images = true -%} {%- if message['images'] is iterable and message['images'] is not string -%} {%- for image in message['images'] -%} {%- set ns.image_count = ns.image_count + 1 -%} {%- endfor -%} {%- else -%} {%- set ns.image_count = ns.image_count + 1 -%} {%- endif -%} {%- endif -%} {%- if message.get('documents') -%} {%- for doc in message['documents'] -%} {{ doc.get('content', '') }} {%- endfor -%} {%- endif -%} {%- if message.get('context') -%} {{ message['context'] }} {%- endif -%} {{- message['content'] -}} {%- if message.get('constraints') -%} {{ message['constraints'] }} {%- endif -%} {%- elif message['role'] == 'assistant' -%} <|assistant|> {%- if message.get('reasoning') -%} {{ message['reasoning'] }} {%- endif -%} {%- if message.get('tool_calls') -%} {%- set ns.has_tools = true -%} {%- for tool_call in message['tool_calls'] -%} {%- set ns.tool_call_count = ns.tool_call_count + 1 -%} {"tool": "{{ tool_call.get('name', tool_call.get('tool', 'unknown')) }}", "parameters": {{ tool_call.get('parameters', tool_call.get('arguments', '{}')) | tojson }}} {%- endfor -%} {%- endif -%} {{- message['content'] -}} {%- if message.get('code_execution') -%} {{ message['code_execution'].get('code', '') }} {%- endif -%} {%- if message.get('citations') -%} {%- for citation in message['citations'] -%} - [{{ loop.index }}] {{ citation.get('source', 'Unknown source') }}{% if citation.get('url') %}: {{ citation['url'] }}{% endif %} {%- endfor -%} {%- endif -%} {%- elif message['role'] == 'tool' -%} <|tool|> {%- if message.get('tool_call_id') -%} {%- else -%} {%- endif -%} {%- if message.get('result') -%} {{ message['result'] | tojson if message['result'] is mapping else message['result'] }} {%- else -%} {{ message['content'] }} {%- endif -%} {%- if message.get('error') -%} {{ message['error'] }} {%- endif -%} {%- elif message['role'] == 'function' -%} <|function|> {{ message['content'] }} {%- elif message['role'] == 'thought' -%} <|thought|> {{ message['content'] }} {%- elif message['role'] == 'observation' -%} <|observation|> {{ message['content'] }} {%- else -%} <|{{ message['role'] }}|> {{ message['content'] }} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {%- if ns.has_tools -%} <|assistant|> {%- else -%} <|assistant|> {%- endif -%} {%- endif -%}