{{- bos_token if bos_token is defined else '' }} {%- if custom_tools is defined and custom_tools is not none %} {%- set tools = custom_tools %} {%- endif %} {%- if tools is not defined %} {%- set tools = none %} {%- endif %} {%- if tools is not none and tools is iterable and tools is not mapping and tools|length == 0 %} {%- set tools = none %} {%- endif %} {%- if tools_in_user_message is not defined %} {#- Llama 3.1-style tool prompting is most stable when tools are injected into the first user turn. #} {%- set tools_in_user_message = true %} {%- endif %} {%- if date_string is not defined %} {%- if strftime_now is defined %} {%- set date_string = strftime_now("%d %b %Y") %} {%- else %} {#- Avoid baking a stale Today Date into newer data. Pass date_string explicitly if needed. #} {%- set date_string = none %} {%- endif %} {%- endif %} {%- if cutoff_date_string is not defined %} {%- set cutoff_date_string = "December 2023" %} {%- endif %} {%- if image_token is not defined %} {%- set image_token = "<|image|>" %} {%- endif %} {%- if video_token is not defined %} {%- set video_token = "<|video|>" %} {%- endif %} {#- Modern content renderer inspired by Qwen-style templates. Supports: - string content - None / missing content - list content blocks: {type: text, text: ...}, {type: image/image_url/input_image, ...}, {type: video/input_video, ...} - direct {text: ...}, {image: ...}, {image_url: ...}, {video: ...} #} {%- set image_count = namespace(value=0) %} {%- set video_count = namespace(value=0) %} {%- macro render_content_item(item, do_vision_count, is_system_content=false) %} {%- if item is string %} {{- item }} {%- elif item is mapping %} {%- set item_type = item.get('type', '') %} {%- if item_type in ['image', 'image_url', 'input_image'] or 'image' in item or 'image_url' in item %} {%- if is_system_content %} {{- raise_exception('System message cannot contain images.') }} {%- endif %} {%- if do_vision_count %} {%- set image_count.value = image_count.value + 1 %} {%- endif %} {%- if add_vision_id is defined and add_vision_id %} {{- 'Picture ' ~ image_count.value ~ ': ' }} {%- endif %} {{- image_token }} {%- elif item_type in ['video', 'input_video'] or 'video' in item %} {%- if is_system_content %} {{- raise_exception('System message cannot contain videos.') }} {%- endif %} {%- if do_vision_count %} {%- set video_count.value = video_count.value + 1 %} {%- endif %} {%- if add_vision_id is defined and add_vision_id %} {{- 'Video ' ~ video_count.value ~ ': ' }} {%- endif %} {{- video_token }} {%- elif 'text' in item %} {{- item.text }} {%- elif item_type in ['text', 'input_text'] and 'content' in item %} {{- item.content }} {%- elif 'content' in item %} {{- render_content(item.content, do_vision_count, is_system_content) }} {%- else %} {{- raise_exception('Unexpected item type in content.') }} {%- endif %} {%- elif item is none or item is undefined %} {{- '' }} {%- else %} {{- raise_exception('Unexpected item type in content.') }} {%- endif %} {%- endmacro %} {%- macro render_content(content, do_vision_count, is_system_content=false) %} {%- if content is string %} {{- content }} {%- elif content is none or content is undefined %} {{- '' }} {%- elif content is mapping %} {{- render_content_item(content, do_vision_count, is_system_content) }} {%- elif content is iterable %} {%- for item in content %} {{- render_content_item(item, do_vision_count, is_system_content) }} {%- endfor %} {%- else %} {{- raise_exception('Unexpected content type.') }} {%- endif %} {%- endmacro %} {%- macro render_tool_call_json(tool_call) %} {%- if tool_call.function is defined %} {%- set fn = tool_call.function %} {%- else %} {%- set fn = tool_call %} {%- endif %} {%- if fn.name is not defined %} {{- raise_exception('Tool call is missing function name.') }} {%- endif %} {%- set args = fn.arguments if fn.arguments is defined and fn.arguments is not none else {} %} {{- '{"name": ' }}{{- fn.name | tojson }}{{- ', "parameters": ' }} {%- if args is string and args|trim %} {{- args }} {%- else %} {{- args | tojson }} {%- endif %} {{- '}' }} {%- endmacro %} {%- macro render_tool_calls_json(tool_calls) %} {%- if tool_calls|length == 1 %} {{- render_tool_call_json(tool_calls[0]) }} {%- else %} {{- '[' }} {%- for tool_call in tool_calls %} {{- render_tool_call_json(tool_call) }} {%- if not loop.last %} {{- ', ' }} {%- endif %} {%- endfor %} {{- ']' }} {%- endif %} {%- endmacro %} {%- if not messages %} {{- raise_exception('No messages provided.') }} {%- endif %} {#- Extract an initial system/developer message, so we can slot it into the Llama system header. #} {%- if messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' %} {%- set system_message = render_content(messages[0]['content'], false, true)|trim %} {%- set messages = messages[1:] %} {%- else %} {%- if tools is not none %} {%- set system_message = "You are a helpful assistant with tool calling capabilities. Only reply with a tool call if the function exists in the library provided by the user. If it doesn't exist, just reply directly in natural language. When you receive a tool call response, use the output to format an answer to the original user question." %} {%- else %} {%- set system_message = "" %} {%- endif %} {%- endif %} {#- System message #} {{- "<|start_header_id|>system<|end_header_id|>\n\n" }} {%- if tools is not none %} {{- "Environment: ipython\n" }} {%- endif %} {%- if cutoff_date_string %} {{- "Cutting Knowledge Date: " + cutoff_date_string + "\n" }} {%- endif %} {%- if date_string %} {{- "Today Date: " + date_string + "\n" }} {%- endif %} {%- if tools is not none or cutoff_date_string or date_string %} {{- "\n" }} {%- endif %} {%- if tools is not none and not tools_in_user_message %} {{- "You have access to the following functions. To call a function, please respond with JSON for a function call. " }} {{- 'For a single function call, respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }} {{- 'If multiple function calls are required, respond with a JSON array of objects in that same format. ' }} {{- "Do not use variables.\n\n" }} {%- for t in tools %} {{- t | tojson(indent=4) }} {{- "\n\n" }} {%- endfor %} {%- endif %} {{- system_message }} {{- "<|eot_id|>" }} {#- Custom tools can be passed in the first user message with extra guidance. #} {%- if tools_in_user_message and tools is not none %} {%- if messages | length != 0 %} {%- set first_user_message = render_content(messages[0]['content'], true)|trim %} {%- set messages = messages[1:] %} {%- else %} {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} {%- endif %} {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}} {{- "Given the following functions, please respond with JSON for a function call " }} {{- "with its proper arguments that best answers the given prompt.\n\n" }} {{- 'For a single function call, respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. ' }} {{- 'If multiple function calls are required, respond with a JSON array of objects in that same format. ' }} {{- "Do not use variables.\n\n" }} {%- for t in tools %} {{- t | tojson(indent=4) }} {{- "\n\n" }} {%- endfor %} {{- first_user_message + "<|eot_id|>" }} {%- endif %} {%- for message in messages %} {%- set role = message.role %} {%- if role == 'system' or role == 'developer' %} {{- '<|start_header_id|>system<|end_header_id|>\n\n' }} {{- render_content(message.content, true, true)|trim }} {{- '<|eot_id|>' }} {%- elif role == 'user' %} {{- '<|start_header_id|>user<|end_header_id|>\n\n' }} {{- render_content(message.content, true)|trim }} {{- '<|eot_id|>' }} {%- elif role == 'assistant' %} {%- set content = render_content(message.content, true)|trim %} {%- set reasoning_content = '' %} {%- if message.reasoning_content is defined and message.reasoning_content is string %} {%- set reasoning_content = message.reasoning_content|trim %} {%- elif '' in content %} {%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n')|trim %} {%- set content = content.split('')[-1].lstrip('\n')|trim %} {%- endif %} {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }} {%- if reasoning_content %} {{- '\n' + reasoning_content + '\n\n\n' }} {%- endif %} {{- content }} {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %} {%- if content or reasoning_content %} {{- '\n\n' }} {%- endif %} {{- render_tool_calls_json(message.tool_calls) }} {%- endif %} {{- '<|eot_id|>' }} {%- elif role == 'tool' or role == 'ipython' or role == 'function' %} {{- '<|start_header_id|>ipython<|end_header_id|>\n\n' }} {%- if message.content is mapping %} {{- {"output": message.content} | tojson }} {%- else %} {%- set tool_content = render_content(message.content, true)|trim %} {{- {"output": tool_content} | tojson }} {%- endif %} {{- '<|eot_id|>' }} {%- else %} {{- raise_exception('Unexpected message role: ' + role) }} {%- endif %} {%- endfor %} {%- if add_generation_prompt is defined and add_generation_prompt %} {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }} {%- if enable_thinking is defined and enable_thinking %} {{- '\n' }} {%- endif %} {%- endif %}