Skip to content

工具调用

阿里云百炼部分模型支持 OpenAI 兼容工具调用,适合查询订单、搜索知识库、调用内部 API、执行受控业务操作。

工具定义

json
{
  "type": "function",
  "function": {
    "name": "get_order_status",
    "description": "Check order shipping status",
    "parameters": {
      "type": "object",
      "properties": {
        "order_id": {
          "type": "string",
          "description": "Order ID"
        }
      },
      "required": ["order_id"],
      "additionalProperties": false
    }
  }
}

调用流程

  1. 请求中传入 tools
  2. 模型返回工具调用请求
  3. 服务端校验参数和权限
  4. 服务端执行真实函数
  5. 将工具结果返回模型生成最终回答

示例

json
{
  "model": "qwen-plus-latest",
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_order_status",
        "description": "Check order status",
        "parameters": {
          "type": "object",
          "properties": {
            "order_id": { "type": "string" }
          },
          "required": ["order_id"]
        }
      }
    }
  ],
  "messages": [
    { "role": "user", "content": "Check the status of order A1001" }
  ]
}

最佳实践

  • 工具执行必须在服务端
  • 所有参数都要做 schema 和业务校验
  • 写操作要二次确认
  • 工具结果只返回回答所需的信息
  • 工具调用日志要能追踪用户、工具名、参数摘要和结果状态