跳转到主要内容

文档索引

获取完整文档索引: https://docs.crewai.com.cn/llms.txt

在深入了解之前,请使用此文件来浏览所有可用页面。

概述

技能是独立的目录,为智能体提供特定领域的指令、准则和参考资料。每个技能都由一个带有 YAML 前置数据(frontmatter)和 Markdown 正文的 SKILL.md 文件定义。 当技能被激活时,其指令会被直接注入到智能体的任务提示词中——这无需修改任何代码即可赋予智能体专业知识。
技能不是工具。 这是最常见的误区。
  • 技能指令和上下文注入智能体的提示词中。它们告诉智能体如何思考一个问题。
  • 工具为智能体提供可调用的函数以执行操作(搜索、读取文件、调用 API)。
你通常需要两者结合:用技能提供专业知识,用工具执行操作。它们是独立配置且相辅相成的。

快速入门

1. 创建技能目录

skills/
└── code-review/
    ├── SKILL.md          # Required — instructions
    ├── references/       # Optional — reference docs
    │   └── style-guide.md
    └── scripts/          # Optional — executable scripts

2. 编写你的 SKILL.md

---
name: code-review
description: Guidelines for conducting thorough code reviews with focus on security and performance.
metadata:
  author: your-team
  version: "1.0"
---

## Code Review Guidelines

When reviewing code, follow this checklist:

1. **Security**: Check for injection vulnerabilities, auth bypasses, and data exposure
2. **Performance**: Look for N+1 queries, unnecessary allocations, and blocking calls
3. **Readability**: Ensure clear naming, appropriate comments, and consistent style
4. **Testing**: Verify adequate test coverage for new functionality

### Severity Levels
- **Critical**: Security vulnerabilities, data loss risks → block merge
- **Major**: Performance issues, logic errors → request changes
- **Minor**: Style issues, naming suggestions → approve with comments

3. 关联至智能体

from crewai import Agent
from crewai_tools import GithubSearchTool, FileReadTool

reviewer = Agent(
    role="Senior Code Reviewer",
    goal="Review pull requests for quality and security issues",
    backstory="Staff engineer with expertise in secure coding practices.",
    skills=["./skills"],                          # Injects review guidelines
    tools=[GithubSearchTool(), FileReadTool()],   # Lets agent read code
)
现在,该智能体既拥有专业知识(来自技能),又具备执行能力(来自工具)。

技能 + 工具:协同工作

以下是展示技能和工具如何互补的常见模式:

模式 1:仅使用技能(领域专业知识,无需操作)

适用于智能体需要特定指令,但无需调用外部服务的场景。
agent = Agent(
    role="Technical Writer",
    goal="Write clear API documentation",
    backstory="Expert technical writer",
    skills=["./skills/api-docs-style"],  # Writing guidelines and templates
    # No tools needed — agent writes based on provided context
)

模式 2:仅使用工具(操作,无需特殊专业知识)

适用于智能体需要执行操作,但无需特定领域指令的场景。
from crewai_tools import SerperDevTool, ScrapeWebsiteTool

agent = Agent(
    role="Web Researcher",
    goal="Find information about a topic",
    backstory="Skilled at finding information online",
    tools=[SerperDevTool(), ScrapeWebsiteTool()],  # Can search and scrape
    # No skills needed — general research doesn't need special guidelines
)

模式 3:技能 + 工具(专业知识 AND 操作)

这是现实中最常见的模式。技能提供如何处理工作的方法;工具提供智能体能做什么
from crewai_tools import SerperDevTool, FileReadTool, CodeInterpreterTool

analyst = Agent(
    role="Security Analyst",
    goal="Audit infrastructure for vulnerabilities",
    backstory="Expert in cloud security and compliance",
    skills=["./skills/security-audit"],   # Audit methodology and checklists
    tools=[
        SerperDevTool(),                  # Research known vulnerabilities
        FileReadTool(),                   # Read config files
        CodeInterpreterTool(),            # Run analysis scripts
    ],
)

模式 4:技能 + MCP

技能与 MCP 服务器配合使用的方式与配合工具使用的方式相同。
agent = Agent(
    role="Data Analyst",
    goal="Analyze customer data and generate reports",
    backstory="Expert data analyst with strong statistical background",
    skills=["./skills/data-analysis"],           # Analysis methodology
    mcps=["https://data-warehouse.example.com/sse"],  # Remote data access
)

模式 5:技能 + 应用

技能可以指导智能体如何使用平台集成功能。
agent = Agent(
    role="Customer Support Agent",
    goal="Respond to customer inquiries professionally",
    backstory="Experienced support representative",
    skills=["./skills/support-playbook"],  # Response templates and escalation rules
    apps=["gmail", "zendesk"],             # Can send emails and update tickets
)

Crew(团队)级技能

技能可以设置在 Crew 上,从而应用于所有智能体
from crewai import Crew

crew = Crew(
    agents=[researcher, writer, reviewer],
    tasks=[research_task, write_task, review_task],
    skills=["./skills"],  # All agents get these skills
)
智能体级技能具有优先权——如果同一技能在两个级别都被发现,将使用智能体版本的技能。

SKILL.md 格式

---
name: my-skill
description: Short description of what this skill does and when to use it.
license: Apache-2.0                    # optional
compatibility: crewai>=0.1.0          # optional
metadata:                              # optional
  author: your-name
  version: "1.0"
allowed-tools: web-search file-read   # optional, experimental
---

Instructions for the agent go here. This markdown body is injected
into the agent's prompt when the skill is activated.

前置数据字段

字段必填描述
name1-64 个字符。小写字母、数字和连字符。必须与目录名匹配。
description (描述)1-1024 个字符。描述技能的作用及使用场景。
license许可名称或对捆绑许可文件的引用。
compatibility最多 500 个字符。环境要求(产品、包、网络)。
metadata任意字符串键值映射。
allowed-tools空格分隔的预批准工具列表。实验性功能。

目录结构

my-skill/
├── SKILL.md            # Required — frontmatter + instructions
├── scripts/            # Optional — executable scripts
├── references/         # Optional — reference documents
└── assets/             # Optional — static files (configs, data)
目录名称必须与 SKILL.md 中的 name 字段匹配。对于需要直接引用文件的智能体,scripts/references/assets/ 目录可在技能的 path 中使用。

预加载技能

为了获得更多控制权,你可以通过编程方式发现并激活技能。
from pathlib import Path
from crewai.skills import discover_skills, activate_skill

# Discover all skills in a directory
skills = discover_skills(Path("./skills"))

# Activate them (loads full SKILL.md body)
activated = [activate_skill(s) for s in skills]

# Pass to an agent
agent = Agent(
    role="Researcher",
    goal="Find relevant information",
    backstory="An expert researcher.",
    skills=activated,
)

技能如何加载

技能采用渐进式披露——仅在每个阶段加载所需内容。
阶段加载内容时间
发现(Discovery)名称、描述、前置数据字段discover_skills()
激活(Activation)完整的 SKILL.md 正文activate_skill()
在正常的智能体执行期间(通过 skills=["./skills"] 传递目录路径),技能会自动发现并激活。渐进式加载仅在使用编程 API 时才有意义。

技能 vs 知识

技能和知识都会修改智能体的提示词,但它们有不同的用途。
方面技能 (Skills)知识
提供内容指令、流程、指南事实、数据、信息
存储方式Markdown 文件 (SKILL.md)嵌入向量存储 (ChromaDB)
检索方式整个正文注入提示词语义搜索查找相关分段
最适合方法论、检查清单、风格指南公司文档、产品信息、参考数据
设置通过skills=["./skills"]knowledge_sources=[source]
经验法则: 如果智能体需要遵循特定的流程,请使用技能。如果智能体需要引用数据,请使用知识。

常见问题

取决于你的使用场景。技能和工具是独立的——你可以单独使用其中一个、同时使用两个或都不使用。
  • 仅技能:当智能体需要专业知识但无需外部操作时(例如:按照风格指南写作)。
  • 仅工具:当智能体需要执行操作但无需特殊方法论时(例如:简单的网页搜索)。
  • 两者结合:当智能体既需要专业知识又需要执行操作时(例如:使用特定清单进行安全审计,并具备扫描代码的能力)。
不会。 SKILL.md 中的 allowed-tools 字段仅为实验性元数据——它不会配置或注入任何工具。你必须始终通过 tools=[]mcps=[]apps=[] 单独设置工具。
智能体级技能具有优先权。技能按名称去重——智能体的技能会先被处理,因此如果同一技能名称在两个级别出现,将使用智能体版本的技能。
在 50,000 字符时会有软警告,但没有硬限制。为了达到最佳效果,请保持技能内容聚焦且简洁——过大的提示词注入可能会分散智能体的注意力。