基于2026年最新技术发展,安卓手机部署Hermes Agent和Gemma 4有两种主流方案。下面提供详细的操作指南:

| 场景 | 推荐配置 | 支持模型 | 运行效果 |
|---|---|---|---|
| 入门体验 | 骁龙7+ Gen3/8GB RAM | Gemma 4 E2B | 基础对话,响应较慢 |
| 流畅使用 | 骁龙8 Gen3/12GB RAM | Gemma 4 E2B + Hermes Lite | 正常速度,支持基础Agent功能 |
| 极致体验 | 骁龙8 Gen4/16GB RAM | Gemma 4 E4B + 完整Hermes | 接近桌面体验,支持复杂任务 |
安装步骤:
|
1 2 3 4 5 6 7 |
# 无需代码,纯APP操作 1. 在应用商店搜索 "Google AI Edge Gallery" 2. 下载安装(谷歌官方出品,完全免费) 3. 打开应用,进入"模型库" 4. 搜索"Gemma4",选择"E2B"版本(约2.5GB) 5. 点击下载,等待10-15分钟 6. 下载完成后自动激活,断网也可使用 |
使用技巧:
配置方法:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 在Termux中创建Python脚本 pip install requests python-telegram-bot # hermes_mobile.py import requests import json def call_gemma4(prompt): # 通过本地API调用Gemma 4 response = requests.post( "http://localhost:8080/generate", json={ "model": "gemma4:e2b", "prompt": prompt, "temperature": 0.7 } ) return response.json()["response"] def hermes_agent_task(task): # Hermes Agent逻辑处理 system_prompt = """ 你是一个运行在安卓手机上的AI助手,需要帮助用户完成任务。 当前设备:Android手机,内存有限,需要高效运行。 请提供简洁、实用的解决方案。 """ full_prompt = f"{system_prompt}\n任务:{task}" result = call_gemma4(full_prompt) return result |
完整安装流程:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 1. 安装Termux(必须从F-Droid下载) - 访问 https://f-droid.org/packages/com.termux/ - 下载最新版Termux(Google Play版本已停止更新)
# 2. 基础环境配置 pkg update -y pkg upgrade -y pkg install git python nodejs rust clang make -y
# 3. 安装存储访问权限 termux-setup-storage
# 4. 配置SSH(方便远程管理) pkg install openssh -y ssh-keygen -A sv-enable sshd |
安装命令:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 1. 安装Ollama(Android专用版) pkg install -y ollama
# 2. 启动Ollama服务 ollama serve &
# 3. 下载Gemma 4轻量版 ollama pull gemma4:e2b # 2.1GB,推荐手机使用 # 或 ollama pull gemma4:e4b # 4.3GB,旗舰手机可用
# 4. 验证安装 ollama list # 应显示:gemma4:e2b (或e4b) |
详细步骤:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 1. 克隆Hermes Agent仓库 git clone https://github.com/NousResearch/hermes-agent.git cd hermes-agent
# 2. 创建虚拟环境(节省空间) python -m venv .venv source .venv/bin/activate
# 3. 安装依赖(精简版) pip install --upgrade pip pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu pip install -r requirements.txt --no-cache-dir
# 4. 配置Hermes Agent cat > config.yaml << EOF model: provider: "ollama" endpoint: "http://localhost:11434/api/generate" model_name: "gemma4:e2b" timeout: 120
memory: type: "sqlite" path: "/data/data/com.termux/files/home/hermes_agent/memory.db" max_entries: 100
skills: directory: "./skills" auto_learn: false # 手机端建议关闭自动学习 max_skills: 20 EOF
# 5. 启动Hermes Agent python -m hermes_agent --config config.yaml --port 8080 & |
|
1 |
手机APP/Telegram Bot → Hermes Agent API (8080端口) → Ollama (11434端口) → Gemma 4 |
安装 Bot:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
pip install python-telegram-bot # 创建bot.py import logging from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.message.reply_text('Hermes Agent已启动!发送任务给我吧。') async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): user_message = update.message.text # 调用Hermes Agent API response = requests.post( "http://localhost:8080/execute", json={"task": user_message} ) await update.message.reply_text(response.json()["result"]) # 设置Bot application = Application.builder().token("YOUR_BOT_TOKEN").build() application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) application.run_polling() |
启动Bot:
|
1 |
nohup python bot.py & |
简易HTML界面:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 安装Flask pip install flask # 创建web_ui.py from flask import Flask, request, render_template_string import requests app = Flask(__name__) HTML_TEMPLATE = """ <!DOCTYPE html> <html> <head> <title>Hermes Agent Mobile</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-family: Arial, sans-serif; padding: 20px; } #chat { height: 300px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; margin-bottom: 10px; } #input { width: 100%; padding: 10px; box-sizing: border-box; } button { width: 100%; padding: 10px; margin-top: 10px; } </style> </head> <body> <div id="chat" id="chat"></div> <input type="text" id="input" placeholder="输入任务..."> <button onclick="sendMessage()">发送</button> <script> function sendMessage() { const input = document.getElementById('input'); const message = input.value; if (!message) return; // 添加用户消息 document.getElementById('chat').innerHTML += `<div><strong>你:</strong> ${message}</div>`; input.value = ''; // 调用API fetch('/api/chat', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({message: message}) }) .then(response => response.json()) .then(data => { document.getElementById('chat').innerHTML += `<div><strong>Hermes:</strong> ${data.response}</div>`; document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight; }); } </script> </body> </html> """ @app.route('/') def home(): return render_template_string(HTML_TEMPLATE) @app.route('/api/chat', methods=['POST']) def chat(): data = request.json response = requests.post( "http://localhost:8080/execute", json={"task": data["message"]} ) return {"response": response.json()["result"]} if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, threaded=True) |
|
1 2 3 4 5 6 7 8 9 10 |
# 在Termux中设置内存限制 echo "Max memory per process: 2GB" > ~/.bashrc ulimit -v 2097152 # 2GB限制 # 清理缓存脚本(保存为clean_cache.sh) #!/data/data/com.termux/files/usr/bin/bash echo "清理缓存..." pkg clean rm -rf ~/.cache/pip/* rm -rf ~/.cache/ollama/* echo "缓存清理完成!" |
|
1 2 3 4 5 6 7 8 9 |
# 创建量化版本 ollama create gemma4-e2b-q4 --modelfile - <<EOF FROM gemma4:e2b QUANTIZE 4 PARAMETER num_ctx 4096 EOF
# 更新Hermes配置 sed -i 's/gemma4:e2b/gemma4-e2b-q4/g' config.yaml |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 创建服务管理脚本(save as services.sh) #!/data/data/com.termux/files/usr/bin/bash
start_services() { echo "启动Ollama服务..." ollama serve & sleep 5
echo "启动Hermes Agent..." cd ~/hermes-agent source .venv/bin/activate nohup python -m hermes_agent --config config.yaml --port 8080 > /dev/null 2>&1 &
echo "启动Telegram Bot..." cd ~ nohup python bot.py > /dev/null 2>&1 &
echo "所有服务已启动!" }
stop_services() { echo "停止所有服务..." pkill -f ollama pkill -f hermes_agent pkill -f bot.py echo "服务已停止" }
case "$1" in start) start_services ;; stop) stop_services ;; restart) stop_services; start_services ;; *) echo "用法: $0 {start|stop|restart}" ;; esac
# 赋予执行权限 chmod +x services.sh
# 启动服务 ./services.sh start |
|
1 2 3 4 5 6 |
症状:服务频繁崩溃,OOM错误 解决方案: - 使用Gemma 4 E2B替代E4B - 关闭Hermes的auto_learn功能 - 设置内存限制:ulimit -v 2097152 - 定期清理缓存:./clean_cache.sh |
|
1 2 3 4 5 6 |
症状:模型下载失败,磁盘写满 解决方案: - 将模型存储到外部SD卡: export OLLAMA_MODELS=/storage/XXXX-XXXX/ollama_models - 清理旧模型:ollama rm gemma4:old_version - 使用量化模型:gemma4-e2b-q4 |
|
1 2 3 4 5 |
优化建议: - 限制CPU核心数:taskset -c 0-3 python bot.py - 降低模型温度:temperature: 0.3 - 添加休眠机制:30分钟无活动自动暂停服务 - 使用后台服务管理:./services.sh stop(不用时) |
|
1 2 3 4 5 6 7 8 9 10 11 |
# 测试Gemma 4 curl http://localhost:11434/api/generate -d '{ "model": "gemma4:e2b", "prompt": "你好,介绍一下你自己", "stream": false }'
# 测试Hermes Agent curl http://localhost:8080/execute -d '{ "task": "创建一个Python函数来计算斐波那契数列" }' |
|
1 2 3 4 5 6 7 |
# 安装监控工具 pkg install htop sysstat -y
# 实时监控 htop # 查看CPU/内存使用 dstat -tam # 查看系统资源 termux-battery-status # 查看电池状态 |
通过这套方案,你可以在安卓手机上实现完整的Hermes Agent + Gemma 4本地部署。新手建议从方案一开始,使用Google AI Edge Gallery体验基础功能;开发者推荐方案二,通过Termux获得完整的Agent能力和自定义选项。记住,手机端部署需要合理管理资源,建议在高性能设备上运行以获得最佳体验。