5分钟搭建本地AI助手:Gemma 4 完全部署指南
在自己的电脑上运行一个媲美 GPT 的 AI 模型,不需要联网,数据完全私有。这不是科幻,这是 Google 刚刚发布的 Gemma 4。
重磅更新:Gemma 4 来了
2026年4月2日,Google DeepMind 发布了 Gemma 4 —— 这是他们迄今最强大的开源模型家族。
亮点是什么?
- Arena AI 榜单第3名:31B 版本在全球开源模型中排名第三,26B 版本排名第六
- 以小博大:Gemma 4 的性能超过了参数量 20 倍于它的模型
- Apache 2.0 开源:完全免费,可商用,无限制
- 多模态:支持文本、图片、音频(边缘模型)
- 超长上下文:最高 256K token
更重要的是:你可以在自己的电脑上运行它。
为什么要在本地跑大模型?
2026年,AI 大模型已经成为生产力工具标配。但每次使用 ChatGPT、Claude,你都在把数据发给云端。企业机密、个人隐私、代码资产… 全部暴露在第三方服务器。
更现实的问题是:云服务不稳定。高峰期限流、API 调用限制、网络波动… 当你需要它的时候,它可能正在"思考中"。
本地部署大模型,意味着:
- ✅ 数据完全私有 — 你的对话、代码、文档永远不离开你的硬盘
- ✅ 零网络依赖 — 断网也能用,高铁上、飞机上都能写代码
- ✅ 无 API 费用 — 一次部署,永久免费使用
- ✅ 响应更快 — 本地推理,毫秒级延迟
认识 Gemma 4:基于 Gemini 3 技术打造
Gemma 4 是 Google DeepMind 基于 Gemini 3 同源技术打造的开源模型。简单说:它继承了 Google 最前沿的 AI 能力,但完全开放给你。
核心能力
| 特性 | 说明 |
|---|---|
| 深度推理 | 支持多步骤规划、复杂逻辑推理 |
| Agent 能力 | 原生支持函数调用、JSON 输出、系统指令 |
| 多模态 | 全系列支持图片理解,E2B/E4B 还支持音频 |
| 超长上下文 | 边缘模型 128K,大模型 256K |
| 多语言 | 原生支持 140+ 种语言 |
| 代码生成 | 高质量离线代码助手 |
四种尺寸,覆盖所有场景
| 模型 | 参数量 | 激活参数 | 上下文 | 适用场景 |
|---|---|---|---|---|
| E2B | 2.3B effective | 2B | 128K | 手机、IoT、Raspberry Pi |
| E4B | 4.5B effective | 4B | 128K | 手机、平板、笔记本 |
| 26B MoE | 25.2B total | 3.8B | 256K | 工作站、游戏显卡 |
| 31B Dense | 30.7B | 30.7B | 256K | 旗舰工作站、专业开发 |
性能基准测试
Gemma 4 在多项基准测试中表现出色:
| 模型 | MMLU Pro | AIME 2026 | LiveCodeBench | Codeforces ELO |
|---|---|---|---|---|
| Gemma 4 31B | 85.2% | 89.2% | 80.0% | 2150 |
| Gemma 4 26B | 82.6% | 88.3% | 77.1% | 1718 |
| Gemma 4 E4B | 69.4% | 42.5% | 52.0% | 940 |
| Gemma 4 E2B | 60.0% | 37.5% | 44.0% | 633 |
| Gemma 3 27B | 67.6% | 20.8% | 29.1% | 110 |
结论:Gemma 4 全面碾压上一代,31B 版本的数学推理能力(AIME 89.2%)甚至接近闭源模型水平。
选型指南:哪个版本适合你?
按硬件配置选
| 你的配置 | 推荐模型 | 内存需求 | 特点 |
|---|---|---|---|
| 4GB 内存,无独显 | gemma4:e2b | 2GB+ | 手机端可运行 |
| 8GB 内存,核显 | gemma4:e4b | 4GB+ | 笔记本首选,支持音频 |
| 16GB 内存 + 8GB 显存 | gemma4:26b | 10GB+ | 性价比之王,MoE 架构快 |
| 32GB 内存 + 16GB 显存 | gemma4:31b | 20GB+ | 旗舰性能,最强推理 |
我的推荐
- 普通笔记本用户 →
gemma4:e4b(4B 参数,支持多模态+音频) - 有独立显卡的开发者 →
gemma4:26b(MoE 架构,速度快,质量高) - 追求极致性能 →
gemma4:31b(Dense 模型,最佳质量)
开始部署:Ollama 一键安装
Ollama 是目前最简单的本地大模型运行工具,支持 macOS、Windows、Linux。
第一步:安装 Ollama
macOS / Windows:
访问 ollama.com/download,下载对应版本,双击安装。
Linux(一行命令):
curl -fsSL https://ollama.com/install.sh | sh
安装完成后,验证:
ollama --version
第二步:下载 Gemma 4 模型
打开终端,运行:
# 推荐:E4B 多模态版本(笔记本首选)
ollama pull gemma4:e4b
# 或者:26B MoE 版本(有独显选这个)
ollama pull gemma4:26b
# 旗舰:31B Dense 版本(追求极致性能)
ollama pull gemma4:31b
第三步:开始对话
ollama run gemma4:e4b
你会看到类似界面:
>>> Hi, who are you?
I'm Gemma 4, an open-weights AI assistant created by Google DeepMind.
I'm built on the same technology as Gemini 3, and I can help with
reasoning, coding, and understanding images. How can I assist you today?
恭喜!你已经在本地运行 Google 最新的 AI 大模型了。 🎉
实战:让 Gemma 4 帮你写代码
示例 1:生成 Python 函数
>>> Write a Python function to check if a number is prime
Gemma 4 会输出:
def is_prime(n: int) -> bool:
"""Check if a number is prime."""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# Test cases
print(is_prime(29)) # True
print(is_prime(15)) # False
示例 2:复杂推理(Gemma 4 强项)
>>> A bat and ball cost $1.10 total. The bat costs $1 more than the ball.
How much does the ball cost? Think step by step.
Gemma 4 会展示推理过程:
Let me think through this carefully:
1. Let the ball cost x dollars
2. Then the bat costs x + $1
3. Total: x + (x + $1) = $1.10
4. So: 2x + $1 = $1.10
5. 2x = $0.10
6. x = $0.05
The ball costs $0.05 (5 cents).
示例 3:代码解释
>>> Explain what this code does:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
进阶:用 Python 调用本地 Gemma 4
Ollama 提供了 REST API,你可以用 Python 调用本地模型。
安装 Python 库
pip install ollama
代码示例
import ollama
# 简单文本生成
response = ollama.generate(
model='gemma4:e4b',
prompt='用中文解释什么是递归'
)
print(response['response'])
# 多轮对话(带系统提示)
messages = [
{'role': 'system', 'content': '你是一个专业的 Python 编程助手'},
{'role': 'user', 'content': '如何读取 JSON 文件?'}
]
chat_response = ollama.chat(
model='gemma4:e4b',
messages=messages
)
print(chat_response['message']['content'])
# 流式输出(逐字显示)
stream = ollama.chat(
model='gemma4:e4b',
messages=[{'role': 'user', 'content': '写一个快速排序'}],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
直接调用 REST API
curl http://localhost:11434/api/generate -d '{
"model": "gemma4:e4b",
"prompt": "解释 Docker 容器的原理",
"stream": false
}'
进阶:搭建可视化界面(ChatGPT 体验)
命令行对话不够友好?可以部署 Open WebUI,获得类似 ChatGPT 的网页界面。
Docker 一键部署
docker run -d -p 3000:8080 \
--add-host=host.docker.internal:host-gateway \
-v open-webui:/app/backend/data \
--name open-webui \
ghcr.io/open-webui/open-webui:main
然后访问 http://localhost:3000,你就有了一个本地的 ChatGPT 界面。
多模态能力:让 AI 看懂图片
Gemma 4 全系列支持图片理解。通过 API 可以实现:
import ollama
import base64
# 读取图片
with open("screenshot.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode()
response = ollama.chat(
model='gemma4:e4b',
messages=[{
'role': 'user',
'content': '这张图片里有什么?',
'images': [image_data]
}]
)
print(response['message']['content'])
变量分辨率支持
Gemma 4 支持可变图片分辨率,你可以根据任务调整:
- 低分辨率(70-140 tokens):分类、标签、视频理解
- 高分辨率(560-1120 tokens):OCR、文档解析、小字识别
性能优化:让模型跑得更快
1. 启用 GPU 加速
如果你有 NVIDIA 显卡,Ollama 会自动检测并使用 GPU,速度提升 3-10 倍。
nvidia-smi # 检查 GPU 状态
2. 选择 MoE 模型
26B MoE 版本每次只激活 3.8B 参数,速度比 31B Dense 快很多,但质量接近。
ollama run gemma4:26b
3. 调整采样参数
Gemma 4 推荐的采样配置:
response = ollama.chat(
model='gemma4:e4b',
messages=[{'role': 'user', 'content': '你好'}],
options={
'temperature': 1.0,
'top_p': 0.95,
'top_k': 64
}
)
常见问题
Q: 8GB 内存的笔记本能跑吗?
可以。选择 gemma4:e2b 或 gemma4:e4b,4GB 内存就能流畅运行。
Q: Apple Silicon (M1/M2/M3/M4) 支持吗?
完美支持。Ollama 会自动使用 Apple 的 Metal 框架进行 GPU 加速。
Q: 能用来做翻译吗?
可以。Gemma 4 原生支持 140+ 种语言,中英互译效果很好。
Q: 和 Llama 4 相比怎么样?
Gemma 4 31B 在 Arena AI 榜单上排名第三,超过了更大参数的竞争对手。同级别对比中,Gemma 4 的推理和代码能力领先。
Q: 商业用途有限制吗?
没有! Gemma 4 采用 Apache 2.0 许可证,完全开源,可商用,无任何限制。这是 Google 对开发者社区的诚意。
Q: 边缘模型能跑在手机上吗?
可以。E2B 和 E4B 专为移动设备优化,可以在 Android 手机、Raspberry Pi、NVIDIA Jetson 上离线运行。
写在最后
五年前,在自己的电脑上运行一个"像样"的 AI 模型是不可想象的。今天,Google 给了你一个 Apache 2.0 开源、Arena 榜单前三、支持 256K 上下文 的模型。
Gemma 4 的意义不只是"免费",而是控制权。你的数据、你的隐私、你的 AI。
现在就试试吧:
# 三行命令,5分钟搞定
curl -fsSL https://ollama.com/install.sh | sh
ollama pull gemma4:e4b
ollama run gemma4:e4b
有问题欢迎留言讨论 💬
参考资料:
- Google DeepMind Gemma 4 官方博客
- Ollama Gemma 4 模型库
更多推荐
所有评论(0)