G4
ガイド
Gemma 4 + Ollama 使い方ガイド
OllamaでGemma 4を実行するための完全ガイド——インストール、モデルのプル、REST APIの使用、OpenAI SDKによるPython連携、カスタムModelfileの作成など。
Ollama REST API OpenAI SDK Modelfile
1. Ollama のインストール
# Linux / macOS
curl -fsSL https://ollama.com/install.sh | sh
# macOS (Homebrew)
brew install ollama
# Windows: download .exe from ollama.comインストール後、Ollamaはバックグラウンドサービスとして実行されます。以下で確認: ollama --version.
2. Gemma 4 モデルのプル
# Pull the default 31B model
ollama pull gemma4
# Pull specific variants
ollama pull gemma4:e4b # Edge 4B — best for 8 GB VRAM
ollama pull gemma4:e2b # Edge 2B — runs on 4 GB VRAM or CPU
# List downloaded models
ollama listどのバリアントをプルするか
| Tag | VRAM | Speed |
|---|---|---|
gemma4:e2b | ~4 GB | Fastest |
gemma4:e4b | ~6 GB | Fast |
gemma4 | ~18 GB | Best quality |
OllamaはGGUF量化モデルを使用します(デフォルトはQ4_K_M)。
3. CLI から実行
# Interactive chat in terminal
ollama run gemma4
# Single prompt (non-interactive)
ollama run gemma4 "Explain the MoE architecture in Gemma 4"
# With a custom system prompt
ollama run gemma4 --system "You are a Python expert." "Write a FastAPI hello world"インタラクティブモードでは /bye to exit or /help to see commands.
4. REST API
Ollamaは以下でREST APIを提供します: http://localhost:11434:
# The Ollama REST API runs at localhost:11434
# Generate completion
curl http://localhost:11434/api/generate -d '{
"model": "gemma4",
"prompt": "Why is Gemma 4 good for local deployment?",
"stream": false
}'
# Chat completions
curl http://localhost:11434/api/chat -d '{
"model": "gemma4",
"messages": [
{"role": "user", "content": "Hello!"}
]
}'POST /api/generate
Single completion
POST /api/chat
Multi-turn conversation
GET /api/tags
List installed models
5. OpenAI Python SDK との使用
Ollamaは以下でOpenAI APIフォーマットをサポート: /v1/ — use your existing OpenAI code with zero changes:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="ollama" # required but unused
)
response = client.chat.completions.create(
model="gemma4",
messages=[{"role": "user", "content": "What is Gemma 4?"}]
)
print(response.choices[0].message.content)6. ストリーミングレスポンス
import requests
import json
response = requests.post(
"http://localhost:11434/api/chat",
json={
"model": "gemma4",
"messages": [{"role": "user", "content": "Tell me a story"}],
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
chunk = json.loads(line)
print(chunk["message"]["content"], end="", flush=True)7. カスタム Modelfile
Ollama ベースモデルから
# Create a Modelfile
FROM gemma4:e4b
SYSTEM "You are a helpful coding assistant specializing in Python."
PARAMETER temperature 0.7
PARAMETER top_p 0.9# Build and run your custom model
ollama create mygemma -f Modelfile
ollama run mygemmaローカル GGUF ファイルから
# Use a local GGUF file
FROM /path/to/your/gemma4-q4_k_m.gguf
SYSTEM "You are a helpful assistant."Hugging FaceのbartowskiなどのコミュニティリポジトリからGGUFファイルをダウンロードしてください。
ヒントと一般的な問題
パフォーマンスのヒント
- Set
OLLAMA_NUM_GPU=1to force GPU offloading - Use
OLLAMA_NUM_PARALLEL=4for concurrent requests - Keep context short — 2048 tokens is enough for most tasks
- E4B with Q4_K_M is the best quality/speed ratio under 8 GB
ネットワークにOllamaを公開
OLLAMA_HOST=0.0.0.0 ollama serve他のデバイスからアクセス: http://<your-ip>:11434. Add authentication with a reverse proxy (nginx/Caddy) for production.