详细教程如何在边缘硬件上构建完全本地化的 LLM Agent,实现 IoT 设备监控、自然语言交互和自主决策,延迟低于 100ms,无云 API 依赖。
现在大家都在做 AI Agent,但大多数都跑在云上——你把请求发给 OpenAI 或 Anthropic,拿回响应,然后祈祷延迟和成本还能接受。但如果你的 Agent 需要控制物理设备呢?监控工厂传感器?在 100ms 内响应安全摄像头的事件?
基于云的 Agent 每次推理调用会增加 200-800ms 的往返延迟。对对话式聊天机器人来说,这没什么。但对一个监控生产线的自主 Agent 而言,这就是"发现缺陷"和"让它流出"之间的天壤之别。
本指南将介绍如何使用 NeoMind + Ollama 构建完全跑在边缘硬件上的生产级 LLM Agent。无需云 API key,数据不离开你的网络,在 200 美元设备上实现亚 100ms 推理。
读完本指南,你将拥有:
一个本地 LLM Agent,实时监控 IoT 设备遥测数据
通过聊天与 Agent 进行自然语言交互
基于传感器数据模式的自主决策
多层记忆系统,让 Agent 随着时间学习你的环境
零云依赖——全部运行在你的局域网内
硬件:任意 x86_64 或 ARM64 机器,内存 ≥8GB(Raspberry Pi 5、Intel NUC、旧笔记本均可)
操作系统:Linux(Ubuntu 22.04+)、macOS 或 Windows
GPU:可选但推荐。Ollama 对小模型(7B)可在 CPU 上运行,但 13B+ 会受益于 GPU 加速
NeoMind:边缘 AI 平台(处理设备连接、自动化、UI)
Ollama:本地 LLM 运行时(处理模型服务和推理)
curl -fsSL https://ollama.com/install.sh | sh
拉取适合你硬件的模型:
ollama pull llama3.1:8b
curl -fsSL https://raw.githubusercontent.com/camthink-ai/NeoMind/main/scripts/install.sh | sh
编辑 NeoMind 的配置文件(通常在 ~/.config/neomind/config.toml):
[ai]
backend = "ollama"
model = "llama3.1:8b"
api_url = "http://localhost:11434"
max_tokens = 2048
temperature = 0.7
neomind start
在 http://localhost:9375 打开 Web UI,你会看到 AI 聊天面板已准备就绪。
在 Agent 能监控任何东西之前,它需要能与设备通信。NeoMind 开箱即支持 MQTT、BLE 和 Webhook 协议。
如果你还没有物理 IoT 设备,可以使用 NeoMind 的设备模拟器:
neomind device simulate --type temperature-sensor --interval 5s
这会创建一个虚拟温度传感器,每 5 秒向 NeoMind 内置的 MQTT broker 发布一次读数。
对于真实设备,在 NeoMind 中配置 MQTT:
[mqtt]
enabled = true
port = 1883
# 设备连接到 neomind-host:1883
或者使用自动发现功能——插入一个 USB IoT 网关,NeoMind 会检测并注册它。
这就是有趣的部分了。我们不再只是和 AI 聊天,而是创建一个自主监控设备并采取行动的 Agent。
在 NeoMind 的 Web UI 中,导航到 AI → Agents → New Agent:
name: "Temperature Guardian"
schedule: "every 5 minutes"
mission: |
You are monitoring temperature sensors across a building.
Your responsibilities:
1. Check all temperature readings from the last 5 minutes
2. Flag any reading above 28°C or below 16°C as anomalous
3. If a sensor shows 3+ consecutive anomalous readings, alert the operator
4. Log a summary of findings to the knowledge base
tools:
- query_device_metrics
- send_notification
- update_knowledge_base
每隔 5 分钟,NeoMind 的 Agent 运行时:
唤醒 Agent 并注入当前上下文(设备状态、最近历史、知识库条目)
通过 Ollama 运行推理——LLM 分析数据并决定采取什么行动
执行工具调用——Agent 查询特定设备指标、在超出阈值时发送通知、并更新其记忆
进入睡眠直到下一个周期
┌──────────────────────────────────────────────────┐
│ Agent Runtime Loop │
│ │
│ ┌─────────┐ ┌──────────┐ ┌─────────────┐ │
│ │ Wake + │───►│ LLM │───►│ Execute │ │
│ │ Inject │ │ Inference│ │ Tool Calls │ │
│ │ Context │ │ (Ollama) │ │ (typed) │ │
│ └─────────┘ └──────────┘ └──────┬──────┘ │
│ │ │
│ ┌─────────────────────────────────────▼──────┐ │
│ │ Memory Update + Sleep until next cycle │ │
│ └────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────┘
一个关键的设计决策:NeoMind 的 Agent 工具不是基于字符串的函数调用。当 LLM 决定"查询设备温度"时,它会产生一个结构化命令:
// LLM 输出被反序列化为一个类型化的枚举
enum AgentToolCall {
QueryDeviceMetrics { device_id: String, metric: String, since: Duration },
SendNotification { channel: String, message: String, severity: Severity },
UpdateKnowledgeBase { topic: String, content: String },
ControlDevice { device_id: String, action: DeviceAction },
}
// 分发是一个类型安全的 match——没有 eval(),没有 shell exec
match tool_call {
AgentToolCall::QueryDeviceMetrics { device_id, metric, since } => {
let readings = device_manager.query_range(&device_id, &metric, since).await?;
Ok(ToolResult::MetricReadings(readings))
}
// ...
}
这消除了注入攻击、幻觉的命令字符串,以及困扰其他 Agent 框架的脆弱的 JSON-to-shell 管道。
一个无状态的 Agent 只是一个花哨的 cron 任务。NeoMind 的记忆系统给你的 Agent 提供跨会话的持久上下文:

Agent 不需要显式的"保存这个"命令。NeoMind 的记忆引擎从 Agent 交互中自动提取和压缩重要信息:
Agent 观察到: "Sensor-07 连续 3 个周期读数偏高 (29.1°C, 29.4°C, 28.8°C)。
这超过了 28°C 的阈值。"
→ 自动提取到知识库:
"Sensor-07 反复热异常,首次检测于 2026-08-19T14:30Z,
读数持续在 28-30°C,可能表示 3 区 HVAC 问题"
长期运行的 Agent 会积累大量上下文。NeoMind 使用以下方式压缩记忆:
时间去重——将重复的观测合并为时间范围
重要性评分——安全事件优先于常规读数
分层摘要——将每日摘要压缩为每周概览
这使得即使运行数月后,上下文窗口仍然可控。
当自主 Agent 在后台运行时,你也可以和它进行对话:
你: "Sensor-07 的情况怎么样了?"
Agent: "3 区的 Sensor-07 从昨天 14:30 开始就一直在发热。读数持续在 28-30°C,比该区域平均温度 24°C 高出 4-6°C。我已经记录了 12 次异常读数。根据这个模式,这看起来像是 HVAC 性能下降而不是瞬时峰值。需要我创建一个维护工单吗?"
你: "好的,另外接下来 48 小时设置为每小时监控"
Agent: "好的。我已经为 3 区 HVAC 检查创建了维护工单,并安排自己在 8 月 21 日之前每小时检查 sensor-07。如果读数超过 32°C,我会立即提醒你。"
Agent 的回答基于其实际记忆和设备数据——不是幻觉的。NeoMind 通过只将已验证的数据注入 LLM 上下文来确保这一点。
以下是我们 在常见边缘硬件上通过 Ollama 运行 llama3.1:8b 测量的结果:
Agent 周期 = 唤醒 + 上下文注入 + 推理 + 工具执行 + 记忆更新。
作为对比,使用 OpenAI 的 gpt-4o-mini API(云端)的相同 Agent 每次推理调用会增加约 800ms 的网络延迟,加上每 1M 输入 token 0.15 美元的费用。
NeoMind 的 Skill 系统让你无需重新训练即可微调 Agent 行为:
# skills/factory-safety.yaml
name: "Factory Floor Safety Monitor"
trigger: "scheduled:every 2 minutes"
context: |
You are a safety monitor for an industrial facility.
Critical rules (NEVER override):
- If CO2 > 1000ppm in any zone, immediately alert + activate ventilation
- If temperature > 45°C near equipment, trigger emergency shutdown
- If unauthorized motion detected after 22:00, alert security
Normal operations:
- Log all readings to knowledge base
- Flag anomalies (>2σ from 7-day rolling average)
- Generate daily safety summary at 18:00
tools:
- query_device_metrics
- send_notification
- control_device
- update_knowledge_base
Skill 是 YAML + Markdown 文件,Agent 运行时将其注入 LLM 上下文。它们提供护栏和领域知识,无需模型微调。
从一个更小的模型开始——llama3.2:3b 对于监控任务来说足够快,还能为操作系统和 NeoMind 服务留出余量
选择性使用 GPU 卸载——如果你有 GPU,用它来做 Agent 推理,同时让 NeoMind 核心服务跑在 CPU 上
设置内存限制——配置 max_knowledge_entries 和 max_session_history 以防止长期运行部署时的内存膨胀
监控 Agent 周期——NeoMind 的仪表盘显示 Agent 执行时间、token 使用量和工具调用成功率
分层模型——用快速的小模型处理常规监控,复杂分析任务使用更大的模型(或云 API)
┌─────────────────────────────────────────────┐
│ Your Edge Device │
│ │
│ ┌──────────┐ ┌──────────┐ ┌───────────┐ │
│ │ NeoMind │ │ Ollama │ │ IoT │ │
│ │ Platform │──│ LLM │──│ Devices │ │
│ │ │ │ Runtime │ │ (MQTT) │ │
│ └──────────┘ └──────────┘ └───────────┘ │
│ │
│ Everything local. Zero cloud dependency. │
└─────────────────────────────────────────────┘

这就是整个技术栈。两个组件。一台机器。没有 Kubernetes,没有服务网格,没有云账户。
NeoMind: github.com/camthink-ai/NeoMind (Apache 2.0)
快速入门指南: wiki.camthink.ai
让你的 AI Agent 运行在数据所在的地方——边缘。无需云。