Diff
checker
텍스트
텍스트
이미지
문서
Excel
폴더
Legal
Enterprise
데스크톱
요금제
로그인
데스크톱 앱 다운로드
텍스트 비교
두 텍스트 파일의 차이점을 찾아보세요
도구
기록
실시간 편집
공백 변경 숨기기
변경 없는 행 숨기기
줄바꿈 비활성화
레이아웃
나란히 보기
합쳐 보기
비교 단위
스마트
단어
글자
텍스트 스타일
모양 변경
구문 강조
언어 선택
제외
텍스트 변환
첫 변경으로
수정
Diffchecker Desktop
가장 안전하게 Diffchecker를 사용하는 방법. 데스크톱 앱을 사용하면 비교 데이터가 외부로 전송되지 않습니다!
데스크톱 앱 받기
Gemma 4 preserve thinking fix
생성일
그저께
비교 결과 만료 없음
초기화
내보내기
공유
설명
4 삭제
행
총
삭제
글자
총
삭제
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
390 행
복사
3 추가
행
총
추가
글자
총
추가
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
391 행
복사
{#
{#
Template: Google Gemma 4 Canonical Chat Template
Template: Google Gemma 4 Canonical Chat Template
Author: Google Gemma Engineering Team
Author: Google Gemma Engineering Team
Published: 2026-07-09
Published: 2026-07-09
Context: Fixed tool-calling loops, turn closures, and thinking content-ordering.
Context: Fixed tool-calling loops, turn closures, and thinking content-ordering.
#}
#}
{%- macro format_parameters(properties, required, filter_keys=false) -%}
{%- macro format_parameters(properties, required, filter_keys=false) -%}
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
{%- set ns = namespace(found_first=false) -%}
{%- set ns = namespace(found_first=false) -%}
{%- for key, value in properties | dictsort -%}
{%- for key, value in properties | dictsort -%}
{%- set add_comma = false -%}
{%- set add_comma = false -%}
{%- if not filter_keys or key not in standard_keys -%}
{%- if not filter_keys or key not in standard_keys -%}
{%- if ns.found_first %},{% endif -%}
{%- if ns.found_first %},{% endif -%}
{%- set ns.found_first = true -%}
{%- set ns.found_first = true -%}
{{ key }}:{
{{ key }}:{
{%- if value['description'] -%}
{%- if value['description'] -%}
description:<|"|>{{ value['description'] }}<|"|>
description:<|"|>{{ value['description'] }}<|"|>
{%- set add_comma = true -%}
{%- set add_comma = true -%}
{%- endif -%}
{%- endif -%}
{%- if value['type'] | upper == 'STRING' -%}
{%- if value['type'] | upper == 'STRING' -%}
{%- if value['enum'] -%}
{%- if value['enum'] -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
enum:{{ format_argument(value['enum']) }}
enum:{{ format_argument(value['enum']) }}
{%- endif -%}
{%- endif -%}
{%- elif value['type'] | upper == 'ARRAY' -%}
{%- elif value['type'] | upper == 'ARRAY' -%}
{%- if value['items'] is mapping and value['items'] -%}
{%- if value['items'] is mapping and value['items'] -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
items:{
items:{
{%- set ns_items = namespace(found_first=false) -%}
{%- set ns_items = namespace(found_first=false) -%}
{%- for item_key, item_value in value['items'] | dictsort -%}
{%- for item_key, item_value in value['items'] | dictsort -%}
{%- if item_value is not none -%}
{%- if item_value is not none -%}
{%- if ns_items.found_first %},{% endif -%}
{%- if ns_items.found_first %},{% endif -%}
{%- set ns_items.found_first = true -%}
{%- set ns_items.found_first = true -%}
{%- if item_key == 'properties' -%}
{%- if item_key == 'properties' -%}
properties:{
properties:{
{%- if item_value is mapping -%}
{%- if item_value is mapping -%}
{{- format_parameters(item_value, value['items']['required'] | default([])) -}}
{{- format_parameters(item_value, value['items']['required'] | default([])) -}}
{%- endif -%}
{%- endif -%}
}
}
{%- elif item_key == 'required' -%}
{%- elif item_key == 'required' -%}
required:[
required:[
{%- for req_item in item_value -%}
{%- for req_item in item_value -%}
<|"|>{{- req_item -}}<|"|>
<|"|>{{- req_item -}}<|"|>
{%- if not loop.last %},{% endif -%}
{%- if not loop.last %},{% endif -%}
{%- endfor -%}
{%- endfor -%}
]
]
{%- elif item_key == 'type' -%}
{%- elif item_key == 'type' -%}
{%- if item_value is string -%}
{%- if item_value is string -%}
type:{{ format_argument(item_value | upper) }}
type:{{ format_argument(item_value | upper) }}
{%- else -%}
{%- else -%}
type:{{ format_argument(item_value | map('upper') | list) }}
type:{{ format_argument(item_value | map('upper') | list) }}
{%- endif -%}
{%- endif -%}
{%- else -%}
{%- else -%}
{{ item_key }}:{{ format_argument(item_value) }}
{{ item_key }}:{{ format_argument(item_value) }}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
}
}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if value['nullable'] %}
{%- if value['nullable'] %}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
nullable:true
nullable:true
{%- endif -%}
{%- endif -%}
{%- if value['type'] | upper == 'OBJECT' -%}
{%- if value['type'] | upper == 'OBJECT' -%}
{%- if value['properties'] is defined and value['properties'] is mapping -%}
{%- if value['properties'] is defined and value['properties'] is mapping -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
properties:{
properties:{
{{- format_parameters(value['properties'], value['required'] | default([])) -}}
{{- format_parameters(value['properties'], value['required'] | default([])) -}}
}
}
{%- elif value is mapping -%}
{%- elif value is mapping -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
properties:{
properties:{
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
}
}
{%- endif -%}
{%- endif -%}
{%- if value['required'] -%}
{%- if value['required'] -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
required:[
required:[
{%- for item in value['required'] | default([]) -%}
{%- for item in value['required'] | default([]) -%}
<|"|>{{- item -}}<|"|>
<|"|>{{- item -}}<|"|>
{%- if not loop.last %},{% endif -%}
{%- if not loop.last %},{% endif -%}
{%- endfor -%}
{%- endfor -%}
]
]
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
type:<|"|>{{ value['type'] | upper }}<|"|>}
type:<|"|>{{ value['type'] | upper }}<|"|>}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endmacro -%}
{%- endmacro -%}
{%- macro format_function_declaration(tool_data) -%}
{%- macro format_function_declaration(tool_data) -%}
declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
{%- set params = tool_data['function']['parameters'] -%}
{%- set params = tool_data['function']['parameters'] -%}
{%- if params -%}
{%- if params -%}
,parameters:{
,parameters:{
{%- if params['properties'] -%}
{%- if params['properties'] -%}
properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
{%- endif -%}
{%- endif -%}
{%- if params['required'] -%}
{%- if params['required'] -%}
required:[
required:[
{%- for item in params['required'] -%}
{%- for item in params['required'] -%}
<|"|>{{- item -}}<|"|>
<|"|>{{- item -}}<|"|>
{{- ',' if not loop.last -}}
{{- ',' if not loop.last -}}
{%- endfor -%}
{%- endfor -%}
],
],
{%- endif -%}
{%- endif -%}
{%- if params['type'] -%}
{%- if params['type'] -%}
type:<|"|>{{- params['type'] | upper -}}<|"|>}
type:<|"|>{{- params['type'] | upper -}}<|"|>}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if 'response' in tool_data['function'] -%}
{%- if 'response' in tool_data['function'] -%}
{%- set response_declaration = tool_data['function']['response'] -%}
{%- set response_declaration = tool_data['function']['response'] -%}
,response:{
,response:{
{%- if response_declaration['description'] -%}
{%- if response_declaration['description'] -%}
description:<|"|>{{- response_declaration['description'] -}}<|"|>,
description:<|"|>{{- response_declaration['description'] -}}<|"|>,
{%- endif -%}
{%- endif -%}
{%- if response_declaration['type'] | upper == 'OBJECT' -%}
{%- if response_declaration['type'] | upper == 'OBJECT' -%}
type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
}
}
{%- endmacro -%}
{%- endmacro -%}
{%- macro format_argument(argument, escape_keys=True) -%}
{%- macro format_argument(argument, escape_keys=True) -%}
{%- if argument is none -%}
{%- if argument is none -%}
{{- 'null' -}}
{{- 'null' -}}
{%- elif argument is string -%}
{%- elif argument is string -%}
{{- '<|"|>' + argument + '<|"|>' -}}
{{- '<|"|>' + argument + '<|"|>' -}}
{%- elif argument is boolean -%}
{%- elif argument is boolean -%}
{{- 'true' if argument else 'false' -}}
{{- 'true' if argument else 'false' -}}
{%- elif argument is mapping -%}
{%- elif argument is mapping -%}
{{- '{' -}}
{{- '{' -}}
{%- set ns = namespace(found_first=false) -%}
{%- set ns = namespace(found_first=false) -%}
{%- for key, value in argument | dictsort -%}
{%- for key, value in argument | dictsort -%}
{%- if ns.found_first %},{% endif -%}
{%- if ns.found_first %},{% endif -%}
{%- set ns.found_first = true -%}
{%- set ns.found_first = true -%}
{%- if escape_keys -%}
{%- if escape_keys -%}
{{- '<|"|>' + key + '<|"|>' -}}
{{- '<|"|>' + key + '<|"|>' -}}
{%- else -%}
{%- else -%}
{{- key -}}
{{- key -}}
{%- endif -%}
{%- endif -%}
:{{- format_argument(value, escape_keys=escape_keys) -}}
:{{- format_argument(value, escape_keys=escape_keys) -}}
{%- endfor -%}
{%- endfor -%}
{{- '}' -}}
{{- '}' -}}
{%- elif argument is sequence -%}
{%- elif argument is sequence -%}
{{- '[' -}}
{{- '[' -}}
{%- for item in argument -%}
{%- for item in argument -%}
{{- format_argument(item, escape_keys=escape_keys) -}}
{{- format_argument(item, escape_keys=escape_keys) -}}
{%- if not loop.last %},{% endif -%}
{%- if not loop.last %},{% endif -%}
{%- endfor -%}
{%- endfor -%}
{{- ']' -}}
{{- ']' -}}
{%- else -%}
{%- else -%}
{{- argument -}}
{{- argument -}}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{%- endmacro -%}
{%- macro strip_thinking(text) -%}
{%- macro strip_thinking(text) -%}
{%- set ns = namespace(result='') -%}
{%- set ns = namespace(result='') -%}
{%- for part in text.split('<channel|>') -%}
{%- for part in text.split('<channel|>') -%}
{%- if '<|channel>' in part -%}
{%- if '<|channel>' in part -%}
{%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
{%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
{%- else -%}
{%- else -%}
{%- set ns.result = ns.result + part -%}
{%- set ns.result = ns.result + part -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{{- ns.result | trim -}}
{{- ns.result | trim -}}
{%- endmacro -%}
{%- endmacro -%}
{%- macro format_tool_response_block(tool_name, response) -%}
{%- macro format_tool_response_block(tool_name, response) -%}
{{- '<|tool_response>' -}}
{{- '<|tool_response>' -}}
{%- if response is mapping -%}
{%- if response is mapping -%}
{{- 'response:' + tool_name + '{' -}}
{{- 'response:' + tool_name + '{' -}}
{%- for key, value in response | dictsort -%}
{%- for key, value in response | dictsort -%}
{{- key -}}:{{- format_argument(value, escape_keys=False) -}}
{{- key -}}:{{- format_argument(value, escape_keys=False) -}}
{%- if not loop.last %},{% endif -%}
{%- if not loop.last %},{% endif -%}
{%- endfor -%}
{%- endfor -%}
{{- '}' -}}
{{- '}' -}}
{%- else -%}
{%- else -%}
{{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
{{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
{%- endif -%}
{%- endif -%}
{{- '<tool_response|>' -}}
{{- '<tool_response|>' -}}
{%- endmacro -%}
{%- endmacro -%}
{#- ===== SETUP ===== -#}
{#- ===== SETUP ===== -#}
{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
{%- set loop_messages = messages -%}
{%- set loop_messages = messages -%}
{%- set enable_thinking = enable_thinking | default(false) -%}
{%- set enable_thinking = enable_thinking | default(false) -%}
복사
복사됨
복사
복사됨
{%- set preserve_thinking = preserve_thinking
| default(fa
lse
)
-%}
{%- set preserve_thinking = preserve_thinking
if preserve_thinking is defined e
lse
true
-%}
{{- bos_token -}}
{{- bos_token -}}
{#- Handle System/Tool Definitions Block -#}
{#- Handle System/Tool Definitions Block -#}
{%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%}
{%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%}
{{- '<|turn>system\n' -}}
{{- '<|turn>system\n' -}}
{#- Inject Thinking token at the very top of the FIRST system turn -#}
{#- Inject Thinking token at the very top of the FIRST system turn -#}
{%- if enable_thinking -%}
{%- if enable_thinking -%}
{{- '<|think|>\n' -}}
{{- '<|think|>\n' -}}
{%- set ns.prev_message_type = 'think' -%}
{%- set ns.prev_message_type = 'think' -%}
{%- endif -%}
{%- endif -%}
{%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
{%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
{%- if messages[0]['content'] is string -%}
{%- if messages[0]['content'] is string -%}
{{- messages[0]['content'] | trim -}}
{{- messages[0]['content'] | trim -}}
{%- elif messages[0]['content'] is sequence -%}
{%- elif messages[0]['content'] is sequence -%}
{%- for item in messages[0]['content'] -%}
{%- for item in messages[0]['content'] -%}
{{- item['text'] | trim + ' '-}}
{{- item['text'] | trim + ' '-}}
{%- endfor -%}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- set loop_messages = messages[1:] -%}
{%- set loop_messages = messages[1:] -%}
{%- endif -%}
{%- endif -%}
{%- if tools -%}
{%- if tools -%}
{%- for tool in tools %}
{%- for tool in tools %}
{{- '<|tool>' -}}
{{- '<|tool>' -}}
{{- format_function_declaration(tool) | trim -}}
{{- format_function_declaration(tool) | trim -}}
{{- '<tool|>' -}}
{{- '<tool|>' -}}
{%- endfor %}
{%- endfor %}
{%- set ns.prev_message_type = 'tool' -%}
{%- set ns.prev_message_type = 'tool' -%}
{%- endif -%}
{%- endif -%}
{{- '<turn|>\n' -}}
{{- '<turn|>\n' -}}
{%- endif %}
{%- endif %}
{#- Pre-scan: find last user message index for reasoning guard -#}
{#- Pre-scan: find last user message index for reasoning guard -#}
{%- set ns_turn = namespace(last_user_idx=-1) -%}
{%- set ns_turn = namespace(last_user_idx=-1) -%}
{%- for i in range(loop_messages | length) -%}
{%- for i in range(loop_messages | length) -%}
{%- if loop_messages[i]['role'] == 'user' -%}
{%- if loop_messages[i]['role'] == 'user' -%}
{%- set ns_turn.last_user_idx = i -%}
{%- set ns_turn.last_user_idx = i -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{#- Loop through messages -#}
{#- Loop through messages -#}
{%- for message in loop_messages -%}
{%- for message in loop_messages -%}
{%- if message['role'] != 'tool' -%}
{%- if message['role'] != 'tool' -%}
{%- set ns.prev_message_type = None -%}
{%- set ns.prev_message_type = None -%}
{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
{#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}
{#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}
{%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
{%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
{%- if not continue_same_model_turn -%}
{%- if not continue_same_model_turn -%}
{{- '<|turn>' + role + '\n' }}
{{- '<|turn>' + role + '\n' }}
{%- endif -%}
{%- endif -%}
{#- Render reasoning/reasoning_content as thinking channel -#}
{#- Render reasoning/reasoning_content as thinking channel -#}
{%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
{%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
복사
복사됨
복사
복사됨
{%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or
(
preserve_thinking
and message.get('tool_calls'))
-%}
{%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or
preserve_thinking
-%}
{%- if thinking_text and thinking_gate -%}
{%- if thinking_text and thinking_gate -%}
{{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
{{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
{%- endif -%}
{%- endif -%}
{%- if message.get('tool_calls') -%}
{%- if message.get('tool_calls') -%}
{%- for tool_call in message.get('tool_calls') -%}
{%- for tool_call in message.get('tool_calls') -%}
{%- set function = tool_call['function'] -%}
{%- set function = tool_call['function'] -%}
{{- '<|tool_call>call:' + function['name'] + '{' -}}
{{- '<|tool_call>call:' + function['name'] + '{' -}}
{%- if function['arguments'] is mapping -%}
{%- if function['arguments'] is mapping -%}
{%- set ns_args = namespace(found_first=false) -%}
{%- set ns_args = namespace(found_first=false) -%}
{%- for key, value in function['arguments'] | dictsort -%}
{%- for key, value in function['arguments'] | dictsort -%}
{%- if ns_args.found_first %},{% endif -%}
{%- if ns_args.found_first %},{% endif -%}
{%- set ns_args.found_first = true -%}
{%- set ns_args.found_first = true -%}
{{- key -}}:{{- format_argument(value, escape_keys=False) -}}
{{- key -}}:{{- format_argument(value, escape_keys=False) -}}
{%- endfor -%}
{%- endfor -%}
{%- elif function['arguments'] is none -%}
{%- elif function['arguments'] is none -%}
{%- else -%}
{%- else -%}
{{- raise_exception(
{{- raise_exception(
"chat_template: tool_calls[].function.arguments must be a "
"chat_template: tool_calls[].function.arguments must be a "
"JSON object (mapping), not a string. Deserialize arguments "
"JSON object (mapping), not a string. Deserialize arguments "
"before passing to the template."
"before passing to the template."
) -}}
) -}}
{%- endif -%}
{%- endif -%}
{{- '}<tool_call|>' -}}
{{- '}<tool_call|>' -}}
{%- endfor -%}
{%- endfor -%}
{%- set ns.prev_message_type = 'tool_call' -%}
{%- set ns.prev_message_type = 'tool_call' -%}
{%- endif -%}
{%- endif -%}
{%- set ns_tr_out = namespace(flag=false) -%}
{%- set ns_tr_out = namespace(flag=false) -%}
{%- if message.get('tool_responses') -%}
{%- if message.get('tool_responses') -%}
{#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
{#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
{%- for tool_response in message.get('tool_responses') -%}
{%- for tool_response in message.get('tool_responses') -%}
{{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
{{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
{%- set ns_tr_out.flag = true -%}
{%- set ns_tr_out.flag = true -%}
{%- set ns.prev_message_type = 'tool_response' -%}
{%- set ns.prev_message_type = 'tool_response' -%}
{%- endfor -%}
{%- endfor -%}
{%- elif message.get('tool_calls') -%}
{%- elif message.get('tool_calls') -%}
{#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
{#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
{%- set ns_tool_scan = namespace(stopped=false) -%}
{%- set ns_tool_scan = namespace(stopped=false) -%}
{%- for k in range(loop.index0 + 1, loop_messages | length) -%}
{%- for k in range(loop.index0 + 1, loop_messages | length) -%}
{%- if ns_tool_scan.stopped -%}
{%- if ns_tool_scan.stopped -%}
{%- elif loop_messages[k]['role'] != 'tool' -%}
{%- elif loop_messages[k]['role'] != 'tool' -%}
{%- set ns_tool_scan.stopped = true -%}
{%- set ns_tool_scan.stopped = true -%}
{%- else -%}
{%- else -%}
{%- set follow = loop_messages[k] -%}
{%- set follow = loop_messages[k] -%}
{#- Resolve tool_call_id to function name -#}
{#- Resolve tool_call_id to function name -#}
{%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
{%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
{%- for tc in message.get('tool_calls') -%}
{%- for tc in message.get('tool_calls') -%}
{%- if tc.get('id') == follow.get('tool_call_id') -%}
{%- if tc.get('id') == follow.get('tool_call_id') -%}
{%- set ns_tname.name = tc['function']['name'] -%}
{%- set ns_tname.name = tc['function']['name'] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{#- Handle content as string or content-parts array -#}
{#- Handle content as string or content-parts array -#}
{%- set tool_body = follow.get('content') -%}
{%- set tool_body = follow.get('content') -%}
{%- if tool_body is string -%}
{%- if tool_body is string -%}
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
{%- elif tool_body is sequence and tool_body is not string -%}
{%- elif tool_body is sequence and tool_body is not string -%}
{%- set ns_txt = namespace(s='') -%}
{%- set ns_txt = namespace(s='') -%}
{%- for part in tool_body -%}
{%- for part in tool_body -%}
{%- if part.get('type') == 'text' -%}
{%- if part.get('type') == 'text' -%}
{%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
{%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
{%- for part in tool_body -%}
{%- for part in tool_body -%}
{%- if part.get('type') in ['image', 'image_url'] -%}
{%- if part.get('type') in ['image', 'image_url'] -%}
{{- '<|image|>' -}}
{{- '<|image|>' -}}
{%- elif part.get('type') in ['audio', 'input_audio'] -%}
{%- elif part.get('type') in ['audio', 'input_audio'] -%}
{{- '<|audio|>' -}}
{{- '<|audio|>' -}}
{%- elif part.get('type') == 'video' -%}
{%- elif part.get('type') == 'video' -%}
{{- '<|video|>' -}}
{{- '<|video|>' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- else -%}
{%- else -%}
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
{%- endif -%}
{%- endif -%}
{%- set ns_tr_out.flag = true -%}
{%- set ns_tr_out.flag = true -%}
{%- set ns.prev_message_type = 'tool_response' -%}
{%- set ns.prev_message_type = 'tool_response' -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- set captured_content -%}
{%- set captured_content -%}
{%- if message.get('content') is string -%}
{%- if message.get('content') is string -%}
{%- if role == 'model' -%}
{%- if role == 'model' -%}
{{- strip_thinking(message['content']) -}}
{{- strip_thinking(message['content']) -}}
{%- else -%}
{%- else -%}
{{- message['content'] | trim -}}
{{- message['content'] | trim -}}
{%- endif -%}
{%- endif -%}
{%- elif message.get('content') is sequence -%}
{%- elif message.get('content') is sequence -%}
{%- for item in message['content'] -%}
{%- for item in message['content'] -%}
{%- if item.get('type') == 'text' -%}
{%- if item.get('type') == 'text' -%}
{%- if role == 'model' -%}
{%- if role == 'model' -%}
{{- strip_thinking(item['text']) -}}
{{- strip_thinking(item['text']) -}}
{%- else -%}
{%- else -%}
{{- item['text'] | trim -}}
{{- item['text'] | trim -}}
{%- endif -%}
{%- endif -%}
{%- elif item.get('type') in ['image', 'image_url'] -%}
{%- elif item.get('type') in ['image', 'image_url'] -%}
{{- '<|image|>' -}}
{{- '<|image|>' -}}
{%- elif item.get('type') in ['audio', 'input_audio'] -%}
{%- elif item.get('type') in ['audio', 'input_audio'] -%}
{{- '<|audio|>' -}}
{{- '<|audio|>' -}}
{%- elif item.get('type') == 'video' -%}
{%- elif item.get('type') == 'video' -%}
{{- '<|video|>' -}}
{{- '<|video|>' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endset -%}
{%- endset -%}
{{- captured_content -}}
{{- captured_content -}}
{%- set has_content = captured_content | trim | length > 0 -%}
{%- set has_content = captured_content | trim | length > 0 -%}
{#- Forward-scan: find next non-tool message role for continuation detection -#}
{#- Forward-scan: find next non-tool message role for continuation detection -#}
{%- set next_nt = namespace(role=None, found=false) -%}
{%- set next_nt = namespace(role=None, found=false) -%}
{%- for j in range(loop.index0 + 1, loop_messages | length) -%}
{%- for j in range(loop.index0 + 1, loop_messages | length) -%}
{%- if not next_nt.found -%}
{%- if not next_nt.found -%}
{%- if loop_messages[j]['role'] != 'tool' -%}
{%- if loop_messages[j]['role'] != 'tool' -%}
{%- set next_nt.role = loop_messages[j]['role'] -%}
{%- set next_nt.role = loop_messages[j]['role'] -%}
{%- set next_nt.found = true -%}
{%- set next_nt.found = true -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- set continues_into_next = (
{%- set continues_into_next = (
role == 'model'
role == 'model'
and next_nt.role == 'assistant'
and next_nt.role == 'assistant'
and (not message.get('tool_calls') or ns_tr_out.flag)
and (not message.get('tool_calls') or ns_tr_out.flag)
) -%}
) -%}
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
{{- '<|tool_response>' -}}
{{- '<|tool_response>' -}}
{%- elif continues_into_next -%}
{%- elif continues_into_next -%}
{%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
{%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
{{- '<turn|>\n' -}}
{{- '<turn|>\n' -}}
{%- endif -%}
{%- endif -%}
{#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
{#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
{%- set ns.prev_non_tool_role = message['role'] -%}
{%- set ns.prev_non_tool_role = message['role'] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{%- if add_generation_prompt -%}
{%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
{%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
{{- '<|turn>model\n' -}}
{{- '<|turn>model\n' -}}
{%- if not enable_thinking -%}
{%- if not enable_thinking -%}
{{- '<|channel>thought\n<channel|>' -}}
{{- '<|channel>thought\n<channel|>' -}}
{%- endif -%}
{%- endif -%}
{%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
{%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
{{- '<|channel>thought\n' -}}
{{- '<|channel>thought\n' -}}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
복사
복사됨
복사
복사됨
저장된 비교 결과
원본
파일 열기
{# Template: Google Gemma 4 Canonical Chat Template Author: Google Gemma Engineering Team Published: 2026-07-09 Context: Fixed tool-calling loops, turn closures, and thinking content-ordering. #} {%- macro format_parameters(properties, required, filter_keys=false) -%} {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%} {%- set ns = namespace(found_first=false) -%} {%- for key, value in properties | dictsort -%} {%- set add_comma = false -%} {%- if not filter_keys or key not in standard_keys -%} {%- if ns.found_first %},{% endif -%} {%- set ns.found_first = true -%} {{ key }}:{ {%- if value['description'] -%} description:<|"|>{{ value['description'] }}<|"|> {%- set add_comma = true -%} {%- endif -%} {%- if value['type'] | upper == 'STRING' -%} {%- if value['enum'] -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} enum:{{ format_argument(value['enum']) }} {%- endif -%} {%- elif value['type'] | upper == 'ARRAY' -%} {%- if value['items'] is mapping and value['items'] -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} items:{ {%- set ns_items = namespace(found_first=false) -%} {%- for item_key, item_value in value['items'] | dictsort -%} {%- if item_value is not none -%} {%- if ns_items.found_first %},{% endif -%} {%- set ns_items.found_first = true -%} {%- if item_key == 'properties' -%} properties:{ {%- if item_value is mapping -%} {{- format_parameters(item_value, value['items']['required'] | default([])) -}} {%- endif -%} } {%- elif item_key == 'required' -%} required:[ {%- for req_item in item_value -%} <|"|>{{- req_item -}}<|"|> {%- if not loop.last %},{% endif -%} {%- endfor -%} ] {%- elif item_key == 'type' -%} {%- if item_value is string -%} type:{{ format_argument(item_value | upper) }} {%- else -%} type:{{ format_argument(item_value | map('upper') | list) }} {%- endif -%} {%- else -%} {{ item_key }}:{{ format_argument(item_value) }} {%- endif -%} {%- endif -%} {%- endfor -%} } {%- endif -%} {%- endif -%} {%- if value['nullable'] %} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} nullable:true {%- endif -%} {%- if value['type'] | upper == 'OBJECT' -%} {%- if value['properties'] is defined and value['properties'] is mapping -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} properties:{ {{- format_parameters(value['properties'], value['required'] | default([])) -}} } {%- elif value is mapping -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} properties:{ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}} } {%- endif -%} {%- if value['required'] -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} required:[ {%- for item in value['required'] | default([]) -%} <|"|>{{- item -}}<|"|> {%- if not loop.last %},{% endif -%} {%- endfor -%} ] {%- endif -%} {%- endif -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} type:<|"|>{{ value['type'] | upper }}<|"|>} {%- endif -%} {%- endfor -%} {%- endmacro -%} {%- macro format_function_declaration(tool_data) -%} declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|> {%- set params = tool_data['function']['parameters'] -%} {%- if params -%} ,parameters:{ {%- if params['properties'] -%} properties:{ {{- format_parameters(params['properties'], params['required']) -}} }, {%- endif -%} {%- if params['required'] -%} required:[ {%- for item in params['required'] -%} <|"|>{{- item -}}<|"|> {{- ',' if not loop.last -}} {%- endfor -%} ], {%- endif -%} {%- if params['type'] -%} type:<|"|>{{- params['type'] | upper -}}<|"|>} {%- endif -%} {%- endif -%} {%- if 'response' in tool_data['function'] -%} {%- set response_declaration = tool_data['function']['response'] -%} ,response:{ {%- if response_declaration['description'] -%} description:<|"|>{{- response_declaration['description'] -}}<|"|>, {%- endif -%} {%- if response_declaration['type'] | upper == 'OBJECT' -%} type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>} {%- endif -%} {%- endif -%} } {%- endmacro -%} {%- macro format_argument(argument, escape_keys=True) -%} {%- if argument is none -%} {{- 'null' -}} {%- elif argument is string -%} {{- '<|"|>' + argument + '<|"|>' -}} {%- elif argument is boolean -%} {{- 'true' if argument else 'false' -}} {%- elif argument is mapping -%} {{- '{' -}} {%- set ns = namespace(found_first=false) -%} {%- for key, value in argument | dictsort -%} {%- if ns.found_first %},{% endif -%} {%- set ns.found_first = true -%} {%- if escape_keys -%} {{- '<|"|>' + key + '<|"|>' -}} {%- else -%} {{- key -}} {%- endif -%} :{{- format_argument(value, escape_keys=escape_keys) -}} {%- endfor -%} {{- '}' -}} {%- elif argument is sequence -%} {{- '[' -}} {%- for item in argument -%} {{- format_argument(item, escape_keys=escape_keys) -}} {%- if not loop.last %},{% endif -%} {%- endfor -%} {{- ']' -}} {%- else -%} {{- argument -}} {%- endif -%} {%- endmacro -%} {%- macro strip_thinking(text) -%} {%- set ns = namespace(result='') -%} {%- for part in text.split('<channel|>') -%} {%- if '<|channel>' in part -%} {%- set ns.result = ns.result + part.split('<|channel>')[0] -%} {%- else -%} {%- set ns.result = ns.result + part -%} {%- endif -%} {%- endfor -%} {{- ns.result | trim -}} {%- endmacro -%} {%- macro format_tool_response_block(tool_name, response) -%} {{- '<|tool_response>' -}} {%- if response is mapping -%} {{- 'response:' + tool_name + '{' -}} {%- for key, value in response | dictsort -%} {{- key -}}:{{- format_argument(value, escape_keys=False) -}} {%- if not loop.last %},{% endif -%} {%- endfor -%} {{- '}' -}} {%- else -%} {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}} {%- endif -%} {{- '<tool_response|>' -}} {%- endmacro -%} {#- ===== SETUP ===== -#} {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%} {%- set loop_messages = messages -%} {%- set enable_thinking = enable_thinking | default(false) -%} {%- set preserve_thinking = preserve_thinking | default(false) -%} {{- bos_token -}} {#- Handle System/Tool Definitions Block -#} {%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%} {{- '<|turn>system\n' -}} {#- Inject Thinking token at the very top of the FIRST system turn -#} {%- if enable_thinking -%} {{- '<|think|>\n' -}} {%- set ns.prev_message_type = 'think' -%} {%- endif -%} {%- if messages and messages[0]['role'] in ['system', 'developer'] -%} {%- if messages[0]['content'] is string -%} {{- messages[0]['content'] | trim -}} {%- elif messages[0]['content'] is sequence -%} {%- for item in messages[0]['content'] -%} {{- item['text'] | trim + ' '-}} {%- endfor -%} {%- endif -%} {%- set loop_messages = messages[1:] -%} {%- endif -%} {%- if tools -%} {%- for tool in tools %} {{- '<|tool>' -}} {{- format_function_declaration(tool) | trim -}} {{- '<tool|>' -}} {%- endfor %} {%- set ns.prev_message_type = 'tool' -%} {%- endif -%} {{- '<turn|>\n' -}} {%- endif %} {#- Pre-scan: find last user message index for reasoning guard -#} {%- set ns_turn = namespace(last_user_idx=-1) -%} {%- for i in range(loop_messages | length) -%} {%- if loop_messages[i]['role'] == 'user' -%} {%- set ns_turn.last_user_idx = i -%} {%- endif -%} {%- endfor -%} {#- Loop through messages -#} {%- for message in loop_messages -%} {%- if message['role'] != 'tool' -%} {%- set ns.prev_message_type = None -%} {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%} {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#} {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%} {%- if not continue_same_model_turn -%} {{- '<|turn>' + role + '\n' }} {%- endif -%} {#- Render reasoning/reasoning_content as thinking channel -#} {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%} {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%} {%- if thinking_text and thinking_gate -%} {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}} {%- endif -%} {%- if message.get('tool_calls') -%} {%- for tool_call in message.get('tool_calls') -%} {%- set function = tool_call['function'] -%} {{- '<|tool_call>call:' + function['name'] + '{' -}} {%- if function['arguments'] is mapping -%} {%- set ns_args = namespace(found_first=false) -%} {%- for key, value in function['arguments'] | dictsort -%} {%- if ns_args.found_first %},{% endif -%} {%- set ns_args.found_first = true -%} {{- key -}}:{{- format_argument(value, escape_keys=False) -}} {%- endfor -%} {%- elif function['arguments'] is none -%} {%- else -%} {{- raise_exception( "chat_template: tool_calls[].function.arguments must be a " "JSON object (mapping), not a string. Deserialize arguments " "before passing to the template." ) -}} {%- endif -%} {{- '}<tool_call|>' -}} {%- endfor -%} {%- set ns.prev_message_type = 'tool_call' -%} {%- endif -%} {%- set ns_tr_out = namespace(flag=false) -%} {%- if message.get('tool_responses') -%} {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#} {%- for tool_response in message.get('tool_responses') -%} {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}} {%- set ns_tr_out.flag = true -%} {%- set ns.prev_message_type = 'tool_response' -%} {%- endfor -%} {%- elif message.get('tool_calls') -%} {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#} {%- set ns_tool_scan = namespace(stopped=false) -%} {%- for k in range(loop.index0 + 1, loop_messages | length) -%} {%- if ns_tool_scan.stopped -%} {%- elif loop_messages[k]['role'] != 'tool' -%} {%- set ns_tool_scan.stopped = true -%} {%- else -%} {%- set follow = loop_messages[k] -%} {#- Resolve tool_call_id to function name -#} {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%} {%- for tc in message.get('tool_calls') -%} {%- if tc.get('id') == follow.get('tool_call_id') -%} {%- set ns_tname.name = tc['function']['name'] -%} {%- endif -%} {%- endfor -%} {#- Handle content as string or content-parts array -#} {%- set tool_body = follow.get('content') -%} {%- if tool_body is string -%} {{- format_tool_response_block(ns_tname.name, tool_body) -}} {%- elif tool_body is sequence and tool_body is not string -%} {%- set ns_txt = namespace(s='') -%} {%- for part in tool_body -%} {%- if part.get('type') == 'text' -%} {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%} {%- endif -%} {%- endfor -%} {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}} {%- for part in tool_body -%} {%- if part.get('type') in ['image', 'image_url'] -%} {{- '<|image|>' -}} {%- elif part.get('type') in ['audio', 'input_audio'] -%} {{- '<|audio|>' -}} {%- elif part.get('type') == 'video' -%} {{- '<|video|>' -}} {%- endif -%} {%- endfor -%} {%- else -%} {{- format_tool_response_block(ns_tname.name, tool_body) -}} {%- endif -%} {%- set ns_tr_out.flag = true -%} {%- set ns.prev_message_type = 'tool_response' -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- set captured_content -%} {%- if message.get('content') is string -%} {%- if role == 'model' -%} {{- strip_thinking(message['content']) -}} {%- else -%} {{- message['content'] | trim -}} {%- endif -%} {%- elif message.get('content') is sequence -%} {%- for item in message['content'] -%} {%- if item.get('type') == 'text' -%} {%- if role == 'model' -%} {{- strip_thinking(item['text']) -}} {%- else -%} {{- item['text'] | trim -}} {%- endif -%} {%- elif item.get('type') in ['image', 'image_url'] -%} {{- '<|image|>' -}} {%- elif item.get('type') in ['audio', 'input_audio'] -%} {{- '<|audio|>' -}} {%- elif item.get('type') == 'video' -%} {{- '<|video|>' -}} {%- endif -%} {%- endfor -%} {%- endif -%} {%- endset -%} {{- captured_content -}} {%- set has_content = captured_content | trim | length > 0 -%} {#- Forward-scan: find next non-tool message role for continuation detection -#} {%- set next_nt = namespace(role=None, found=false) -%} {%- for j in range(loop.index0 + 1, loop_messages | length) -%} {%- if not next_nt.found -%} {%- if loop_messages[j]['role'] != 'tool' -%} {%- set next_nt.role = loop_messages[j]['role'] -%} {%- set next_nt.found = true -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- set continues_into_next = ( role == 'model' and next_nt.role == 'assistant' and (not message.get('tool_calls') or ns_tr_out.flag) ) -%} {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%} {{- '<|tool_response>' -}} {%- elif continues_into_next -%} {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%} {{- '<turn|>\n' -}} {%- endif -%} {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#} {%- set ns.prev_non_tool_role = message['role'] -%} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%} {{- '<|turn>model\n' -}} {%- if not enable_thinking -%} {{- '<|channel>thought\n<channel|>' -}} {%- endif -%} {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%} {{- '<|channel>thought\n' -}} {%- endif -%} {%- endif -%}
수정본
파일 열기
{# Template: Google Gemma 4 Canonical Chat Template Author: Google Gemma Engineering Team Published: 2026-07-09 Context: Fixed tool-calling loops, turn closures, and thinking content-ordering. #} {%- macro format_parameters(properties, required, filter_keys=false) -%} {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%} {%- set ns = namespace(found_first=false) -%} {%- for key, value in properties | dictsort -%} {%- set add_comma = false -%} {%- if not filter_keys or key not in standard_keys -%} {%- if ns.found_first %},{% endif -%} {%- set ns.found_first = true -%} {{ key }}:{ {%- if value['description'] -%} description:<|"|>{{ value['description'] }}<|"|> {%- set add_comma = true -%} {%- endif -%} {%- if value['type'] | upper == 'STRING' -%} {%- if value['enum'] -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} enum:{{ format_argument(value['enum']) }} {%- endif -%} {%- elif value['type'] | upper == 'ARRAY' -%} {%- if value['items'] is mapping and value['items'] -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} items:{ {%- set ns_items = namespace(found_first=false) -%} {%- for item_key, item_value in value['items'] | dictsort -%} {%- if item_value is not none -%} {%- if ns_items.found_first %},{% endif -%} {%- set ns_items.found_first = true -%} {%- if item_key == 'properties' -%} properties:{ {%- if item_value is mapping -%} {{- format_parameters(item_value, value['items']['required'] | default([])) -}} {%- endif -%} } {%- elif item_key == 'required' -%} required:[ {%- for req_item in item_value -%} <|"|>{{- req_item -}}<|"|> {%- if not loop.last %},{% endif -%} {%- endfor -%} ] {%- elif item_key == 'type' -%} {%- if item_value is string -%} type:{{ format_argument(item_value | upper) }} {%- else -%} type:{{ format_argument(item_value | map('upper') | list) }} {%- endif -%} {%- else -%} {{ item_key }}:{{ format_argument(item_value) }} {%- endif -%} {%- endif -%} {%- endfor -%} } {%- endif -%} {%- endif -%} {%- if value['nullable'] %} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} nullable:true {%- endif -%} {%- if value['type'] | upper == 'OBJECT' -%} {%- if value['properties'] is defined and value['properties'] is mapping -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} properties:{ {{- format_parameters(value['properties'], value['required'] | default([])) -}} } {%- elif value is mapping -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} properties:{ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}} } {%- endif -%} {%- if value['required'] -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} required:[ {%- for item in value['required'] | default([]) -%} <|"|>{{- item -}}<|"|> {%- if not loop.last %},{% endif -%} {%- endfor -%} ] {%- endif -%} {%- endif -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} type:<|"|>{{ value['type'] | upper }}<|"|>} {%- endif -%} {%- endfor -%} {%- endmacro -%} {%- macro format_function_declaration(tool_data) -%} declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|> {%- set params = tool_data['function']['parameters'] -%} {%- if params -%} ,parameters:{ {%- if params['properties'] -%} properties:{ {{- format_parameters(params['properties'], params['required']) -}} }, {%- endif -%} {%- if params['required'] -%} required:[ {%- for item in params['required'] -%} <|"|>{{- item -}}<|"|> {{- ',' if not loop.last -}} {%- endfor -%} ], {%- endif -%} {%- if params['type'] -%} type:<|"|>{{- params['type'] | upper -}}<|"|>} {%- endif -%} {%- endif -%} {%- if 'response' in tool_data['function'] -%} {%- set response_declaration = tool_data['function']['response'] -%} ,response:{ {%- if response_declaration['description'] -%} description:<|"|>{{- response_declaration['description'] -}}<|"|>, {%- endif -%} {%- if response_declaration['type'] | upper == 'OBJECT' -%} type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>} {%- endif -%} {%- endif -%} } {%- endmacro -%} {%- macro format_argument(argument, escape_keys=True) -%} {%- if argument is none -%} {{- 'null' -}} {%- elif argument is string -%} {{- '<|"|>' + argument + '<|"|>' -}} {%- elif argument is boolean -%} {{- 'true' if argument else 'false' -}} {%- elif argument is mapping -%} {{- '{' -}} {%- set ns = namespace(found_first=false) -%} {%- for key, value in argument | dictsort -%} {%- if ns.found_first %},{% endif -%} {%- set ns.found_first = true -%} {%- if escape_keys -%} {{- '<|"|>' + key + '<|"|>' -}} {%- else -%} {{- key -}} {%- endif -%} :{{- format_argument(value, escape_keys=escape_keys) -}} {%- endfor -%} {{- '}' -}} {%- elif argument is sequence -%} {{- '[' -}} {%- for item in argument -%} {{- format_argument(item, escape_keys=escape_keys) -}} {%- if not loop.last %},{% endif -%} {%- endfor -%} {{- ']' -}} {%- else -%} {{- argument -}} {%- endif -%} {%- endmacro -%} {%- macro strip_thinking(text) -%} {%- set ns = namespace(result='') -%} {%- for part in text.split('<channel|>') -%} {%- if '<|channel>' in part -%} {%- set ns.result = ns.result + part.split('<|channel>')[0] -%} {%- else -%} {%- set ns.result = ns.result + part -%} {%- endif -%} {%- endfor -%} {{- ns.result | trim -}} {%- endmacro -%} {%- macro format_tool_response_block(tool_name, response) -%} {{- '<|tool_response>' -}} {%- if response is mapping -%} {{- 'response:' + tool_name + '{' -}} {%- for key, value in response | dictsort -%} {{- key -}}:{{- format_argument(value, escape_keys=False) -}} {%- if not loop.last %},{% endif -%} {%- endfor -%} {{- '}' -}} {%- else -%} {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}} {%- endif -%} {{- '<tool_response|>' -}} {%- endmacro -%} {#- ===== SETUP ===== -#} {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%} {%- set loop_messages = messages -%} {%- set enable_thinking = enable_thinking | default(false) -%} {%- set preserve_thinking = preserve_thinking if preserve_thinking is defined else true -%} {{- bos_token -}} {#- Handle System/Tool Definitions Block -#} {%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%} {{- '<|turn>system\n' -}} {#- Inject Thinking token at the very top of the FIRST system turn -#} {%- if enable_thinking -%} {{- '<|think|>\n' -}} {%- set ns.prev_message_type = 'think' -%} {%- endif -%} {%- if messages and messages[0]['role'] in ['system', 'developer'] -%} {%- if messages[0]['content'] is string -%} {{- messages[0]['content'] | trim -}} {%- elif messages[0]['content'] is sequence -%} {%- for item in messages[0]['content'] -%} {{- item['text'] | trim + ' '-}} {%- endfor -%} {%- endif -%} {%- set loop_messages = messages[1:] -%} {%- endif -%} {%- if tools -%} {%- for tool in tools %} {{- '<|tool>' -}} {{- format_function_declaration(tool) | trim -}} {{- '<tool|>' -}} {%- endfor %} {%- set ns.prev_message_type = 'tool' -%} {%- endif -%} {{- '<turn|>\n' -}} {%- endif %} {#- Pre-scan: find last user message index for reasoning guard -#} {%- set ns_turn = namespace(last_user_idx=-1) -%} {%- for i in range(loop_messages | length) -%} {%- if loop_messages[i]['role'] == 'user' -%} {%- set ns_turn.last_user_idx = i -%} {%- endif -%} {%- endfor -%} {#- Loop through messages -#} {%- for message in loop_messages -%} {%- if message['role'] != 'tool' -%} {%- set ns.prev_message_type = None -%} {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%} {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#} {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%} {%- if not continue_same_model_turn -%} {{- '<|turn>' + role + '\n' }} {%- endif -%} {#- Render reasoning/reasoning_content as thinking channel -#} {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%} {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or preserve_thinking -%} {%- if thinking_text and thinking_gate -%} {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}} {%- endif -%} {%- if message.get('tool_calls') -%} {%- for tool_call in message.get('tool_calls') -%} {%- set function = tool_call['function'] -%} {{- '<|tool_call>call:' + function['name'] + '{' -}} {%- if function['arguments'] is mapping -%} {%- set ns_args = namespace(found_first=false) -%} {%- for key, value in function['arguments'] | dictsort -%} {%- if ns_args.found_first %},{% endif -%} {%- set ns_args.found_first = true -%} {{- key -}}:{{- format_argument(value, escape_keys=False) -}} {%- endfor -%} {%- elif function['arguments'] is none -%} {%- else -%} {{- raise_exception( "chat_template: tool_calls[].function.arguments must be a " "JSON object (mapping), not a string. Deserialize arguments " "before passing to the template." ) -}} {%- endif -%} {{- '}<tool_call|>' -}} {%- endfor -%} {%- set ns.prev_message_type = 'tool_call' -%} {%- endif -%} {%- set ns_tr_out = namespace(flag=false) -%} {%- if message.get('tool_responses') -%} {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#} {%- for tool_response in message.get('tool_responses') -%} {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}} {%- set ns_tr_out.flag = true -%} {%- set ns.prev_message_type = 'tool_response' -%} {%- endfor -%} {%- elif message.get('tool_calls') -%} {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#} {%- set ns_tool_scan = namespace(stopped=false) -%} {%- for k in range(loop.index0 + 1, loop_messages | length) -%} {%- if ns_tool_scan.stopped -%} {%- elif loop_messages[k]['role'] != 'tool' -%} {%- set ns_tool_scan.stopped = true -%} {%- else -%} {%- set follow = loop_messages[k] -%} {#- Resolve tool_call_id to function name -#} {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%} {%- for tc in message.get('tool_calls') -%} {%- if tc.get('id') == follow.get('tool_call_id') -%} {%- set ns_tname.name = tc['function']['name'] -%} {%- endif -%} {%- endfor -%} {#- Handle content as string or content-parts array -#} {%- set tool_body = follow.get('content') -%} {%- if tool_body is string -%} {{- format_tool_response_block(ns_tname.name, tool_body) -}} {%- elif tool_body is sequence and tool_body is not string -%} {%- set ns_txt = namespace(s='') -%} {%- for part in tool_body -%} {%- if part.get('type') == 'text' -%} {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%} {%- endif -%} {%- endfor -%} {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}} {%- for part in tool_body -%} {%- if part.get('type') in ['image', 'image_url'] -%} {{- '<|image|>' -}} {%- elif part.get('type') in ['audio', 'input_audio'] -%} {{- '<|audio|>' -}} {%- elif part.get('type') == 'video' -%} {{- '<|video|>' -}} {%- endif -%} {%- endfor -%} {%- else -%} {{- format_tool_response_block(ns_tname.name, tool_body) -}} {%- endif -%} {%- set ns_tr_out.flag = true -%} {%- set ns.prev_message_type = 'tool_response' -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- set captured_content -%} {%- if message.get('content') is string -%} {%- if role == 'model' -%} {{- strip_thinking(message['content']) -}} {%- else -%} {{- message['content'] | trim -}} {%- endif -%} {%- elif message.get('content') is sequence -%} {%- for item in message['content'] -%} {%- if item.get('type') == 'text' -%} {%- if role == 'model' -%} {{- strip_thinking(item['text']) -}} {%- else -%} {{- item['text'] | trim -}} {%- endif -%} {%- elif item.get('type') in ['image', 'image_url'] -%} {{- '<|image|>' -}} {%- elif item.get('type') in ['audio', 'input_audio'] -%} {{- '<|audio|>' -}} {%- elif item.get('type') == 'video' -%} {{- '<|video|>' -}} {%- endif -%} {%- endfor -%} {%- endif -%} {%- endset -%} {{- captured_content -}} {%- set has_content = captured_content | trim | length > 0 -%} {#- Forward-scan: find next non-tool message role for continuation detection -#} {%- set next_nt = namespace(role=None, found=false) -%} {%- for j in range(loop.index0 + 1, loop_messages | length) -%} {%- if not next_nt.found -%} {%- if loop_messages[j]['role'] != 'tool' -%} {%- set next_nt.role = loop_messages[j]['role'] -%} {%- set next_nt.found = true -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- set continues_into_next = ( role == 'model' and next_nt.role == 'assistant' and (not message.get('tool_calls') or ns_tr_out.flag) ) -%} {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%} {{- '<|tool_response>' -}} {%- elif continues_into_next -%} {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%} {{- '<turn|>\n' -}} {%- endif -%} {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#} {%- set ns.prev_non_tool_role = message['role'] -%} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%} {{- '<|turn>model\n' -}} {%- if not enable_thinking -%} {{- '<|channel>thought\n<channel|>' -}} {%- endif -%} {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%} {{- '<|channel>thought\n' -}} {%- endif -%} {%- endif -%}
비교하기