AI之技能Skill简介

0 阅读5分钟

Skill(技能) 本质上是:可被模型调用、可组合、可复用、可执行的能力单元(Capability Unit),连接:

  • LLM 推理能力
  • 外部世界执行能力
  • 长期工作流能力

Skill 介绍

从“概率预测”到“确定性执行”: LLM 本质上是一个概率分布模型,擅长理解、生成、推理规划等,但不擅长执行精确的任务(API 调用、外部工具控制、状态管理、多步稳定执行等)。

生命周期执行时序逻辑

skill-sequence.png

本质

Skill 本质是结构化能力封装,可看作:Skill = Prompt + Tool + Memory + Workflow + Constraints

Skill 要解决以下核心问题:

问题作用
能力复用多 Agent 共用
Tool 封装隐藏复杂性
权限隔离安全执行
可测试单元测试
可观测Trace/Replay
可编排Planner 调度
长期记忆与 Memory 集成
环境隔离Sandbox
可部署独立运行
可版本化Skill Registry

作用

Skill将离散的专业知识、组织规范和工作流程,转化为Agent可发现、可复用、可演进的工程资产能力包:

  • 打破能力碎片化与上下文过载: 解决复杂任务中常面临,能力碎片化(需拼接多个独立模型或工具)、上下文丢失(长流程中记忆衰减)、执行不可控(推理与操作脱节,步骤易错、难回滚)。Skill通过渐进式披露机制,将技能信息分层按需加载:

    • 元数据(约几十Token)预载:仅加载 Skill 名称、触发条件、输入输出 Schema,极低开销完成意图匹配与调度决策
    • SKILL.md内容按需取用:仅在 Agent 判定需要此 Skill 时,才把详细的操作说明、规则判定树、参数示例等注入模型上下文
    • 附加脚本仅在需要时执行:脚本(Python/Bash 等)不进入 LLM 上下文,仅在运行时由沙箱调用,执行返回值再回送给 Agent
  • 打通专业领域知识的“最后一公里”: 通用大模型“知道但不会做”(本质是 陈述性知识(know-what)与程序性知识(know-how)的割裂),Skill 通过工程化封装:

    • 可执行脚本:将反复验证的自动化逻辑(如数据清洗、API 编排、异常重试、权限校验)固化为确定性代码,不再依赖模型间接推理
    • 操作指南:将特定场景下的决策顺序、条件分支、状态检查点与最终验收标准,以结构化文档(Markdown + 元数据)形式绑定给 Agent,使模型在生成每一步行动前都能获得精确的“步骤内指引”
  • 显著提升任务完成质量,从“基本不可用”到“生产级可靠”:LangChain的实测数据呈现相关任务的一次性通过率从低于 30%跃升至 95% ,且任务执行时间更短、可复现性更高。

生命周期

现代 Runtime 中,其生命周期:

skill-life-cycle.png

类比

与工具、子智能体比较

维度Tool (工具)Skill (技能)Sub-Agent (子智能体)
定义单一原子函数 (e.g. get_weather)具有上下文感知的复合能力 (e.g. analyze_log)具备独立规划能力的组件
状态无状态 (Stateless)可包含持久化状态或会话记忆完全自主的状态管理
复杂度低,直接映射 API中,包含逻辑预处理与后处理高,包含闭环推理

Skill 样例

Skill 可看作能被 Agent 动态调用的“能力模块”,通常包含:

组成作用
Manifest声明能力
Prompt行为约束
Tool外部能力
Runtime执行环境
Dependencies依赖
Schema输入输出
Memory Hook长期记忆
Permission安全控制
Tests回归验证

以下以 “网页技术文章抓取 + 摘要 + 存入知识库” 为例进行说明。

目录结构

完整目录结构(会根据实际任务进行取舍):

skills/
└── tech_article_research/
    ├── skill.yaml
    ├── README.md
    ├── requirements.txt
    ├── .env.example
    │
    ├── prompts/
    │   ├── system.md
    │   └── summarize.md
    │
    ├── schemas/
    │   ├── input.schema.json
    │   └── output.schema.json
    │
    ├── tools/
    │   ├── web_fetch.py
    │   ├── article_extract.py
    │   ├── embedding.py
    │   └── vectordb.py
    │
    ├── runtime/
    │   ├── executor.py
    │   ├── sandbox.py
    │   └── permissions.py
    │
    ├── memory/
    │   └── memory_hook.py
    │
    ├── tests/
    │   ├── test_skill.py
    │   └── fixtures/
    │
    └── logs/

核心 Manifest

核心文件 skill.yaml: 技能的唯一身份标识,定义名称、输入输出、入口、配置

name: tech_article_research

version: "1.0.0"

description: |
  Fetch technical article content, summarize it,
  and store embeddings into vector database.

author: xxxx

entrypoint: runtime/executor.py

runtime:
  type: python
  python_version: "3.12"

permissions:
  network: true
  filesystem: false
  shell: false

timeout:
  execution_seconds: 120

resources:
  memory_mb: 1024
  cpu: 1

tools:
  - tools/web_fetch.py
  - tools/article_extract.py
  - tools/embedding.py
  - tools/vectordb.py

environment:
  env_file: .env

dependencies:
  pip:
    - requests
    - beautifulsoup4
    - trafilatura
    - openai
    - chromadb
    - pydantic

input_schema: schemas/input.schema.json

output_schema: schemas/output.schema.json

memory:
  enabled: true
  hook: memory/memory_hook.py

observability:
  tracing: true
  metrics: true
  logging: info

输入 schema

schemas/input.schema.json: 输入格式校验,技能执行的契约必须项

{
  "type": "object",
  "properties": {
    "url": {
      "type": "string"
    },
    "topic": {
      "type": "string"
    }
  },
  "required": ["url"]
}

输出 schema

schemas/output.schema.json:输出格式校验,技能执行的契约必须项

{
  "type": "object",
  "properties": {
    "title": {
      "type": "string"
    },
    "summary": {
      "type": "string"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

Prompt System

prompts/system.md: 系统提示词、总结提示词,属于核心逻辑必须

You are a senior technical research assistant.

Your responsibilities:

1. Extract core technical insights
2. Ignore marketing content
3. Focus on architecture and engineering value
4. Produce concise structured summaries
5. Generate reusable knowledge chunks

Tool 示例

1、网页抓取 Tool

tools/web_fetch.py

import requests

def fetch_url(url: str) -> str:
    response = requests.get(
        url,
        timeout=20,
        headers={
            "User-Agent": "SkillBot/1.0"
        }
    )

    response.raise_for_status()

    return response.text

2、正文提取 Tool

tools/article_extract.py

import trafilatura

def extract_article(html: str) -> str:
    content = trafilatura.extract(html)

    if not content:
        raise RuntimeError("Article extraction failed")

    return content

3、Embedding Tool

tools/embedding.py

from openai import OpenAI

client = OpenAI()

def create_embedding(text: str):
    result = client.embeddings.create(
        model="text-embedding-3-small",
        input=text
    )

    return result.data[0].embedding

4、VectorDB Tool

tools/vectordb.py

import chromadb

client = chromadb.Client()

collection = client.get_or_create_collection(
    "tech_articles"
)

def store_document(doc_id, text, embedding):
    collection.add(
        ids=[doc_id],
        documents=[text],
        embeddings=[embedding]
    )

Runtime 执行器

runtime/executor.py

from tools.web_fetch import fetch_url
from tools.article_extract import extract_article
from tools.embedding import create_embedding
from tools.vectordb import store_document

from openai import OpenAI

client = OpenAI()

def run(input_data):

    url = input_data["url"]

    html = fetch_url(url)

    article = extract_article(html)

    completion = client.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {
                "role": "system",
                "content": open(
                    "prompts/system.md"
                ).read()
            },
            {
                "role": "user",
                "content": article
            }
        ]
    )

    summary = completion.choices[0].message.content

    embedding = create_embedding(article)

    store_document(
        doc_id=url,
        text=article,
        embedding=embedding
    )

    return {
        "title": url,
        "summary": summary,
        "tags": [
            "AI",
            "Research"
        ]
    }

Memory Hook

memory/memory_hook.py现代 Agent 核心

def after_run(result):

    memory_record = {
        "type": "article_summary",
        "content": result["summary"]
    }

    return memory_record

作用:

Skill -> 自动写入长期记忆

Sandbox

runtime/sandbox.py(生产系统关键)

import resource

def limit_resources():

    resource.setrlimit(
        resource.RLIMIT_CPU,
        (30, 30)
    )

    resource.setrlimit(
        resource.RLIMIT_AS,
        (1024 * 1024 * 1024,
         1024 * 1024 * 1024)
    )

作用:

限制作用
CPU防死循环
Memory防 OOM
Network防滥用
Filesystem防越权
Shell防 RCE