← 返回首页
🧠

函数Schema

📂 llm ⏱ 1 min 160 words

函数Schema概述

函数Schema(Function Schema)是使用JSON Schema格式定义函数接口的规范。在LLM函数调用中,Schema定义了函数的名称、描述、参数结构和约束,是模型理解如何调用函数的关键。

Schema结构

基本框架

{
  "name": "get_weather",
  "description": "获取指定城市的天气信息",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "城市名称"
      },
      "unit": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"],
        "description": "温度单位"
      }
    },
    "required": ["city"]
  }
}

字段说明

类型系统

基本类型

复合类型

约束属性

设计原则

描述清晰

参数最小化

向前兼容

嵌套Schema

支持复杂的参数结构:

{
  "parameters": {
    "type": "object",
    "properties": {
      "filters": {
        "type": "object",
        "properties": {
          "date_range": {
            "type": "object",
            "properties": {
              "start": {"type": "string", "format": "date"},
              "end": {"type": "string", "format": "date"}
            }
          },
          "category": {"type": "string", "enum": ["all", "news", "sports"]}
        }
      }
    }
  }
}

多函数Schema

定义多个可用函数:

{
  "functions": [
    {"name": "search", "description": "搜索信息", ...},
    {"name": "calculate", "description": "执行计算", ...},
    {"name": "translate", "description": "翻译文本", ...}
  ]
}

模型根据用户意图选择合适的函数。

验证与测试

最佳实践

  1. 函数命名遵循一致的命名规范
  2. description应包含使用场景和约束
  3. 使用enum限制可选值
  4. 为复杂参数提供示例
  5. 定期审查和更新Schema设计

良好的函数Schema设计是实现可靠LLM工具调用的基础。