nanobot:4,000 行 Python AI 助手,分钟级完成建置

nanobot 是什么?

nabot 是受 Clawdbot 启发的超轻量级个人 AI 助手,但代码量仅占其一小部分——约 4,000 行,相较于 Clawdbot 的 430k+ 行。它基于开源工具和 Python,能在数分钟内在您的笔记本或云 VM 上运行一个即插即用的聊天机器人。

为什么选择 nanobot?

Benefit Details
紧凑 4,000 行干净可读的代码,比其他代理小 99%。
快速 极小占用意味着更低的 RAM/CPU 使用率和快速启动。
即研究就可用 模块化架构(技能、工具、渠道)让扩展或修改变得轻而易举。
灵活部署 兼容 OpenRouter,或通过 vLLM 连接任何本地 LLM,使用简洁的配置文件即可。
多渠道 可在 Telegram 或 WhatsApp 上聊天,甚至支持通过 Groq 进行语音转写。
定时调度 类 Cron 任务让您运行循环任务——非常适合提醒或数据抓取。

架构概览

nanobot/
├─ agent/          # Core loop, memory, skills
├─ skills/         # Built‑in capabilities (github, weather, tmux…)
├─ channels/       # Telegram/WhatsApp integration
├─ cron/           # Scheduler for recurring tasks
├─ bus/            # Message routing
├─ providers/      # OpenAI, OpenRouter, Groq, vLLM
├─ config/         # CLI helpers
└─ cli/            # Command‑line entry points

skill(技能)是一个自包含的模块,可通过提示调用。代理循环将用户查询发送到 LLM,接收工具调用令牌,并将其交给相应的技能。记忆在简单的 SQLite 存储中跨会话持久化。

安装 nanobot

1. 从 PyPI(稳定、快捷)

pip install nanobot-ai

2. 从源码(最新功能)

git clone https://github.com/HKUDS/nanobot.git
cd nanobot
pip install -e .         # editable for development

3. 使用 UV(快速安装)

uv tool install nanobot-ai

快速开始指南

~/.nanobot/config.json 准备一个最小配置文件:

{
  "providers": {
    "openrouter": { "apiKey": "sk-or-xxxx" }
  },
  "agents": {
    "defaults": { "model": "anthropic/claude-opus-4-5" }
  }
}

启动聊天机器人

nanobot onboard          # initializes config & workspace
nanobot agent -m "What is 2+2?"

交互模式:

nanobot agent            # starts a REPL

通过 Telegram 聊天

  1. 通过 @BotFather 创建一个机器人并复制其 token。
  2. config.json 中添加 token 与您的用户 ID:

"channels": {
  "telegram": { "enabled": true, "token": "YOUR_BOT_TOKEN", "allowFrom": ["YOUR_USER_ID"] }
}
3. 运行:

nanobot gateway

现在您发送给机器人的每条消息都会出现在 CLI,反之亦然。

使用 vLLM 部署本地 LLM

# Start a local Llama‑3 model
vllm serve meta-llama/Llama-3.1-8B-Instruct --port 8000
编辑配置:

"providers": {
  "vllm": { "apiKey": "dummy", "apiBase": "http://localhost:8000/v1" }
},
"agents": { "defaults": { "model": "meta-llama/Llama-3.1-8B-Instruct" } }
Run the agent as usual.

Docker 友好式部署

# Build the image
docker build -t nanobot .
# One‑time onboarding
docker run -v ~/.nanobot:/root/.nanobot --rm nanobot onboard
# Run the gateway
docker run -v ~/.nanobot:/root/.nanobot -p 18790:18790 nanobot gateway

保留 ~/.nanobot 可以确保 API 密钥和工作区在容器重启后不丢失。

定时任务 (Cron)

添加每日问候:

nanobot cron add --name "daily" --message "Good morning!" --cron "0 9 * * *"
列出任务:

nanobot cron list
删除任务:

nanobot cron remove <job_id>

扩展 nanobot

  1. skills/ 中创建一个新目录。
  2. 添加一个实现 class Tool:(包含 namedescriptionrun())的 tool.py
  3. skills/__init__.py 的扫描器中注册该技能。
  4. 重启 nanobot gateway —— 新技能即刻可用。

所有技能均为纯 Python,实验非常简便。

贡献

目标是创建一个简洁、易懂的代码库。欢迎你: - 添加新的技能(天气、新闻、GitHub 搜索等) - 实现通过 Groq Whisper 的语音转写 - 扩展多模态支持(图像、语音) - 提升推理与规划能力 - 加入更多渠道集成(Discord、Slack、邮件)

欢迎提交 Pull Request —— 只需遵循现有风格并添加测试。

总结

  • nanobot 在一份 4,000 行 Python 代码仓库中提供完整功能的个人 AI 助手。
  • 它可在本地或 Docker 中运行,支持任何 OpenAI 兼容 LLM,并连接 Telegram/WhatsApp。
  • 模块化设计让你可以添加自定义技能、调度任务,甚至切换到本地模型。
  • 无论你是想要一个干净、即研究就可用的代理的研究员、想快速原型化的开发者,还是好奇构建 AI 助手的业余爱好者,nanobot 都能以最小摩擦提供你所需的一切。

祝你编码愉快!

原创文章: 查看原文

分享本文