Nanobot技能开发:自定义AI能力扩展

张开发
2026/5/5 15:25:29 15 分钟阅读
Nanobot技能开发:自定义AI能力扩展
Nanobot技能开发自定义AI能力扩展1. 引言想给你的AI助手添加一些特殊能力吗比如让它帮你查天气、管理待办事项或者连接你常用的工具Nanobot的技能系统让你可以轻松扩展AI的能力边界。今天我们就来手把手教你如何为Nanobot开发自定义技能。不用担心即使你是编程新手跟着步骤走也能轻松上手。我们将从最简单的Hello World技能开始逐步深入到更实用的功能开发。2. 环境准备与项目结构2.1 安装Nanobot首先确保你已经安装了Nanobot。如果还没有可以通过pip快速安装pip install nanobot-ai或者从源码安装推荐给想要深入了解的开发者git clone https://github.com/HKUDS/nanobot.git cd nanobot pip install -e .2.2 项目结构概览了解Nanobot的项目结构对技能开发很重要nanobot/ ├── skills/ # 技能目录 │ ├── builtin/ # 内置技能 │ └── custom/ # 自定义技能你的舞台 ├── agent/ │ └── tools/ # 工具注册系统 └── config/ # 配置文件3. 创建你的第一个技能3.1 最简单的Hello World技能让我们从一个最简单的技能开始。在nanobot/skills/custom/目录下创建hello_world.md文件# Hello World技能 这是一个简单的问候技能示例。 ## 功能描述 让AI助手能够用不同的语言打招呼 ## 使用示例 用户可以说用中文打招呼 或 say hello in spanish ## 实现代码 python tool def greet_in_language(language: str english) - str: 根据指定语言返回问候语 greetings { english: Hello! How can I help you today?, chinese: 你好今天需要什么帮助, spanish: ¡Hola! ¿En qué puedo ayudarte hoy?, french: Bonjour ! Comment puis-je vous aider aujourdhui ? } return greetings.get(language.lower(), Hello! Nice to meet you.)保存文件后重启Nanobot你的新技能就自动加载了3.2 测试你的技能在终端中测试新技能nanobot agent -m 请用中文打招呼你应该能看到AI助手用中文回应你。4. 开发实用技能示例4.1 天气查询技能让我们创建一个更实用的天气查询技能。创建weather_check.md文件# 天气查询技能 实时查询各地天气情况 ## 功能描述 提供指定城市的当前天气信息 ## 依赖配置 需要配置天气API密钥如OpenWeatherMap ## 实现代码 python import requests from datetime import datetime tool def get_weather(city: str, api_key: str None) - str: 获取指定城市的天气信息 if not api_key: return 请先配置天气API密钥 try: url fhttp://api.openweathermap.org/data/2.5/weather?q{city}appid{api_key}unitsmetric response requests.get(url) data response.json() if data[cod] ! 200: return f无法获取{city}的天气信息 weather { city: data[name], temperature: data[main][temp], description: data[weather][0][description], humidity: data[main][humidity], wind_speed: data[wind][speed] } return (f{weather[city]}的天气{weather[description]} f温度{weather[temperature]}°C f湿度{weather[humidity]}% f风速{weather[wind_speed]}m/s) except Exception as e: return f获取天气信息时出错{str(e)}4.2 待办事项管理技能创建一个简单的待办事项管理技能# 待办事项管理 管理个人待办事项列表 ## 功能描述 添加、查看、完成待办事项 ## 实现代码 python import json import os from pathlib import Path class TodoManager: def __init__(self, workspace_path: str): self.todo_file Path(workspace_path) / todo_list.json self.todos self._load_todos() def _load_todos(self): if self.todo_file.exists(): with open(self.todo_file, r, encodingutf-8) as f: return json.load(f) return [] def _save_todos(self): with open(self.todo_file, w, encodingutf-8) as f: json.dump(self.todos, f, ensure_asciiFalse, indent2) def add_todo(self, task: str) - str: 添加新的待办事项 new_todo { id: len(self.todos) 1, task: task, completed: False, created_at: datetime.now().isoformat() } self.todos.append(new_todo) self._save_todos() return f已添加待办事项{task} def list_todos(self, show_completed: bool False) - str: 列出待办事项 if not self.todos: return 当前没有待办事项 visible_todos [t for t in self.todos if show_completed or not t[completed]] if not visible_todos: return 没有未完成的待办事项 if not show_completed else 没有任何待办事项 result [待办事项列表] for todo in visible_todos: status ✅ if todo[completed] else ⏳ result.append(f{todo[id]}. {status} {todo[task]}) return \n.join(result) def complete_todo(self, todo_id: int) - str: 标记待办事项为已完成 for todo in self.todos: if todo[id] todo_id: todo[completed] True todo[completed_at] datetime.now().isoformat() self._save_todos() return f已完成{todo[task]} return f找不到ID为{todo_id}的待办事项 # 在技能初始化时创建TodoManager实例 todo_manager TodoManager(config.workspace) tool def add_todo(task: str) - str: 添加待办事项 return todo_manager.add_todo(task) tool def list_todos(show_completed: bool False) - str: 列出待办事项 return todo_manager.list_todos(show_completed) tool def complete_todo(todo_id: int) - str: 完成待办事项 return todo_manager.complete_todo(todo_id)5. 技能配置与依赖管理5.1 配置文件设置为了让天气技能正常工作需要在Nanobot配置文件中添加API密钥{ skills: { weather: { api_key: your_weather_api_key_here } } }5.2 依赖管理如果你的技能需要额外的Python包可以在技能文件中声明## 依赖要求 requirements requests2.25.0 python-dateutil2.8.0Nanobot会在加载技能时自动检查并提示安装缺失的依赖。6. 安全沙箱配置6.1 理解安全限制Nanobot默认在安全沙箱中运行技能这意味着文件访问限制在workspace目录内网络访问受到控制系统命令执行需要明确权限6.2 配置技能权限在技能文件中声明需要的权限## 所需权限 - 文件读写workspace目录 - 网络访问api.openweathermap.org或者在配置文件中为特定技能授予权限{ security: { skill_permissions: { weather_check: { network_access: [api.openweathermap.org], file_access: [read, write] } } } }7. 调试与测试技巧7.1 技能调试当技能出现问题时可以启用调试模式nanobot agent --debug -m 测试天气查询7.2 单元测试为你的技能编写简单的测试# test_weather_skill.py def test_weather_skill(): # 测试正常情况 result get_weather(北京, test_key) assert 北京 in result or 错误 in result # 测试错误情况 result get_weather(不存在的城市, test_key) assert 无法获取 in result or 错误 in result8. 高级技能开发8.1 使用外部API更复杂的技能可能需要调用多个APItool def get_news(category: str technology, count: int 5) - str: 获取指定分类的新闻头条 # 这里可以集成新闻API pass8.2 定时任务技能创建可以定时执行的技能# 每日简报技能 每天早晨发送个性化简报 ## 定时配置 cron 0 9 * * * # 每天上午9点执行实现代码tool def daily_briefing() - str: 生成每日简报 weather get_weather(北京, config.skills.weather.api_key) news get_news(technology, 3) todos list_todos(False) return f早安今日简报\n\n天气{weather}\n\n科技新闻{news}\n\n待办事项{todos}9. 总结通过本文的学习你应该已经掌握了Nanobot技能开发的基本方法。从简单的问候技能到实用的天气查询和待办事项管理技能开发其实并不复杂。关键是要从小处着手先实现一个简单可用的功能然后逐步添加更多特性。记得在开发过程中充分考虑安全性合理配置权限和沙箱限制。最好的学习方式就是动手实践。选择一个你需要的功能尝试为你的Nanobot创建一个专属技能吧获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章