Instructions to use google/gemma-4-12B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-4-12B-it with Transformers:
# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("google/gemma-4-12B-it") model = AutoModelForMultimodalLM.from_pretrained("google/gemma-4-12B-it") - Notebooks
- Google Colab
- Kaggle
Preserve the correct ordering of content and tool calls when rendering OpenAI Chat Completions
Browse files## Preserve the correct ordering for OpenAI-style assistant turns that include both visible content and tool_calls
- `[{"role": "assistant", "content": ..., "tool_calls": ...}, {"role": "tool", ...}]` was previously rendered as `tool_calls, tool_responses, content` with an extra `<turn|>` token, now renders as `content, tool_calls, tool_responses`.
## Avoid emitting a duplicate <|tool_response> opener for the legacy assistant-embedded tool_responses
- `[{"role": "assistant", "tool_calls": ...}, {"role": "assistant", "tool_responses": ...}]` was previously rendered as `<|tool_response><|tool_response> ... <tool_response|>`. An extra `<|tool_response>` was emitted.
Example before vs after:
```
<bos><|turn>system
<|think|>
<|tool>declaration:get_current_pe_ratio{description:<|"|>Get the current price-to-earnings ratio for a stock ticker.<|"|>,parameters:{properties:{ticker:{description:<|"|>Stock ticker. Example: INTC for Intel Corporation.<|"|>,type:<|"|>STRING<|"|>}},required:[<|"|>ticker<|"|>],type:<|"|>OBJECT<|"|>}}<tool|><turn|>
<|turn>user
What does a P/E ratio mean, and what's NVIDIA's current one?<turn|>
<|turn>model
<|channel>thought
The user is asking two things:
1. The meaning of a P/E ratio.
2. The current P/E ratio for NVIDIA.
I can explain the P/E ratio based on general knowledge.
To get NVIDIA's current P/E ratio, I should use the `get_current_pe_ratio` tool.
Plan:
1. Explain what a P/E ratio is.
2. Call `get_current_pe_ratio` with the ticker 'NVDA' (NVIDIA's ticker).
3. Provide the result to the user.
<channel|><|tool_call>call:get_current_pe_ratio{ticker:<|"|>NVDA<|"|>}<tool_call|><|tool_response>response:get_current_pe_ratio{value:<|"|>{"ticker": "NVDA", "pe_ratio": 35.13}<|"|>}<tool_response|>A **P/E ratio**, or Price-to-Earnings ratio, is a key financial metric used to value a company. It represents the relationship between a company's current share price and its earnings per share (EPS).
Essentially, it tells you how much investors are willing to pay for every $1 of profit the company generates.
* **High P/E:** Often suggests that investors expect high growth in the future, or that the stock is overvalued.
* **Low P/E:** May indicate that the company is undervalued, or that investors have low expectations for its future growth.
To get NVIDIA's current P/E ratio, let me look that up for you.<turn|>
<|channel>thought
The tool call has already been made and the result is available: `{"ticker": "NVDA", "pe_ratio": 35.13}`. I should now incorporate this value into the final response.
<channel|>NVIDIA's current P/E ratio is **35.13**.<turn|>
```
Note that an extra `<turn|>` was emitted. Although tool call was emitted after visible content, the template rendered tool call and tool result before content.
vs
```
<bos><|turn>system
<|think|>
<|tool>declaration:get_current_pe_ratio{description:<|"|>Get the current price-to-earnings ratio for a stock ticker.<|"|>,parameters:{properties:{ticker:{description:<|"|>Stock ticker. Example: INTC for Intel Corporation.<|"|>,type:<|"|>STRING<|"|>}},required:[<|"|>ticker<|"|>],type:<|"|>OBJECT<|"|>}}<tool|><turn|>
<|turn>user
What does a P/E ratio mean, and what's NVIDIA's current one?<turn|>
<|turn>model
<|channel>thought
The user is asking two things:
1. The meaning of a P/E ratio.
2. The current P/E ratio for NVIDIA.
I can explain the P/E ratio based on general knowledge.
To get NVIDIA's current P/E ratio, I should use the `get_current_pe_ratio` tool.
Plan:
1. Explain what a P/E ratio is.
2. Call `get_current_pe_ratio` with the ticker 'NVDA' (NVIDIA's ticker).
3. Provide the result to the user.
<channel|><|tool_call>call:get_current_pe_ratio{ticker:<|"|>NVDA<|"|>}<tool_call|><|tool_response>response:get_current_pe_ratio{value:<|"|>{"ticker": "NVDA", "pe_ratio": 35.13}<|"|>}<tool_response|>A **P/E ratio**, or Price-to-Earnings ratio, is a key financial metric used to value a company. It represents the relationship between a company's current share price and its earnings per share (EPS).
Essentially, it tells you how much investors are willing to pay for every $1 of profit the company generates.
* **High P/E:** Often suggests that investors expect high growth in the future, or that the stock is overvalued.
* **Low P/E:** May indicate that the company is undervalued, or that investors have low expectations for its future growth.
To get NVIDIA's current P/E ratio, let me look that up for you.<turn|>
<|channel>thought
The tool call has already been made and the result is available: `{"ticker": "NVDA", "pe_ratio": 35.13}`. I should now incorporate this value into the final response.
<channel|>NVIDIA's current P/E ratio is **35.13**.<turn|>
```
https://github.com/vllm-project/vllm/pull/42776
- chat_template.jinja +74 -50
|
@@ -172,16 +172,25 @@
|
|
| 172 |
{{- '<tool_response|>' -}}
|
| 173 |
{%- endmacro -%}
|
| 174 |
|
| 175 |
-
{%-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
{%- set loop_messages = messages -%}
|
| 177 |
{{- bos_token -}}
|
| 178 |
{#- Handle System/Tool Definitions Block -#}
|
| 179 |
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
|
| 180 |
-
{{- '
|
| 181 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 182 |
{%- if enable_thinking is defined and enable_thinking -%}
|
| 183 |
{{- '<|think|>\n' -}}
|
| 184 |
-
{%- set ns.prev_message_type = 'think' -%}
|
| 185 |
{%- endif -%}
|
| 186 |
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 187 |
{%- if messages[0]['content'] is string -%}
|
|
@@ -199,9 +208,8 @@
|
|
| 199 |
{{- format_function_declaration(tool) | trim -}}
|
| 200 |
{{- '<tool|>' -}}
|
| 201 |
{%- endfor %}
|
| 202 |
-
{%- set ns.prev_message_type = 'tool' -%}
|
| 203 |
{%- endif -%}
|
| 204 |
-
{{-
|
| 205 |
{%- endif %}
|
| 206 |
|
| 207 |
{#- Pre-scan: find last user message index for reasoning guard -#}
|
|
@@ -215,31 +223,24 @@
|
|
| 215 |
{#- Loop through messages -#}
|
| 216 |
{%- for message in loop_messages -%}
|
| 217 |
{%- if message['role'] != 'tool' -%}
|
| 218 |
-
{%- set ns.prev_message_type = None -%}
|
| 219 |
{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
|
| 220 |
-
{
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
{%- set prev_nt.found = true -%}
|
| 228 |
-
{%- endif -%}
|
| 229 |
-
{%- endif -%}
|
| 230 |
-
{%- endfor -%}
|
| 231 |
-
{%- endif -%}
|
| 232 |
-
{%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
|
| 233 |
-
{%- if not continue_same_model_turn -%}
|
| 234 |
-
{{- '<|turn>' + role + '\n' }}
|
| 235 |
{%- endif -%}
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
| 242 |
|
|
|
|
| 243 |
{%- if message['tool_calls'] -%}
|
| 244 |
{%- for tool_call in message['tool_calls'] -%}
|
| 245 |
{%- set function = tool_call['function'] -%}
|
|
@@ -256,18 +257,20 @@
|
|
| 256 |
{%- endif -%}
|
| 257 |
{{- '}<tool_call|>' -}}
|
| 258 |
{%- endfor -%}
|
| 259 |
-
{%- set ns.prev_message_type = 'tool_call' -%}
|
| 260 |
{%- endif -%}
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
{%- if message.get('tool_responses') -%}
|
| 264 |
{#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
|
| 265 |
{%- for tool_response in message['tool_responses'] -%}
|
| 266 |
-
{{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
|
| 267 |
-
{%- set ns_tr_out.flag = true -%}
|
| 268 |
-
{%- set ns.prev_message_type = 'tool_response' -%}
|
| 269 |
{%- endfor -%}
|
| 270 |
-
{%-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
{#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
|
| 272 |
{%- set ns_tool_scan = namespace(stopped=false) -%}
|
| 273 |
{%- for k in range(loop.index0 + 1, loop_messages | length) -%}
|
|
@@ -277,7 +280,7 @@
|
|
| 277 |
{%- else -%}
|
| 278 |
{%- set follow = loop_messages[k] -%}
|
| 279 |
{#- Resolve tool_call_id to function name -#}
|
| 280 |
-
{%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}
|
| 281 |
{%- for tc in message['tool_calls'] -%}
|
| 282 |
{%- if tc.get('id') == follow.get('tool_call_id') -%}
|
| 283 |
{%- set ns_tname.name = tc['function']['name'] -%}
|
|
@@ -307,13 +310,12 @@
|
|
| 307 |
{%- else -%}
|
| 308 |
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
| 309 |
{%- endif -%}
|
| 310 |
-
{%- set ns_tr_out.flag = true -%}
|
| 311 |
-
{%- set ns.prev_message_type = 'tool_response' -%}
|
| 312 |
{%- endif -%}
|
| 313 |
{%- endfor -%}
|
| 314 |
{%- endif -%}
|
|
|
|
| 315 |
|
| 316 |
-
|
| 317 |
{%- if message['content'] is string -%}
|
| 318 |
{%- if role == 'model' -%}
|
| 319 |
{{- strip_thinking(message['content']) -}}
|
|
@@ -330,34 +332,56 @@
|
|
| 330 |
{%- endif -%}
|
| 331 |
{%- elif item['type'] == 'image' -%}
|
| 332 |
{{- '<|image|>' -}}
|
| 333 |
-
{%- set ns.prev_message_type = 'image' -%}
|
| 334 |
{%- elif item['type'] == 'audio' -%}
|
| 335 |
{{- '<|audio|>' -}}
|
| 336 |
-
{%- set ns.prev_message_type = 'audio' -%}
|
| 337 |
{%- elif item['type'] == 'video' -%}
|
| 338 |
{{- '<|video|>' -}}
|
| 339 |
-
{%- set ns.prev_message_type = 'video' -%}
|
| 340 |
{%- endif -%}
|
| 341 |
{%- endfor -%}
|
| 342 |
{%- endif -%}
|
| 343 |
-
|
| 344 |
|
|
|
|
|
|
|
| 345 |
{{- captured_content -}}
|
| 346 |
-
{
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
{{-
|
| 350 |
-
|
| 351 |
-
{{-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
{%- endif -%}
|
| 353 |
{%- endif -%}
|
| 354 |
{%- endfor -%}
|
| 355 |
|
| 356 |
{%- if add_generation_prompt -%}
|
| 357 |
-
{
|
| 358 |
-
|
| 359 |
-
{%- if
|
| 360 |
-
{{-
|
| 361 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
{%- endif -%}
|
| 363 |
{%- endif -%}
|
|
|
|
| 172 |
{{- '<tool_response|>' -}}
|
| 173 |
{%- endmacro -%}
|
| 174 |
|
| 175 |
+
{%- macro open_turn(role, ns) -%}
|
| 176 |
+
{%- set ns.open_turn_role = role -%}
|
| 177 |
+
{{- '<|turn>' + role + '\n' -}}
|
| 178 |
+
{%- endmacro -%}
|
| 179 |
+
|
| 180 |
+
{%- macro end_turn(ns) -%}
|
| 181 |
+
{%- set ns.open_turn_role = none -%}
|
| 182 |
+
{{- '<turn|>\n' -}}
|
| 183 |
+
{%- endmacro -%}
|
| 184 |
+
|
| 185 |
+
{%- set ns = namespace(open_turn_role=none) -%}
|
| 186 |
{%- set loop_messages = messages -%}
|
| 187 |
{{- bos_token -}}
|
| 188 |
{#- Handle System/Tool Definitions Block -#}
|
| 189 |
{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
|
| 190 |
+
{{- open_turn('system', ns) -}}
|
| 191 |
{#- Inject Thinking token at the very top of the FIRST system turn -#}
|
| 192 |
{%- if enable_thinking is defined and enable_thinking -%}
|
| 193 |
{{- '<|think|>\n' -}}
|
|
|
|
| 194 |
{%- endif -%}
|
| 195 |
{%- if messages[0]['role'] in ['system', 'developer'] -%}
|
| 196 |
{%- if messages[0]['content'] is string -%}
|
|
|
|
| 208 |
{{- format_function_declaration(tool) | trim -}}
|
| 209 |
{{- '<tool|>' -}}
|
| 210 |
{%- endfor %}
|
|
|
|
| 211 |
{%- endif -%}
|
| 212 |
+
{{- end_turn(ns) -}}
|
| 213 |
{%- endif %}
|
| 214 |
|
| 215 |
{#- Pre-scan: find last user message index for reasoning guard -#}
|
|
|
|
| 223 |
{#- Loop through messages -#}
|
| 224 |
{%- for message in loop_messages -%}
|
| 225 |
{%- if message['role'] != 'tool' -%}
|
|
|
|
| 226 |
{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
|
| 227 |
+
{%- if ns.open_turn_role -%}
|
| 228 |
+
{%- if ns.open_turn_role != role -%}
|
| 229 |
+
{{- end_turn(ns) -}}
|
| 230 |
+
{{- open_turn(role, ns) -}}
|
| 231 |
+
{%- endif -%}
|
| 232 |
+
{%- else -%}
|
| 233 |
+
{{- open_turn(role, ns) -}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
{%- endif -%}
|
| 235 |
|
| 236 |
+
{%- set reasoning_block -%}
|
| 237 |
+
{%- set thinking_text = message['reasoning'] | default(message['reasoning_content']) | default(none) -%}
|
| 238 |
+
{%- if thinking_text is not none and loop.index0 > ns_turn.last_user_idx -%}
|
| 239 |
+
{{- '<|channel>thought\n' + thinking_text + '<channel|>' -}}
|
| 240 |
+
{%- endif -%}
|
| 241 |
+
{%- endset -%}
|
| 242 |
|
| 243 |
+
{%- set tool_calls_block -%}
|
| 244 |
{%- if message['tool_calls'] -%}
|
| 245 |
{%- for tool_call in message['tool_calls'] -%}
|
| 246 |
{%- set function = tool_call['function'] -%}
|
|
|
|
| 257 |
{%- endif -%}
|
| 258 |
{{- '}<tool_call|>' -}}
|
| 259 |
{%- endfor -%}
|
|
|
|
| 260 |
{%- endif -%}
|
| 261 |
+
{%- endset -%}
|
| 262 |
|
| 263 |
+
{%- set legacy_tool_responses_block -%}
|
| 264 |
{%- if message.get('tool_responses') -%}
|
| 265 |
{#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
|
| 266 |
{%- for tool_response in message['tool_responses'] -%}
|
| 267 |
+
{{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
|
|
|
|
|
|
|
| 268 |
{%- endfor -%}
|
| 269 |
+
{%- endif -%}
|
| 270 |
+
{%- endset -%}
|
| 271 |
+
|
| 272 |
+
{%- set openai_tool_responses_block -%}
|
| 273 |
+
{%- if message.get('tool_calls') and not message.get('tool_responses') -%}
|
| 274 |
{#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
|
| 275 |
{%- set ns_tool_scan = namespace(stopped=false) -%}
|
| 276 |
{%- for k in range(loop.index0 + 1, loop_messages | length) -%}
|
|
|
|
| 280 |
{%- else -%}
|
| 281 |
{%- set follow = loop_messages[k] -%}
|
| 282 |
{#- Resolve tool_call_id to function name -#}
|
| 283 |
+
{%- set ns_tname = namespace(name=follow.get('name') | default('unknown', true)) -%}
|
| 284 |
{%- for tc in message['tool_calls'] -%}
|
| 285 |
{%- if tc.get('id') == follow.get('tool_call_id') -%}
|
| 286 |
{%- set ns_tname.name = tc['function']['name'] -%}
|
|
|
|
| 310 |
{%- else -%}
|
| 311 |
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
| 312 |
{%- endif -%}
|
|
|
|
|
|
|
| 313 |
{%- endif -%}
|
| 314 |
{%- endfor -%}
|
| 315 |
{%- endif -%}
|
| 316 |
+
{%- endset -%}
|
| 317 |
|
| 318 |
+
{%- set captured_content -%}
|
| 319 |
{%- if message['content'] is string -%}
|
| 320 |
{%- if role == 'model' -%}
|
| 321 |
{{- strip_thinking(message['content']) -}}
|
|
|
|
| 332 |
{%- endif -%}
|
| 333 |
{%- elif item['type'] == 'image' -%}
|
| 334 |
{{- '<|image|>' -}}
|
|
|
|
| 335 |
{%- elif item['type'] == 'audio' -%}
|
| 336 |
{{- '<|audio|>' -}}
|
|
|
|
| 337 |
{%- elif item['type'] == 'video' -%}
|
| 338 |
{{- '<|video|>' -}}
|
|
|
|
| 339 |
{%- endif -%}
|
| 340 |
{%- endfor -%}
|
| 341 |
{%- endif -%}
|
| 342 |
+
{%- endset -%}
|
| 343 |
|
| 344 |
+
{%- if role != 'model' -%}
|
| 345 |
+
{#- Non-model turn -#}
|
| 346 |
{{- captured_content -}}
|
| 347 |
+
{{- end_turn(ns) -}}
|
| 348 |
+
{%- elif message.get('tool_responses') -%}
|
| 349 |
+
{#- Model turn with legacy tool -#}
|
| 350 |
+
{{- reasoning_block -}}
|
| 351 |
+
{{- tool_calls_block -}}
|
| 352 |
+
{{- legacy_tool_responses_block -}}
|
| 353 |
+
{%- if captured_content -%}
|
| 354 |
+
{{- captured_content -}}
|
| 355 |
+
{{- end_turn(ns) -}}
|
| 356 |
+
{%- endif -%}
|
| 357 |
+
{%- elif message.get('tool_calls') -%}
|
| 358 |
+
{#- Model turn with OpenAI tool -#}
|
| 359 |
+
{{- reasoning_block -}}
|
| 360 |
+
{{- captured_content -}}
|
| 361 |
+
{{- tool_calls_block -}}
|
| 362 |
+
{%- if openai_tool_responses_block -%}
|
| 363 |
+
{{- openai_tool_responses_block -}}
|
| 364 |
+
{%- elif loop.last -%}
|
| 365 |
+
{{- '<|tool_response>' -}}
|
| 366 |
+
{%- endif -%}
|
| 367 |
+
{%- else -%}
|
| 368 |
+
{#- Model turn without tool -#}
|
| 369 |
+
{{- reasoning_block -}}
|
| 370 |
+
{{- captured_content -}}
|
| 371 |
+
{{- end_turn(ns) -}}
|
| 372 |
{%- endif -%}
|
| 373 |
{%- endif -%}
|
| 374 |
{%- endfor -%}
|
| 375 |
|
| 376 |
{%- if add_generation_prompt -%}
|
| 377 |
+
{#- Close previous non-model turn and start a new model turn -#}
|
| 378 |
+
{%- if ns.open_turn_role != 'model' -%}
|
| 379 |
+
{%- if ns.open_turn_role -%}
|
| 380 |
+
{{- end_turn(ns) -}}
|
| 381 |
{%- endif -%}
|
| 382 |
+
{{- open_turn('model', ns) -}}
|
| 383 |
+
{%- endif -%}
|
| 384 |
+
{%- if not enable_thinking | default(false) -%}
|
| 385 |
+
{{- '<|channel>thought\n<channel|>' -}}
|
| 386 |
{%- endif -%}
|
| 387 |
{%- endif -%}
|