はじめに
2026年4月2日、Google は Gemma 4 をリリースしました。Gemini 3 の技術をベースにしたオープンウェイトモデルで、Apache 2.0 ライセンスで公開されています。
Gemma 4 の注目ポイントは次の3つです。
- マルチモーダル対応 — テキスト・画像・音声・動画を入力できる
- ネイティブエージェント機能 — Function Calling・構造化 JSON 出力をモデルが直接サポート
- Apache 2.0 ライセンス — 商用利用も含め制限なし
この記事では、Gemma 4 の概要を押さえたうえで、Ollama・HuggingFace Transformers で実際に動かす方法と Function Calling の実践例を紹介します。
Gemma 4 の概要
Gemma 4 は Gemini 3 の技術を継承したオープンウェイトモデルファミリーです。31B Dense モデルは Arena AI テキストリーダーボードでオープンモデル第3位にランクインしています。
モデル一覧
4つのサイズが用意されており、すべて Base と Instruction-tuned(IT)の2種類があります。
モデル | パラメータ | アクティブパラメータ | コンテキスト長 | 音声対応 | 主な用途 |
|---|---|---|---|---|---|
E2B | 2.3B(実効値) | 2.3B | 128K | ○ | スマートフォン・IoT |
E4B | 4.5B(実効値) | 4.5B | 128K | ○ | エッジデバイス・ノート PC |
26B A4B(MoE) | 26B | 3.8B | 256K | × | サーバー(低レイテンシ) |
31B Dense | 31B | 31B | 256K | × | 最高品質・ファインチューニング |
E = Effective(実効パラメータ数)、A = Active(MoE でのアクティブパラメータ数)を意味します。
### ベンチマーク
Gemma 3 27B からの大幅な性能向上が確認できます。
ベンチマーク | Gemma 4 26B MoE | Gemma 4 31B Dense | Gemma 3 27B |
|---|---|---|---|
AIME 2026(数学) | 88.3% | 89.2% | 20.8%* |
LiveCodeBench(コーディング) | 77.1% | — | 29.1%* |
GPQA Diamond(大学院レベル科学) | 82.3% | — | — |
Codeforces ELO | — | 2150 | — |
*Gemma 3 は thinking モードなしの数値
主な特徴
- マルチモーダル入力: テキスト・画像・動画(全モデル)、音声(E2B / E4B のみ)
- ハイブリッドアテンション: ローカルスライディングウィンドウ + グローバルフルアテンションの交互配置
- 多言語対応: 140 以上の言語で事前学習、35 以上の言語を公式サポート
- ネイティブ推論モード:
<think>タグによるステップバイステップの思考が可能
Apache 2.0 ライセンスへの変更
Gemma 4 の重要な変更点のひとつが、カスタム Gemma License から Apache 2.0 への移行です。
項目 | 旧 Gemma License | Apache 2.0 |
|---|---|---|
商用利用 | 条件付き(有害利用制限あり) | 制限なし |
改変・再配布 | カスタム条項に従う | 自由 |
法的解釈コスト | 独自条項の確認が必要 | 広く理解されたライセンス |
Qwen、Mistral など主要なオープンモデルと同じライセンスになったことで、プロダクション導入のハードルが大きく下がりました。
Gemma 4 を動かしてみる
Google AI Studio で試す
もっとも手軽な方法は Google AI Studio です。ブラウザだけで 31B Dense や 26B MoE を試せます。セットアップ不要で、画像入力やシステムプロンプトの設定も GUI から行えます。
Ollama でローカル実行
Ollama を使えば、ワンコマンドでローカル実行できます。
# Gemma 4 をダウンロード & 実行(デフォルトは 26B MoE の量子化版)
ollama run gemma4
特定のモデルサイズを指定する場合は、タグを付けます。
# E4B(エッジ向け軽量モデル)
ollama run gemma4:e4b
# 31B Dense(最高品質)
ollama run gemma4:31b
Ollama でマルチモーダル(画像入力)
画像を含むプロンプトも送れます。
# 画像の内容を説明させる
ollama run gemma4 "この画像に何が写っていますか?" --images ./photo.jpg
Ollama を API サーバーとして使う
Ollama は OpenAI 互換の API サーバーとしても動作します。
# サーバーを起動(デフォルトで localhost:11434)
ollama serve
import requests
response = requests.post("http://localhost:11434/api/chat", json={
"model": "gemma4",
"messages": [
{"role": "user", "content": "Pythonでクイックソートを実装してください"}
],
"stream": False
})
print(response.json()["message"]["content"])
HuggingFace Transformers で動かす
HuggingFace Transformers を使えば、Python から細かく制御できます。
テキスト生成
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_id = "google/gemma-4-E4B-it"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
)
messages = [
{"role": "user", "content": "Rustの所有権システムを簡潔に説明してください"},
]
inputs = tokenizer.apply_chat_template(
messages, return_tensors="pt", add_generation_prompt=True
).to(model.device)
outputs = model.generate(inputs, max_new_tokens=512)
response = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
print(response)
マルチモーダル(画像 + テキスト)
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
from PIL import Image
model_id = "google/gemma-4-E4B-it"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
)
image = Image.open("diagram.png")
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": "この図の内容を説明してください"},
],
},
]
inputs = processor.apply_chat_template(
messages, return_tensors="pt", add_generation_prompt=True
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=512)
response = processor.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
print(response)
VRAM 要件の目安
モデル | FP16 | Q4 量子化 |
|---|---|---|
E2B | ~5 GB | ~2 GB |
E4B | ~9 GB | ~4 GB |
26B MoE | ~52 GB | ~16 GB |
31B Dense | ~62 GB | ~20 GB |
E4B は 8 GB の VRAM があれば量子化版で動作するため、ノート PC でも試しやすいモデルです。
## エージェント機能を試す — Function Calling
Gemma 4 はネイティブで Function Calling(ツール呼び出し)をサポートしています。外部ツールの定義をプロンプトに渡すと、モデルが適切なタイミングでツールを呼び出し、結果を組み込んで回答を生成します。
仕組み
- ユーザーがツール定義とプロンプトをモデルに送る
- モデルがツール呼び出しを JSON 形式で返す
- アプリケーション側でツールを実行し、結果をモデルに返す
- モデルがツールの結果を踏まえて最終回答を生成する
実践例:天気取得ツール
以下は、天気を取得するツールを Gemma 4 に使わせる例です。
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json
model_id = "google/gemma-4-E4B-it"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
)
# ツール定義
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "指定された都市の現在の天気情報を取得する",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "都市名(例: 東京、大阪)",
},
},
"required": ["city"],
},
},
},
]
# ステップ 1: ユーザーの質問を送信
messages = [
{"role": "user", "content": "東京と大阪の天気を教えてください"},
]
inputs = tokenizer.apply_chat_template(
messages,
tools=tools,
return_tensors="pt",
add_generation_prompt=True,
).to(model.device)
outputs = model.generate(inputs, max_new_tokens=256)
response = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
print("モデルの応答(ツール呼び出し):")
print(response)
# => [{"name": "get_weather", "arguments": {"city": "東京"}},
# {"name": "get_weather", "arguments": {"city": "大阪"}}]
# ステップ 2: ツールの実行結果を返す
messages.append({"role": "assistant", "content": response})
messages.append({
"role": "tool",
"content": json.dumps([
{"name": "get_weather", "content": {"city": "東京", "weather": "晴れ", "temp": "22°C"}},
{"name": "get_weather", "content": {"city": "大阪", "weather": "曇り", "temp": "20°C"}},
], ensure_ascii=False),
})
inputs = tokenizer.apply_chat_template(
messages,
tools=tools,
return_tensors="pt",
add_generation_prompt=True,
).to(model.device)
outputs = model.generate(inputs, max_new_tokens=256)
final_response = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
print("最終回答:")
print(final_response)
# => 東京は晴れで22°C、大阪は曇りで20°Cです。
構造化 JSON 出力
Function Calling 以外にも、構造化された JSON 出力を直接要求できます。
messages = [
{
"role": "user",
"content": (
"以下の文章から人物情報を抽出して JSON で返してください。\n\n"
"田中太郎(35歳)はエンジニアとして東京で働いています。"
),
},
]
inputs = tokenizer.apply_chat_template(
messages, return_tensors="pt", add_generation_prompt=True
).to(model.device)
outputs = model.generate(inputs, max_new_tokens=256)
response = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
print(response)
# => {"name": "田中太郎", "age": 35, "occupation": "エンジニア", "location": "東京"}
まとめ
- Gemma 4 は Gemini 3 ベースのオープンウェイトモデル。E2B / E4B / 26B MoE / 31B Dense の4サイズ展開
- Apache 2.0 ライセンスに変更され、商用利用を含めた制限がなくなった
- マルチモーダル対応(テキスト・画像・動画・音声)で、幅広い入力を処理できる
- ネイティブ Function Calling により、外部ツール連携やエージェント構築が容易
- 31B Dense は Arena AI リーダーボードでオープンモデル第3位の性能
- Ollama ならワンコマンド、HuggingFace Transformers なら Python から柔軟に利用可能
参考
- Gemma 4: Byte for byte, the most capable open models — Google Blog
- Gemma 4 model card — Google AI for Developers
- Function calling with Gemma 4 — Google AI for Developers
- Bring state-of-the-art agentic skills to the edge with Gemma 4 — Google Developers Blog
- Welcome Gemma 4 — Hugging Face Blog
- google/gemma-4-31B-it — Hugging Face
- google/gemma-4-E4B-it — Hugging Face
- Gemma 4 — Google DeepMind