今年双十一那天,我朋友的小红书女装店爆单了——日均咨询量从 800 条飙到 1.2 万条,原本的关键词机器人直接崩盘。我作为接私活的独立开发者,紧急花了一周时间给店铺搭了一套 AI 客服 Agent,核心能力包括"查订单、改地址、申请退款、推荐尺码"。在设计 Agent Skills(也就是工具调用 schema)时,我在 JSON SchemaYAML 之间反复横跳,最终踩了不少坑。本文把整个决策过程、真实代码、价格对比和报错复盘一次性讲清楚。

如果你是第一次接触 HolySheep,可以先 立即注册,注册即送免费额度,新用户首月还有额外赠额,国内直连延迟稳定在 50ms 以内。

一、Agent Skills 到底是什么?为什么序列化格式很关键?

Agent Skills 在大模型语境下,指的是"模型可调用的工具/函数结构化描述"。它包含三件事:工具名称、参数 schema、返回值说明。当 LLM 决定调用工具时,必须严格按这套契约填参,所以序列化标准的选择直接决定了:

二、JSON Schema 方案:电商客服 Agent 实战

我最初选的是 JSON Schema 路线,原因是 OpenAI、Anthropic、Google 三大厂的 Function Calling 协议底层都是 JSON Schema,生态最成熟。下面是当时给"申请退款"工具写的定义:

// skills/refund_request.json
{
  "name": "request_refund",
  "description": "用户申请退款时调用。需校验订单号、金额、原因三项必填。",
  "parameters": {
    "type": "object",
    "properties": {
      "order_id": {
        "type": "string",
        "pattern": "^ORD[0-9]{10}$",
        "description": "10位订单号,ORD开头"
      },
      "amount": {
        "type": "number",
        "minimum": 0.01,
        "maximum": 5000,
        "description": "退款金额,单位元"
      },
      "reason": {
        "type": "string",
        "enum": ["尺码不对", "质量问题", "不想要了", "其他"],
        "description": "退款原因枚举"
      }
    },
    "required": ["order_id", "amount", "reason"],
    "additionalProperties": false
  }
}

实际调用时我用的就是 HolySheep 统一网关,base_url 改成国内直连后,单次工具调用的 P99 延迟从 1.8s 降到 380ms:

import openai, json

client = openai.OpenAI(
    base_url="https://api.holysheep.ai/v1",
    api_key="YOUR_HOLYSHEEP_API_KEY"
)

with open("skills/refund_request.json", "r", encoding="utf-8") as f:
    refund_skill = json.load(f)

resp = client.chat.completions.create(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "订单ORD2024110801要退款89元,尺码不对"}],
    tools=[{"type": "function", "function": refund_skill}],
    tool_choice="auto"
)
print(resp.choices[0].message.tool_calls[0].function.arguments)

{"order_id":"ORD2024110801","amount":89,"reason":"尺码不对"}

三、YAML 方案:当我把它引入 RAG 知识库时

第二周我开始接入企业的 RAG 知识库检索工具,团队里前端、后端、数据标注三个人都要改同一份 skills 文件。JSON 写起来太啰嗦,diff 一片红,PR 几乎没法看。我把同一份契约用 YAML 重写:

# skills/refund_request.yaml
name: request_refund
description: 用户申请退款时调用。需校验订单号、金额、原因三项必填。
parameters:
  type: object
  additionalProperties: false
  required: [order_id, amount, reason]
  properties:
    order_id:
      type: string
      pattern: "^ORD[0-9]{10}$"
      description: 10位订单号,ORD开头
    amount:
      type: number
      minimum: 0.01
      maximum: 5000
      description: 退款金额,单位元
    reason:
      type: string
      enum: [尺码不对, 质量问题, 不想要了, 其他]
      description: 退款原因枚举

加载方式用 PyYAML 一行解决,但要记得在传给 HolySheep 网关前再序列化成 JSON(厂商协议只吃 JSON 字符串):

import yaml, openai

with open("skills/refund_request.yaml", "r", encoding="utf-8") as f:
    refund_skill = yaml.safe_load(f)

client = openai.OpenAI(
    base_url="https://api.holysheep.ai/v1",
    api_key="YOUR_HOLYSHEEP_API_KEY"
)

resp = client.chat.completions.create(
    model="claude-sonnet-4.5",
    messages=[{"role": "user", "content": "ORD2024110888退款150,质量问题"}],
    tools=[{"type": "function", "function": refund_skill}],
    tool_choice="auto"
)
print(resp.choices[0].message.tool_calls[0].function.arguments)

四、核心差异对比表