Instructions to use google/gemma-4-31B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-4-31B-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="google/gemma-4-31B-it") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForMultimodalLM.from_pretrained("google/gemma-4-31B-it", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- AMD Developer Cloud
- Local Apps Settings
- vLLM
How to use google/gemma-4-31B-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-4-31B-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-4-31B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/google/gemma-4-31B-it
- SGLang
How to use google/gemma-4-31B-it with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "google/gemma-4-31B-it" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-4-31B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "google/gemma-4-31B-it" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-4-31B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use google/gemma-4-31B-it with Docker Model Runner:
docker model run hf.co/google/gemma-4-31B-it
fix(chat_template): emit tool_response multimodal placeholders inside the block
#140
by leeesangwon - opened
- chat_template.jinja +18 -22
chat_template.jinja
CHANGED
|
@@ -174,6 +174,23 @@
|
|
| 174 |
{%- if not loop.last %},{% endif -%}
|
| 175 |
{%- endfor -%}
|
| 176 |
{{- '}' -}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
{%- else -%}
|
| 178 |
{{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
|
| 179 |
{%- endif -%}
|
|
@@ -292,28 +309,7 @@
|
|
| 292 |
{%- endfor -%}
|
| 293 |
{#- Handle content as string or content-parts array -#}
|
| 294 |
{%- set tool_body = follow.get('content') -%}
|
| 295 |
-
{
|
| 296 |
-
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
| 297 |
-
{%- elif tool_body is sequence and tool_body is not string -%}
|
| 298 |
-
{%- set ns_txt = namespace(s='') -%}
|
| 299 |
-
{%- for part in tool_body -%}
|
| 300 |
-
{%- if part.get('type') == 'text' -%}
|
| 301 |
-
{%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
|
| 302 |
-
{%- endif -%}
|
| 303 |
-
{%- endfor -%}
|
| 304 |
-
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
|
| 305 |
-
{%- for part in tool_body -%}
|
| 306 |
-
{%- if part.get('type') in ['image', 'image_url'] -%}
|
| 307 |
-
{{- '<|image|>' -}}
|
| 308 |
-
{%- elif part.get('type') in ['audio', 'input_audio'] -%}
|
| 309 |
-
{{- '<|audio|>' -}}
|
| 310 |
-
{%- elif part.get('type') == 'video' -%}
|
| 311 |
-
{{- '<|video|>' -}}
|
| 312 |
-
{%- endif -%}
|
| 313 |
-
{%- endfor -%}
|
| 314 |
-
{%- else -%}
|
| 315 |
-
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
| 316 |
-
{%- endif -%}
|
| 317 |
{%- set ns_tr_out.flag = true -%}
|
| 318 |
{%- set ns.prev_message_type = 'tool_response' -%}
|
| 319 |
{%- endif -%}
|
|
|
|
| 174 |
{%- if not loop.last %},{% endif -%}
|
| 175 |
{%- endfor -%}
|
| 176 |
{{- '}' -}}
|
| 177 |
+
{%- elif response is string -%}
|
| 178 |
+
{{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
|
| 179 |
+
{%- elif response is sequence -%}
|
| 180 |
+
{%- set ns_tr = namespace(txt='', mm='') -%}
|
| 181 |
+
{%- for part in response -%}
|
| 182 |
+
{%- if part.get('type') == 'text' -%}
|
| 183 |
+
{%- set ns_tr.txt = ns_tr.txt + (part.get('text') | default('')) -%}
|
| 184 |
+
{%- elif part.get('type') in ['image', 'image_url'] -%}
|
| 185 |
+
{%- set ns_tr.mm = ns_tr.mm + '<|image|>' -%}
|
| 186 |
+
{%- elif part.get('type') in ['audio', 'input_audio'] -%}
|
| 187 |
+
{%- set ns_tr.mm = ns_tr.mm + '<|audio|>' -%}
|
| 188 |
+
{%- elif part.get('type') == 'video' -%}
|
| 189 |
+
{%- set ns_tr.mm = ns_tr.mm + '<|video|>' -%}
|
| 190 |
+
{%- endif -%}
|
| 191 |
+
{%- endfor -%}
|
| 192 |
+
{{- 'response:' + tool_name + '{value:' + format_argument(ns_tr.txt, escape_keys=False) + '}' -}}
|
| 193 |
+
{{- ns_tr.mm -}}
|
| 194 |
{%- else -%}
|
| 195 |
{{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
|
| 196 |
{%- endif -%}
|
|
|
|
| 309 |
{%- endfor -%}
|
| 310 |
{#- Handle content as string or content-parts array -#}
|
| 311 |
{%- set tool_body = follow.get('content') -%}
|
| 312 |
+
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
{%- set ns_tr_out.flag = true -%}
|
| 314 |
{%- set ns.prev_message_type = 'tool_response' -%}
|
| 315 |
{%- endif -%}
|