skills/README.md
SKILL 机制是 DB-GPT Agent 框架的高级特性,允许 Agent 加载和管理预定义的技能包,实现 Agent 能力的模块化和可复用性。
packages/dbgpt-core/src/dbgpt/agent/skill/
├── __init__.py # 模块入口,导出主要类
├── base.py # Skill 基础类定义
├── parameters.py # Skill 参数类
├── manage.py # Skill 管理器
└── loader.py # Skill 加载器和构建器
Skill 包含以下组件:
| 类型 | 说明 |
|---|---|
Coding | 编程技能 |
DataAnalysis | 数据分析技能 |
WebSearch | 网络搜索技能 |
KnowledgeQA | 知识问答技能 |
Chat | 对话技能 |
Custom | 自定义技能 |
from dbgpt.agent.skill import SkillBuilder, SkillType
skill = (
SkillBuilder(name="my_skill", description="My awesome skill")
.with_version("1.0.0")
.with_author("Your Name")
.with_skill_type(SkillType.Coding)
.with_tags(["coding", "python"])
.with_prompt_template(
"You are a coding assistant. Help users write clean, efficient code."
)
.with_required_tool("python_interpreter")
.build()
)
from dbgpt.agent.skill import get_skill_manager, initialize_skill
from dbgpt.component import SystemApp
system_app = SystemApp()
initialize_skill(system_app)
skill_manager = get_skill_manager(system_app)
skill_manager.register_skill(
skill_instance=skill,
name="my_awesome_skill",
)
from dbgpt.agent import ConversableAgent
from dbgpt.agent.skill import Skill
class SkillBasedAgent(ConversableAgent):
def __init__(self, skill: Skill, **kwargs):
super().__init__(**kwargs)
self._skill = skill
self._apply_skill_to_profile()
@property
def skill(self) -> Skill:
return self._skill
agent = SkillBasedAgent(skill=skill)
await agent.bind(context).bind(llm_config).bind(memory).build()
| 方法 | 参数 | 说明 |
|---|---|---|
with_version(version) | version: str | 设置版本 |
with_author(author) | author: str | 设置作者 |
with_skill_type(type) | type: SkillType | 设置技能类型 |
with_tags(tags) | tags: List[str] | 设置标签 |
with_prompt_template(template) | template: str | 设置提示词模板 |
with_required_tool(name) | name: str | 添加必需工具 |
with_required_knowledge(name) | name: str | 添加必需知识库 |
with_action(action) | action: Any | 添加动作 |
with_config(config) | config: Dict | 设置配置 |
build() | - | 构建 Skill |
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
register_skill() | skill_cls, skill_instance, name, metadata | None | 注册技能 |
get_skill() | name, skill_type, version | SkillBase | 获取技能 |
get_skills_by_type() | skill_type | List[SkillBase] | 按类型获取技能 |
list_skills() | - | List[Dict] | 列出所有技能 |
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
load_skill_from_file() | file_path | Optional[SkillBase] | 从文件加载技能 |
load_skill_from_module() | module_path | Optional[SkillBase] | 从模块加载技能 |
load_skills_from_directory() | directory, recursive | List[SkillBase] | 从目录加载所有技能 |
{
"metadata": {
"name": "web_search_assistant",
"description": "Web search assistant",
"version": "1.0.0",
"author": "DB-GPT Team",
"skill_type": "web_search",
"tags": ["web", "search"]
},
"prompt_template": "You are a web search assistant.",
"required_tools": ["google_search"],
"required_knowledge": [],
"config": {}
}
from dbgpt.agent.skill import Skill, SkillMetadata, SkillType
from dbgpt.core import PromptTemplate
class CustomSkill(Skill):
def __init__(self):
metadata = SkillMetadata(
name="custom_skill",
description="A custom skill",
version="1.0.0",
skill_type=SkillType.Custom,
)
prompt = PromptTemplate.from_template("You are a custom assistant.")
super().__init__(
metadata=metadata,
prompt_template=prompt,
)
查看 examples/agents/skill_agent_example.py 获取完整的使用示例。
skills/web_search_skill.json - 网络搜索技能示例skills/data_analysis_skill.json - 数据分析技能示例skills/skill_implementation_guide.py - 详细的实现指南skills/INTEGRATION_GUIDE.md - 集成到现有 Agent 的指南导入 SKILL 模块
from dbgpt.agent.skill import Skill, SkillBuilder, get_skill_manager
修改 Agent 类
class MyAgent(ConversableAgent):
def __init__(self, skill: Optional[Skill] = None, **kwargs):
super().__init__(**kwargs)
self._skill = skill
if self._skill:
self._apply_skill_to_profile()
初始化 Skill Manager
from dbgpt.component import SystemApp
system_app = SystemApp()
initialize_skill(system_app)
注册并使用 Skill
skill_manager = get_skill_manager(system_app)
skill_manager.register_skill(skill_instance=skill)
agent = MyAgent(skill=skill)
class DynamicSkillAgent(ConversableAgent):
def switch_skill(self, skill_name: str):
self._skill = self._skills[skill_name]
self._apply_skill_to_profile()
class CompositeSkillAgent(ConversableAgent):
def __init__(self, skills: List[Skill], **kwargs):
super().__init__(**kwargs)
self._skills = skills
def get_all_tools(self) -> List[str]:
all_tools = []
for skill in self._skills:
all_tools.extend(skill.required_tools)
return list(set(all_tools))
Q: Skill 加载失败? A: 检查文件路径、JSON 格式是否正确
Q: 找不到必需的工具? A: 确保在绑定 Agent 时提供了所有必需的工具
Q: 提示词模板不生效?
A: 确保在 _apply_skill_to_profile 中正确设置了 bind_prompt
欢迎贡献新的 Skill!请遵循以下步骤:
MIT License
如有问题或建议,请提交 Issue 或 Pull Request。