Nanobot - Python 极简版
约 1132 字大约 4 分钟
2026-03-07
使用 Python 重写核心逻辑,代码量减少一个数量级的极简 AI 助手。
✨ 核心特点
🎯 极简设计
- 使用 Python 重写核心逻辑
- 代码量减少一个数量级
- 采用原生
while循环实现 ReAct(推理-行动)逻辑 - 无需复杂的 Node.js 环境
💾 轻量存储
- 记忆层直接读写本地 Markdown 和 JSON 文件
- 无需外部数据库
- 文件系统即数据库
- 易于备份和迁移
⚡ 快速部署
- pip 安装即可使用
- 依赖包最小化
- 内存占用 < 100MB
- 启动速度快
🚀 快速开始
安装
# 使用 pip 安装
pip install nanobot
# 或从源码安装
git clone https://github.com/HKUDS/nanobot.git
cd nanobot
pip install -e .配置
创建配置文件 config.yaml:
# LLM 配置
llm:
provider: openai
model: gpt-4
api_key: your-api-key-here
temperature: 0.7
# 记忆配置
memory:
type: file
path: ./memory
format: markdown
# 工具配置
tools:
- name: web_search
enabled: true
- name: file_operations
enabled: true
- name: code_execution
enabled: true运行
# 启动 Nanobot
nanobot start
# 或使用 Python 模块
python -m nanobot💻 技术架构
核心组件
# 简化的 ReAct 循环
while not task_completed:
# 1. 思考(Reasoning)
thought = llm.think(context)
# 2. 行动(Action)
action = llm.decide_action(thought)
result = execute_action(action)
# 3. 观察(Observation)
context.update(result)
# 4. 判断是否完成
task_completed = llm.is_task_done(context)记忆系统
# Markdown 格式的记忆存储
class MarkdownMemory:
def save(self, content):
with open(f"{self.path}/{timestamp}.md", "w") as f:
f.write(content)
def load(self, query):
# 简单的文件搜索
files = glob.glob(f"{self.path}/*.md")
return [f for f in files if query in open(f).read()]工具系统
# 简单的工具注册机制
@tool
def web_search(query: str) -> str:
"""搜索网络信息"""
return search_engine.search(query)
@tool
def read_file(path: str) -> str:
"""读取文件内容"""
with open(path, "r") as f:
return f.read()🎯 适用场景
✅ 推荐使用
- 个人极客日常使用: 轻量快速,易于定制
- 树莓派或小型服务器: 内存占用低,适合资源受限环境
- 快速理解和魔改: 代码简洁,易于阅读和修改
- 学习 AI Agent 原理: 核心逻辑清晰,适合学习
❌ 不推荐使用
- 需要高并发处理的生产环境
- 需要复杂的分布式架构
- 对性能有极致要求的场景
📊 性能指标
| 指标 | 数值 |
|---|---|
| 内存占用 | < 100MB |
| 启动时间 | < 2秒 |
| 响应延迟 | 取决于 LLM API |
| 并发支持 | 单进程 |
| 代码行数 | ~2000 行 |
🛠️ 高级功能
自定义工具
from nanobot import tool, Nanobot
@tool
def custom_tool(param: str) -> str:
"""自定义工具描述"""
# 你的逻辑
return result
# 注册工具
bot = Nanobot()
bot.register_tool(custom_tool)记忆检索
# 语义搜索(需要安装 sentence-transformers)
from nanobot.memory import SemanticMemory
memory = SemanticMemory(
path="./memory",
model="all-MiniLM-L6-v2"
)
# 检索相关记忆
results = memory.search("如何部署 Docker", top_k=5)插件系统
# 创建插件
class MyPlugin:
def on_start(self, bot):
print("Bot started!")
def on_message(self, message):
# 处理消息
pass
# 加载插件
bot.load_plugin(MyPlugin())📸 使用示例
命令行交互
$ nanobot
🤖 Nanobot v1.0.0
Type 'help' for commands, 'exit' to quit.
> 帮我搜索 Python 最佳实践
🤔 思考中...
📝 计划: 使用 web_search 工具搜索相关信息
🔍 执行: web_search("Python 最佳实践")
✅ 结果: 找到 10 条相关信息
💡 回答:
Python 最佳实践包括:
1. 遵循 PEP 8 代码风格
2. 使用虚拟环境管理依赖
3. 编写单元测试
...
> 保存到文件
✅ 已保存到 memory/2026-03-07-python-best-practices.mdPython API
from nanobot import Nanobot
# 创建实例
bot = Nanobot(config="config.yaml")
# 发送消息
response = bot.chat("帮我分析这段代码")
# 获取记忆
memories = bot.memory.search("代码分析")
# 执行工具
result = bot.execute_tool("web_search", query="Python")🔗 相关链接
- GitHub: HKUDS/nanobot
- 文档: 使用文档
- PyPI: nanobot
- 问题反馈: Issues
🤝 贡献
欢迎贡献代码、报告问题或提出建议!
# 克隆仓库
git clone https://github.com/HKUDS/nanobot.git
# 安装开发依赖
cd nanobot
pip install -e ".[dev]"
# 运行测试
pytest
# 代码格式化
black .📄 许可证
MIT License - 详见 LICENSE
🌟 为什么选择 Nanobot?
适合你,如果你...
- ✅ 想要快速上手 AI Agent
- ✅ 喜欢 Python 生态
- ✅ 需要轻量级解决方案
- ✅ 想要深度定制和魔改
- ✅ 在资源受限环境运行
不适合你,如果你...
- ❌ 需要企业级高并发
- ❌ 必须使用 Node.js 生态
- ❌ 需要复杂的分布式架构