G4
가이드
Gemma 4 + Ollama 사용 가이드
Ollama로 Gemma 4를 실행하는 완전한 가이드 — 설치, 모델 pull, 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
# 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어떤 버전을 Pull할까요
| 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/google_gemma-4-E4B-it-GGUF 같은 커뮤니티 저장소에서 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.