BLACK HOLE 丨 黑洞智能体
Docs

并行工具调用(Function Calling)与结构化 JSON 输出

TOOLS & JSON · 构建生产级数据库查询、外部 API 编排与严格 Schema 抽取智能体

详解如何在统一网关中使用标准 tools 数组实现多函数并行调用,以及利用 response_format 强制输出符合 JSON Schema 的类型安全结果。

并行工具调用(Tools)标准闭环

在请求中传入 tools JSON Schema 列表后,模型会根据用户意图自动决策是否返回一个或多个 tool_calls。你的服务端执行本地函数后,将结果以 role: "tool" 消息回传即可完成闭环推理。

TypeScript

const res = await client.chat.completions.create({
  model: 'glm-5.3',
  messages: [{ role: 'user', content: '查询成都和上海今天的算力节点延迟并对比' }],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_region_latency',
        description: '查询指定可用区当前的网关平均首字延迟(毫秒)',
        parameters: {
          type: 'object',
          properties: {
            region: { type: 'string', enum: ['cn-chengdu', 'cn-shanghai', 'cn-beijing'] }
          },
          required: ['region']
        }
      }
    }
  ],
  tool_choice: 'auto'
})

严格 JSON Schema 结构化输出

对于工单分拣、简历解析、财报指标抽取等不允许出现 Markdown 废话包裹的场景,推荐配置 response_format 为 json_schema 或 json_object:

Python

resp = client.chat.completions.create(
    model="deepseek-v4.1-flash-1017",
    messages=[
        {"role": "system", "content": "从用户输入中提取结构化工单信息。"},
        {"role": "user", "content": "订单号 BH2026120399 支付了 199 元但发票抬头需要改为广元市黑洞科技有限公司"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "ticket_extract",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "order_no": {"type": "string"},
                    "amount_cny": {"type": "number"},
                    "company_title": {"type": "string"}
                },
                "required": ["order_no", "amount_cny", "company_title"],
                "additionalProperties": False
            }
        }
    }
)
print(resp.choices[0].message.content)

On this page