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, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForImageTextToText.from_pretrained("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?"} ] }, ] 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
- 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
Preserve anyOf/$ref/$defs in Gemma tool declarations
#89
by sigjhl - opened
- chat_template.jinja +24 -8
chat_template.jinja
CHANGED
|
@@ -11,12 +11,20 @@
|
|
| 11 |
description:<|"|>{{ value['description'] }}<|"|>
|
| 12 |
{%- set add_comma = true -%}
|
| 13 |
{%- endif -%}
|
| 14 |
-
{%- if value['
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
{%- if value['enum'] -%}
|
| 16 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 17 |
enum:{{ format_argument(value['enum']) }}
|
| 18 |
{%- endif -%}
|
| 19 |
-
{%- elif value['type'] | upper == 'ARRAY' -%}
|
| 20 |
{%- if value['items'] is mapping and value['items'] -%}
|
| 21 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 22 |
items:{
|
|
@@ -52,11 +60,11 @@
|
|
| 52 |
}
|
| 53 |
{%- endif -%}
|
| 54 |
{%- endif -%}
|
| 55 |
-
{%- if value['nullable'] %}
|
| 56 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 57 |
nullable:true
|
| 58 |
{%- endif -%}
|
| 59 |
-
{%- if value['type'] | upper == 'OBJECT' -%}
|
| 60 |
{%- if value['properties'] is defined and value['properties'] is mapping -%}
|
| 61 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 62 |
properties:{
|
|
@@ -68,7 +76,7 @@
|
|
| 68 |
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
|
| 69 |
}
|
| 70 |
{%- endif -%}
|
| 71 |
-
{%- if value['required'] -%}
|
| 72 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 73 |
required:[
|
| 74 |
{%- for item in value['required'] | default([]) -%}
|
|
@@ -78,8 +86,11 @@
|
|
| 78 |
]
|
| 79 |
{%- endif -%}
|
| 80 |
{%- endif -%}
|
| 81 |
-
{%- if
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
{%- endif -%}
|
| 84 |
{%- endfor -%}
|
| 85 |
{%- endmacro -%}
|
|
@@ -91,6 +102,9 @@
|
|
| 91 |
{%- if params['properties'] -%}
|
| 92 |
properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
|
| 93 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
| 94 |
{%- if params['required'] -%}
|
| 95 |
required:[
|
| 96 |
{%- for item in params['required'] -%}
|
|
@@ -120,6 +134,8 @@
|
|
| 120 |
{{- '<|"|>' + argument + '<|"|>' -}}
|
| 121 |
{%- elif argument is boolean -%}
|
| 122 |
{{- 'true' if argument else 'false' -}}
|
|
|
|
|
|
|
| 123 |
{%- elif argument is mapping -%}
|
| 124 |
{{- '{' -}}
|
| 125 |
{%- set ns = namespace(found_first=false) -%}
|
|
@@ -351,4 +367,4 @@
|
|
| 351 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 352 |
{%- endif -%}
|
| 353 |
{%- endif -%}
|
| 354 |
-
{%- endif -%}
|
|
|
|
| 11 |
description:<|"|>{{ value['description'] }}<|"|>
|
| 12 |
{%- set add_comma = true -%}
|
| 13 |
{%- endif -%}
|
| 14 |
+
{%- if value['anyOf'] is defined and value['anyOf'] -%}
|
| 15 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 16 |
+
anyOf:{{ format_argument(value['anyOf'], escape_keys=false) }}
|
| 17 |
+
{%- endif -%}
|
| 18 |
+
{%- if value['$ref'] is defined and value['$ref'] -%}
|
| 19 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 20 |
+
$ref:<|"|>{{ value['$ref'] }}<|"|>
|
| 21 |
+
{%- endif -%}
|
| 22 |
+
{%- if value['type'] is defined and value['type'] and value['type'] | upper == 'STRING' -%}
|
| 23 |
{%- if value['enum'] -%}
|
| 24 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 25 |
enum:{{ format_argument(value['enum']) }}
|
| 26 |
{%- endif -%}
|
| 27 |
+
{%- elif value['type'] is defined and value['type'] and value['type'] | upper == 'ARRAY' -%}
|
| 28 |
{%- if value['items'] is mapping and value['items'] -%}
|
| 29 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 30 |
items:{
|
|
|
|
| 60 |
}
|
| 61 |
{%- endif -%}
|
| 62 |
{%- endif -%}
|
| 63 |
+
{%- if value['nullable'] is defined and value['nullable'] %}
|
| 64 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 65 |
nullable:true
|
| 66 |
{%- endif -%}
|
| 67 |
+
{%- if value['type'] is defined and value['type'] and value['type'] | upper == 'OBJECT' -%}
|
| 68 |
{%- if value['properties'] is defined and value['properties'] is mapping -%}
|
| 69 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 70 |
properties:{
|
|
|
|
| 76 |
{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
|
| 77 |
}
|
| 78 |
{%- endif -%}
|
| 79 |
+
{%- if value['required'] is defined and value['required'] -%}
|
| 80 |
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 81 |
required:[
|
| 82 |
{%- for item in value['required'] | default([]) -%}
|
|
|
|
| 86 |
]
|
| 87 |
{%- endif -%}
|
| 88 |
{%- endif -%}
|
| 89 |
+
{%- if value['type'] is defined and value['type'] -%}
|
| 90 |
+
{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
|
| 91 |
+
type:<|"|>{{ value['type'] | upper }}<|"|>
|
| 92 |
+
{%- endif -%}
|
| 93 |
+
}
|
| 94 |
{%- endif -%}
|
| 95 |
{%- endfor -%}
|
| 96 |
{%- endmacro -%}
|
|
|
|
| 102 |
{%- if params['properties'] -%}
|
| 103 |
properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
|
| 104 |
{%- endif -%}
|
| 105 |
+
{%- if params['$defs'] is defined and params['$defs'] -%}
|
| 106 |
+
$defs:{{ format_argument(params['$defs'], escape_keys=false) }},
|
| 107 |
+
{%- endif -%}
|
| 108 |
{%- if params['required'] -%}
|
| 109 |
required:[
|
| 110 |
{%- for item in params['required'] -%}
|
|
|
|
| 134 |
{{- '<|"|>' + argument + '<|"|>' -}}
|
| 135 |
{%- elif argument is boolean -%}
|
| 136 |
{{- 'true' if argument else 'false' -}}
|
| 137 |
+
{%- elif argument is none -%}
|
| 138 |
+
{{- 'null' -}}
|
| 139 |
{%- elif argument is mapping -%}
|
| 140 |
{{- '{' -}}
|
| 141 |
{%- set ns = namespace(found_first=false) -%}
|
|
|
|
| 367 |
{{- '<|channel>thought\n<channel|>' -}}
|
| 368 |
{%- endif -%}
|
| 369 |
{%- endif -%}
|
| 370 |
+
{%- endif -%}
|